1 / 8

Intro to Sorting

Intro to Sorting. Sorting "Ideal" Computer Science Topic Theory and Practice meet Efficient Sorting Saves Money First look at some simple (quick and dirty?) algorithms Selection Sort Find smallest; swap with element [0]

ban
Download Presentation

Intro to Sorting

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. Intro to Sorting • Sorting • "Ideal" Computer Science Topic • Theory and Practice meet • Efficient Sorting Saves Money • First look at some simple (quick and dirty?) algorithms • Selection Sort • Find smallest; swap with element [0] • Consider rest of list [1], [2], ...; find smallest,swap with element [1] • Continue process until you get to end

  2. Selection Sorting Example • N items in an array named Data [ 2 | 4 | 7 | 3 | 1 | 8 | 5 ] • Find smallest of elements 1 thru N of Data • Interchange this with 1st element of array Data [ 1 | 4 | 7 | 3 | 2 | 8 | 5 ] • Find smallest of elements 2 thru N of Data • Interchange this with 2nd element of array Data [ 1 | 2 | 7 | 3 | 4 | 8 | 5 ] • ... • Find smallest of elements K thru N of Data • Interchange this with Kth element of array Data [ 1 | 2 | 3 | 7 | 4 | 8 | 5 ] [ 1 | 2 | 3 | 4 | 7 | 8 | 5 ] [ 1 | 2 | 3 | 4 | 5 | 8 | 7 ] • Done when K = N [ 1 | 2 | 3 | 4 | 5 | 7 | 8 ]

  3. Selecton Sort Code publicint locMin(int[] nums, int start){ int loc = start; for (int k = start + 1; k < nums.length; k++){ if (nums[k] < nums[loc]) loc = k; } return loc; } publicvoid SelectSort(int[]nums){ for (int k = 0; k < nums.length; k++) { int minloc = locMin(nums, k); int temp = nums[k]; nums[k] = nums[minloc]; nums[minloc] = temp; } }

  4. Selection Sorting • Think about Selection Sort • Loop Invariant • What can we say about our partially sorted list that is true each time around? • Complexity • What is the big Oh? • Develop relationship for Selection Sort

  5. More Sorting • Other Simple Sorts • Simple results in O(N2) • Bubble Sort? (XXX) • Worst of the O(N2) • Insertion Sort • Develop Algorithm • (steps often used when updating a sorted list, one item at a time) • More complicated to program than selection sort • But, has some very nice properties

  6. [ 5| 4 | 6 | 9 | 3 | 8 | 1 ] 4 [ 5| 4 | 6 | 9 | 3 | 8 | 1 ] 4 [ 5| 5 | 6 | 9 | 3 | 8 | 1 ] 4 [ 4| 5 | 6 | 9 | 3 | 8 | 1 ] [ 4| 5 | 6 | 9 | 3 | 8 | 1 ] 6 [ 4| 5 | 6 | 9 | 3 | 8 | 1 ] 6 [ 4| 5 | 6 | 9 | 3 | 8 | 1 ] [ 4| 5 | 6 | 9 | 3 | 8 | 1 ] 9 [ 4| 5 | 6 | 9 | 3 | 8 | 1 ] 9 [ 4| 5 | 6 | 9 | 3 | 8 | 1 ] [ 4| 5 | 6 | 9 | 3 | 8 | 1 ] 3 [ 4| 5 | 6 | 9 | 3 | 8 | 1 ] 3 [ 4| 5 | 6 | 9 | 9 | 8 | 1 ] 3 [ 4| 5 | 6 | 6 | 9 | 8 | 1 ] 3 [ 4| 5 | 5 | 6 | 9 | 8 | 1 ] 3 [ 4| 4 | 5 | 6 | 9 | 8 | 1 ] 3 [ 3 | 4 | 5 | 6 | 9 | 8 | 1 ] [ 3| 4 | 5 | 6 | 9 | 8 | 1 ] 8 [ 3| 4 | 5 | 6 | 9 | 8 | 1 ] 8 [ 3| 4 | 5 | 6 | 9 | 9 | 1 ] 8 [ 3| 4 | 5 | 6 | 8 | 9 | 1 ] [ 3| 4 | 5 | 6 | 8 | 9 | 1 ] 1 [ 3| 4 | 5 | 6 | 8 | 9 | 1 ] 1 [ 3| 4 | 5 | 6 | 8 | 9 | 9 ] 1 [ 3| 4 | 5 | 6 | 8 | 8 | 9 ] 1 [ 3| 4 | 5 | 6 | 6 | 8 | 9 ] 1 [ 3| 4 | 5 | 5 | 6 | 8 | 9 ] 1 [ 3| 4 | 4 | 5 | 6 | 8 | 9 ] 1 [ 3| 3 | 4 | 5 | 6 | 8 | 9 ] 1 [ 1 | 3 | 4 | 5 | 6 | 8 | 9 ] Example

  7. Insertion Sort Code publicvoid insertSort(int[]nums){ int j, k, temp; for (k = 1; k < nums.length; k++) { temp = nums[k]; for (j = k; j > 0; j--){ // decrement! if (temp<nums[j-1]) nums[j] = nums[j-1]; else break; } nums[j] = temp; } }

  8. Insertion Sort • Loop Invariant? • Complexity? • For almost sorted? • Is stable! • What does that mean?

More Related