Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Sorting Arrays. COMP102 Prog. Fundamentals. Sorting I/ Slide 2 Sorting l To arrange a set of items in sequence. l It is estimated that 25~50%

Similar presentations


Presentation on theme: "Programming Sorting Arrays. COMP102 Prog. Fundamentals. Sorting I/ Slide 2 Sorting l To arrange a set of items in sequence. l It is estimated that 25~50%"— Presentation transcript:

1 Programming Sorting Arrays

2 COMP102 Prog. Fundamentals. Sorting I/ Slide 2 Sorting l To arrange a set of items in sequence. l It is estimated that 25~50% of all computing power is used for 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 COMP102 Prog. Fundamentals. Sorting I/ Slide 3 Sorting Applications l To prepare a list of student ID, names, and scores in a table (sorted by ID or name) for easy checking. l To prepare a list of scores before letter grade assignment. l To produce a list of horses after a race (sorted by the finishing times) for payoff calculation. l To prepare an originally unsorted array for ordered binary searching.

4 COMP102 Prog. Fundamentals. Sorting I/ Slide 4 Some Sorting Methods l Selection sort l Bubble sort l Shell sort (a simple but faster sorting method than above; see p.331 of Numerical Recipes in C, 2nd ed., by William H. Press et al, Cambridge University Press, 1992) l Quick sort (a very efficient sorting method for most applications; p.332-336, ibid.)

5 COMP102 Prog. Fundamentals. Sorting I/ Slide 5 Ex. 1A: Selection Sort l Selection sort performs sorting by repeatedly putting the largest element in the unsorted portion of the array to the end of this unsorted portion until the whole array is sorted. l It is similar to the way that many people do their sorting.

6 COMP102 Prog. Fundamentals. Sorting I/ Slide 6 Ex. 1A: Selection Sort l Algorithm 1. Define the entire array as the unsorted portion of the array 2. While the unsorted portion of the array has more than one element:  Find its largest element.  Swap with last element (assuming their values are different).  Reduce the size of the unsorted portion of the array by 1.

7 Before sorting14210513177 After pass 114210513717 After pass 272105131417 After pass 372351101417 After pass 412357101417

8 // Sort array of integers in ascending order void select(int data[], // in/output: array int size){ // input: array size int temp; // for swap int max_index; // index of max value for (int rightmost=size-1; rightmost>0; rightmost--){ //find the largest item in the unsorted portion //rightmost is the end point of the unsorted part of array max_index = 0; //points the largest element for ( int current=1; current<=rightmost; current++) if (data[current] > data[max_index]) max_index = current; //swap the largest item with last item if necessary if (data[max_index] > data[rightmost]){ temp = data[max_index]; // swap data[max_index] = data[rightmost]; data[rightmost] = temp; } }}

9 const int array_size = 8; int main() { int list[array_size] = {14, 2, 10, 5, 1, 3, 17, 7}; int index, ans; cout << "Before sorting: "; for (index = 0; index<array_size; index++) cout << list[index] <<" "; cout << endl; cout << "Enter sorting method 1(select), 2(bubble):"; cin >> ans; if (ans == 1) select(list, array_size); else bubble(list, array_size); cout << endl << "After sorting: "; for (index = 0; index<array_size; index++) cout << list[index] <<" "; cout << endl; return 0; }

10 COMP102 Prog. Fundamentals. Sorting I/ Slide 10 Ex. 1B: 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 Then it repeats the process for the unsorted portion of the array until the whole array is sorted.

11 COMP102 Prog. Fundamentals. Sorting I/ Slide 11 Ex. 1A: 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.

12 COMP102 Prog. Fundamentals. Sorting I/ Slide 12 Ex. 1B: Bubble Sort l Algorithm n Define the entire array as the unsorted portion of the 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 size of the unsorted portion of the array by 1.

13 Before sorting14210513177 outer=7, inner=021410513177 outer=7, inner=121014513177 outer=7, inner=221051413177 outer=7, inner=321051143177 outer=7, inner=421051314177 outer=7, inner=621051314717 outer=6, inner=125101314717 outer=6, inner=225110314717 outer=6, inner=325131014717 outer=6, inner=525131071417 outer=5, inner=121531071417 outer=5, inner=221351071417 outer=5, inner=421357101417 outer=4, inner=012357101417 --- outer=1, inner=012357101417

14 //Example1b: Bobble 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 outer=size-1; outer > 0; outer--){ for (int inner=0; inner < outer; inner++) { // traverse the nested loops if ( data[inner] > data[inner+1] ) { // swap current element with next // if the current element is greater temp = data[inner]; data[inner] = data[inner+1]; data[inner+1] = temp; } } // inner for loop } // outer for loop }

15 const int array_size = 12, string_size = 11; int main() { char month[array_size][string_size] = {"January", "February","March","April","May","June","July","August", "September", "October", "November", "December"}; int index, ans; cout << "Enter sorting method 1(select), 2(bubble): "; cin >> ans; if (ans == 1) select(month, array_size); else bubble(month, array_size); cout << endl << "After sorting: "; for (index = 0; index<array_size; index++) cout << month[index] <<" "; cout << endl; return 0; }

16 012345678910 month[0]January\0 month[1]February\0 month[2]March\0 month[3]April\0 month[4]May\0 month[5]June\0 month[6]July\0 month[7]August\0 month[8]September\0 month[9]Octcber\0 month[10]November\0 month[11]December\0

17 // Sort array of strings in ascending order void select(char data[][string_size], // in/output: array int size){ // input: array size char temp[string_size]; // for swap int max_index; // index of max value for (int rightmost=size-1; rightmost>0; rightmost--){ // find the largest item max_index = 0; for(int current=1; current<=rightmost; current++) if (strcmp(data[current], data[max_index]) >0) max_index = current; // swap with last item if necessary if(strcmp(data[max_index], data[rightmost])>0){ strcpy(temp,data[max_index]); // swap strcpy(data[max_index],data[rightmost]); strcpy(data[rightmost], temp); for (int index=0; index< size; index++) cout << data[index] << " "; cout<<endl; }

18 // Sort an array of strings in ascending order void bubble(char data[][string_size], // in/output:array int size){ // input: array size char temp[string_size]; // for swap for(int outer=size-1 ; outer>0; outer--){ for (int inner=0; inner < outer; inner++) { // traverse the nested loops if ( strcmp(data[inner], data[inner+1])>0 ) { // swap current element with next // if the current element is greater strcpy(temp, data[inner]); strcpy(data[inner], data[inner+1]); strcpy(data[inner+1], temp); for (int index=0; index< size; index++) cout << data[index] << " "; cout<<endl; } } // inner for loop } // outer for loop }


Download ppt "Programming Sorting Arrays. COMP102 Prog. Fundamentals. Sorting I/ Slide 2 Sorting l To arrange a set of items in sequence. l It is estimated that 25~50%"

Similar presentations


Ads by Google