1 / 16

Programming

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

dbeck
Download Presentation

Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Sorting Arrays

  2. 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

  3. 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

  4. Some Sorting Methods • Selection sort • Bubble sort • Shell sort • Quick sort

  5. 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.

  6. 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

  7. 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]);

  8. 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]); }

  9. 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; }

  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. 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.

  12. 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.

  13. 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

  14. 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]); }

  15. 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]); } }

  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; } } } }

More Related