Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Sorting Arrays. COMP104 Lecture 25 / Slide 2 Sorting l To arrange a set of items in sequence. l It was estimated that 25~50% of all computing.

Similar presentations


Presentation on theme: "Programming Sorting Arrays. COMP104 Lecture 25 / Slide 2 Sorting l To arrange a set of items in sequence. l It was estimated that 25~50% of all computing."— Presentation transcript:

1 Programming Sorting Arrays

2 COMP104 Lecture 25 / Slide 2 Sorting l To arrange a set of items in sequence. l It was estimated that 25~50% of all computing power is being expended in sorting activities. l Possible reasons: n Many applications require sorting n Many applications perform sorting when they don't have to n Many applications use inefficient sorting algorithms

3 COMP104 Lecture 25 / Slide 3 Sorting Applications l List of student ID names and numbers in a table (sorted by name) l List of scores before letter grade l assignment (sorted by students' scores) l List of horses after a race (sorted by the finishing times) l To prepare an originally unsorted array for ordered binary searching

4 COMP104 Lecture 25 / Slide 4 Some Sorting Methods l Selection sort l Bubble sort l Shell sort l Quick sort

5 COMP104 Lecture 25 / Slide 5 Selection Sort l Selection sort performs sorting by n repeatedly putting the smallest element in the first position of the unsorted portion, n until the whole array is sorted. l It is similar to the way that many people do their sorting.

6 COMP104 Lecture 25 / Slide 6 Selection Sort l Algorithm 1. Define the unsorted portion of the array as the entire array 2. While the unsorted portion of the array has more than one element:  Find its smallest element  Swap with first element of the sorted portion of the array  Reduce unsorted portion of the array by 1

7 COMP104 Lecture 25 / Slide 7 Selection Sort min_index = 0; for (j = 1; j < n; j++) if (List[j] <= List[min_index]) min_index = j; swap(List[0], List[min_index]); http://www.cs.brockport.edu/cs/java/apps/sorters/selsort.html One pass to find the smallest number; Swap with List[0].

8 COMP104 Lecture 25 / Slide 8 Selection Sort for (i = 0; i < (n - 1); i++) { min_index = i; for (j = i + 1; j < n; j++) if (List[j] <= List[min_index]) min_index = j; swap(List[i], List[min_index]); } http://www.cs.brockport.edu/cs/java/apps/sorters/selsort.html N passes to find the current smallest number; swap with List[i].

9 COMP104 Lecture 25 / Slide 9 Selection Sort #include using namespace std; void select(int data[], int size); void main(){ int A[9] = { 10, 7, 9, 1, 9, 17, 30, 5, 6 }; select(A, 9); cout << "The sorted array: "; for(int i=0; i<9; i++) cout << A[i] << " "; cout << endl; }

10 COMP104 Lecture 25 / Slide 10 Selection Sort // Sort array of integers in ascending order void select(int data[], // in/output: array int size){ // input: array size int temp; // for swap int min_index; // index of min value for(int leftmost=0; leftmost<size; leftmost++){ // find the smallest item min_index = leftmost; for(int i=leftmost; i<size; i++) if (data[i] < data[min_index]) min_index = i; // swap with last item if necessary if(data[min_index] < data[leftmost]){ temp = data[min_index]; /// swap data[min_index] = data[leftmost]; data[leftmost] = temp; }

11 COMP104 Lecture 25 / Slide 11 Bubble Sort l Bubble sort examines the array from start to finish, comparing elements as it goes. l Any time it finds a larger element before a smaller element, it swaps the two. l In this way, the larger elements are passed towards the end. l The largest element of the array therefore "bubbles" to the end of the array. l It then repeats the process for the unsorted portion of the array until the whole array is sorted.

12 COMP104 Lecture 25 / Slide 12 Bubble Sort l Bubble sort works on the same general principle as shaking a soft drink bottle. l Right after shaking, the contents are a mixture of bubbles and soft drink, distributed randomly. l Because bubbles are lighter than the soft drink, they rise to the surface, displacing the soft drink downwards. l This is how bubble sort got its name, because the smaller elements "float" to the top, while the larger elements "sink" to the bottom.

13 COMP104 Lecture 25 / Slide 13 Bubble Sort l Algorithm n Define the unsorted portion of the array to be the entire array n While the unsorted portion of the array has more than one element: 1. For every element in the unsorted portion, swap with the next neighbor if it is larger than the neighbor 2. Reduce the unprocessed portion of the array by 1

14 COMP104 Lecture 25 / Slide 14 Bubble Sort for (j=0; j<n-1; j++) { if (List[j] > List[j+1]) swap(List[j], List[j+1]); } http://www.cs.brockport.edu/cs/java/apps/sorters/bubblesort.html One pass to “sink” the largest number to the bottom of the list List[0], List[1], … List[n-2], List[n-1]

15 COMP104 Lecture 25 / Slide 15 Bubble Sort for (i=n; i>=1; i--) { for (j=0; j<i-1; j++) { if (List[j] > List[j+1]) swap(List[j], List[j+1]); } http://www.cs.brockport.edu/cs/java/apps/sorters/bubblesort.html N passes to “sink” the currently largest number function (subprogram)

16 COMP104 Lecture 25 / Slide 16 Bubble Sort // Sort an array of integers in ascending order void bubble(int data[], // in/output: array int size){ // input: array size int temp; // for swap for(int rightmost=size-1; rightmost>0; rightmost--){ for(int i=0; i<rightmost; i++) { if(data[i] > data[i+1] ) { // swap with next item if greater temp = data[i]; data[i] = data[i+1]; data[i+1] = temp; }


Download ppt "Programming Sorting Arrays. COMP104 Lecture 25 / Slide 2 Sorting l To arrange a set of items in sequence. l It was estimated that 25~50% of all computing."

Similar presentations


Ads by Google