1 / 10

Lecture 08 Sorting

Lecture 08 Sorting. Sorts. Many programs will execute more efficiently if the data they process is sorted before processing begins. We first looked at a linear search it doesn’t care whether the data is sorted or not

gudrun
Download Presentation

Lecture 08 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. Lecture 08 Sorting

  2. Sorts • Many programs will execute more efficiently if the data they process is sorted before processing begins. • We first looked at a linear search • it doesn’t care whether the data is sorted or not • the algorithm starts at the first element in the vector, and looks at every element in the vector, in sequence, until it finds what it is looking for, or comes to the end of the vector • With small data sets this algorithm performs acceptably • If the data sets are of significant size, than performance can become unacceptable

  3. Sorts • If we know that a data set is sorted in some order (lowest to greatest, largest to smallest, highest priority to lowest priority), then we can write searches to take advantage of this fact. • If you have misplaced your calculator in the library in the afternoon, you do not have to retrace your steps from when you arrived on campus in the morning to find it • You start your search in the library • When looking up a phone number in the phone book, you do not start at page 1 and scan each page in sequence, until you find the name you are looking for

  4. Sorting Comparison • There are all kinds of sorts • For our purposes a sort is a rearrangement of data into either ascending or descending order Fast Sorts versus Slow Sorts O (N log2 N) O (N2) slow sorts are easy to code and sufficient when the amount of data is small

  5. Bubble Sort - N2 Sort • Strategy • ‘bubble’ the smallest item to the left (slot 0) • ‘bubble’ the next smallest item to slot 1 • ‘bubble’ the third smallest item to slot 2 • algorithm (for one pass) walk through the Vector if this item (in spot j) is smaller than the item in spot j-1 swap item in spot j with the item in spot j-1 • code for (int j = count-1; j >= 0; j--) { if (nums[j] < nums[j-1]) { int temp = nums[j]; nums[j] = nums[j-1]; nums[j-1] = temp; } }

  6. Bubble Sort (con’t) • This code for one pass finds the smallest element in the vector and puts it into slot 0 • Now wrap this code in a loop that will find succeeding smaller elements and put them into the proper position in the Vector // outer for loop controls the spot that gets the appropriate smallest value for (int I=0; I<size-1;I++) for (int j = count-1; j >= 0; j--) { if (nums[j] < nums[j-1]) { int temp = nums[j]; nums[j] = nums[j-1]; nums[j-1] = temp; } }

  7. Selection Sort • We have noticed that the most “expensive” part of the bubble sort is swapping (three lines of code to execute) • A selection sort reduces the number of swaps until the end of each pass, when we know what the smallest remaining value is, and placing it appropriately • Strategy • find the smallest item, and swap it with slot 0 • find the next smallest item and swap it with slot 1 • find the third smallest item and swap it with slot 2 • algorithm (for one pass) walk through the Vector the first item is labeled the smallest Every other element is compared to the smallest if it is smaller, than it is labeled the smallest At the end of the walkthrough, the first is swapped with the smallest

  8. Selection Sort (con’t) • The algorithm for one pass finds the smallest element in the vector and puts it into slot 0 • Now wrap this code in a loop that will find succeeding smaller elements and put them into the proper position in the Vector // outer for loop controls the spot that gets the appropriate smallest // value (same as bubble sort) for (int left = 0; left < count - 1; left++) { // last one will be correct int smallest = left; // we will keep the index to the smallest for (int j = left + 1; j < count; j++) if (nums[j] < nums[smallest] { smallest = j; } if (smallest != left) // no sense swapping if left is smallest { int temp = nums[smallest]; nums[smallest] = nums[left]; nums[left] = temp; } }

  9. Use FindSmallest() routine with SelectionSort() • We have spent a lot of time looking at FindSmallest() routines… you should know how to do that • Incorporate that knowledge into selection sort // outer for loop controls the spot that gets the appropriate smallest // value (same as bubble sort) for (int left = 0; left < count - 1; left++) { int smallest = findSmallest(nums, count, left); // use what we already know // pass the starting point of the // remainder of the vector to look at if (smallest != left ) { int temp = nums[smallest]; nums[smallest] = nums[left]; nums[left] = temp; } }

  10. findSmallest(const int nums[], int count, int left); int findSmallest(const int nums[], int count, int left) { int smallest = left; // start with first index as the smallest for (int j = left + 1; j < count; j++) if (nums[j] < nums[smallest] { smallest = j; } return smallest; }

More Related