www.cs.cityu.edu.hk/~helena Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 Bubble Sort Scan the array from left to right, exchange.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Back to Sorting – More efficient sorting algorithms.
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Garfield AP Computer Science
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
CS 112 Introduction to Programming Sorting of an Array Debayan Gupta Computer Science Department Yale University 308A Watson, Phone:
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
System Programming in C Lecture 4. Lecture Summary Operations with Arrays: –Searching the array –Sorting the array.
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Data Structures Chapter 8 Sorting Andreas Savva. 2 Sorting Smith Sanchez Roberts Kennedy Jones Johnson Jackson Brown George Brown 32 Cyprus Road Good.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
CSE 373: Data Structures and Algorithms
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
Simple Sorting Algorithms
C++ Plus Data Structures
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
Mergesort. Merging two sorted arrays To merge two sorted arrays into a third (sorted) array, repeatedly compare the two least elements and copy the smaller.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Computer Science Searching & Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Sorting. 2 contents 3 kinds of sorting methods – Selection, exchange, and insertion O(n 2 ) sorts – VERY inefficient, but OK for ≈ 10 elements – Simple.
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Some comments on lab4. Hi Philippe! Can you tell me if my code works? Thanks! I’ll show you what works…
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
3 – SIMPLE SORTING ALGORITHMS
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Recursion Method calls itself iteratively until a base case is met and usually containing the following: if-else for base case with return value increment/decrement.
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Selection Sort Given an array[0-N], place the smallest item in the array in position 0, the second smallest in position 1, and so forth. We do thisby comparing.
Merge Sort. In plain English: if the size of the array > 1, split the array into two halves, and recursively sort both halves; when the sorts return,
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
1 Sorting. 2 Sorting Data Items Consider a set of data items  Each item may have more than one field Example: a student record with name, roll no, CGPA,…
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Searching and Sorting Searching algorithms with simple arrays
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Bohyung Han CSE, POSTECH
Sorting Mr. Jacobs.
Simple Sorting Algorithms
Searching and Sorting Arrays
Sorting.
Principles of Computing – UFCFA3-30-1
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Searching and Sorting Arrays
CS 101 – Oct. 21 Sorting Much-studied problem in CS – many ways to do it Given a list of data, need to arrange it “in order” Some methods do better based.
Principles of Computing – UFCFA3-30-1
Presentation transcript:

Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 Bubble Sort Scan the array from left to right, exchange pairs of elements that are out-of-order. Repeat the above process for upon (N-1) times where N is the number of records in the array. Example: (1st pass ) X[0..7] The last slot now has the largest data II Bubble Sort - Basic

Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 2 Bubble Sort (2nd pass ) X[0..6] This slot now has the 2nd largest data (3rd pass ) X[0..5] This slot now has the 3rd largest data Others’ ordering may also be improved like the above. II Bubble Sort - Basic

Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 3 Bubble Sort Result: Original (N=8): After pass 1: (x[0..7]) After pass 2: (x[0..6]) After pass 3: (x[0..5]) After pass 4: (x[0..4]) After pass 5: (x[0..3]) After pass 6: (x[0..2]) After pass 7: (x[0..1]) void bubblesort(int x[ ], int N) { int up_to_pos=N-1; //prepare up_to_pos for first pass while ( up_to_pos>=1 ) //perform N-1 passes { for (int j=0; j+1<= up_to_pos ; j++) //process each pair {if (x[j] > x[j+1]) swap(x[j],x[j+1]); } up_to_pos --; //prepare up_to_pos for next pass } } up_to_position II Bubble Sort - Basic

Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 4 Insertion Sort Insertion sort –Repeatedly insert a new element into an already sorted list Example: x[0] is sorted Insert (33) to get x[0..1] sorted Insert (21) to get x[0..2] sorted Insert (84) to get x[0..3] sorted Insert (49) to get x[0..4] sorted Insert (50) to get x[0..5] sorted Insert (75) to get x[0..6] sorted III Linear Insertion Sort - Idea

Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 5 Example: To find a suitable position for (49), - we compare 49 with 84 : Because 84>49, we shift 84 to the right. -Then compare 49 with 67 : Because 67>49, we shift 67 to the right. - Then compare 49 with 33 : Here 33 < 49!!, so 49 should be next to 33. We stop searching and put 49 there. 1for i = 1 to n-1 /*ie. for each x[i]=33, 21, 84,.. 75 */ /* find a suitable position in x[0..i] for x[i] and put x[i] there - the search is done from x[i-1] towards x[0] - the search is stopped once the suitable position for x[i] is found - during the search, shift these neighbors to the right to make a hole for x[i] */ 2value_i = x[i] 3neighbr_pos = i-1 4while neighbr_pos >= 0 and A[neighbr_pos] > value_i 5A[neighbr_pos+1] = A[neighbr_pos] //shift the neighbour 6 neighbr_pos --; //go to next neighbour 7x[neighbr_pos +1] = value_i III Linear Insertion Sort - Algorithm

Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 6 Merge Sort At the beginning, a Mr. MergeSort is called to sort: Then 2 other Mr. MergeSorts are called to sort: Both of them say “Still complicated! I’ll split them and call other Mr. MergeSorts to handle.” Then 4 other Mr. MergeSorts are called to sort: All of them say “Still complicated! I’ll split them and call other Mr. MergeSorts to handle.” Then 8 other Mr. MergeSorts are called to sort: “So complicated!!, I’ll split them and call other Mr. MergeSorts to handle.” All of them say ‘This is easy. No need to do anything.’

Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 7 Merge Sort Then the first Mr. MergeSort succeeds and returns. Then each of the 2 Mr. MergeSorts returns the merged numbers. Then the 4 Mr. MergeSorts returns the merged numbers. Then the 8 Mr. MergeSorts return All of them say ‘This is easy. No need do anything.’ Both Mr. MergeSorts call their secretaries Mr. Merge to merge the returned numbers The 4 Mr. MergeSorts call their secretaries Mr. Merge to merge the returned numbers The first Mr. MergeSort calls his secretary Mr. Merge to merge the returned numbers

Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 8 Merge Sort void MERGE-SORT(x, Lower_bound, Upper_bound) Sorts the elements: x = low high void merge-sort (int x[ ], int low, int high) { if (low < high) { int mid, *buffer = new int[high-low+1]; mid = (low + high) / 2; merge-sort(x, low, mid); merge-sort(x, mid+1, high);.. merge the two sorted lists into buffer[].. copy buffer[] into x[low..high] delete [] buffer; }