160 likes | 178 Views
Learn about sorting applications, methods like selection sort and bubble sort, and how to implement these algorithms in code. Understand how sorting enhances array efficiency for various computational tasks.
E N D
Programming Sorting Arrays
Sorting • To arrange a set of items in sequence. • It was estimated that 25~50% of all computing power is being expended in sorting activities. • Possible reasons: • Many applications require sorting • Many applications perform sorting when they don't have to • Many applications use inefficient sorting algorithms
Sorting Applications • List of student ID names and numbers in a table (sorted by name) • List of scores before letter grade • assignment (sorted by students' scores) • List of horses after a race (sorted by the finishing times) • To prepare an originally unsorted array for ordered binary searching
Some Sorting Methods • Selection sort • Bubble sort • Shell sort • Quick sort
Selection Sort • Selection sort performs sorting by • repeatedly putting the smallest element in the first position of the unsorted portion, • until the whole array is sorted. • It is similar to the way that many people do their sorting.
Selection Sort • 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
Selection Sort http://www.cs.brockport.edu/cs/java/apps/sorters/selsort.html One pass to find the smallest number; Swap with List[0]. min_index = 0; for (j = 1; j < n; j++) if (List[j] <= List[min_index]) min_index = j; swap(List[0], List[min_index]);
Selection Sort http://www.cs.brockport.edu/cs/java/apps/sorters/selsort.html N passes to find the current smallest number; swap with List[i]. 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]); }
Selection Sort #include <iostream> 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; }
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; } } }
Bubble Sort • Bubble sort examines the array from start to finish, comparing elements as it goes. • Any time it finds a larger element before a smaller element, it swaps the two. • In this way, the larger elements are passed towards the end. • The largest element of the array therefore "bubbles" to the end of the array. • It then repeats the process for the unsorted portion of the array until the whole array is sorted.
Bubble Sort • Bubble sort works on the same general principle as shaking a soft drink bottle. • Right after shaking, the contents are a mixture of bubbles and soft drink, distributed randomly. • Because bubbles are lighter than the soft drink, they rise to the surface, displacing the soft drink downwards. • This is how bubble sort got its name, because the smaller elements "float" to the top, while the larger elements "sink" to the bottom.
Bubble Sort • Algorithm • Define the unsorted portion of the array to be the entire array • 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
Bubble Sort http://www.cs.brockport.edu/cs/java/apps/sorters/bubblesort.html List[0], List[1], … List[n-2], List[n-1] One pass to “sink” the largest number to the bottom of the list for (j=0; j<n-1; j++) { if (List[j] > List[j+1]) swap(List[j], List[j+1]); }
function (subprogram) Bubble Sort http://www.cs.brockport.edu/cs/java/apps/sorters/bubblesort.html N passes to “sink” the currently largest number 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]); } }
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; } } } }