sorting. why sort? put matching elements together –uniqueness testing –deleting duplicates...
TRANSCRIPT
![Page 1: Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e7d5503460f94b804b5/html5/thumbnails/1.jpg)
Sorting
![Page 2: Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e7d5503460f94b804b5/html5/thumbnails/2.jpg)
Why Sort?
• Put matching elements together– Uniqueness testing– Deleting duplicates– Frequency Counting– Set operations
• Prioritize Elements• Reconstruct original order• Efficient searching (binary search, pairs)
![Page 3: Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e7d5503460f94b804b5/html5/thumbnails/3.jpg)
Sorting Algorithms (pp. 80-82)
• Insertion Sort– Least code, least data movement (inversions)
• Selection Sort– Conceptually simple, fewest swaps
• Quick Sort– Fastest– Needs careful implementation– Partition algorithm useful on its own
![Page 4: Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e7d5503460f94b804b5/html5/thumbnails/4.jpg)
Comparison Functions
• Use with generalized sorting algorithms• Stable Sort
– Sort criteria separately (most important last)
• Unstable Sort– Previously sorted not necessarily still in order– One comparison function handles all criteria
![Page 5: Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e7d5503460f94b804b5/html5/thumbnails/5.jpg)
Sorting in C++ (STL)
• Library functions sort and stable_sort– Iterator bg (beginning of list)– Iterator end (end of list)– Optional BinaryPredicate (default <=)
• Function that takes two items and returns true if they are in order
![Page 6: Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e7d5503460f94b804b5/html5/thumbnails/6.jpg)
Sorting in Java
• In java.util.Arrays– Array of Objects (Object[] a)– Optional Comparator (default <=)
![Page 7: Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e7d5503460f94b804b5/html5/thumbnails/7.jpg)
Search
• Sequential Search
• Binary Search– Tricky to get right under pressure
• Consider stopping condition
– Library functions• C++ STL: bsearch• Java: binarySearch• (see pp. 84-85)
![Page 8: Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e7d5503460f94b804b5/html5/thumbnails/8.jpg)
Generalizing Search
• Sequential– Foreach element
• If ( correct(element) ) process and break
• Binary– While (remaining list not empty)
• If (too-high (middle-of-list))– Cut off bottom half
• Else if (too-low (middle-of-list))– Cut off top half
• Else process and break
![Page 9: Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e7d5503460f94b804b5/html5/thumbnails/9.jpg)
Notes on Binary Search
• Generalized functions “too-high” and “too-low” might evaluate criteria other than a value– E.g. outcome of function with a given parameter
• If you can estimate a location better than the middle, use it! (phone book search)
![Page 10: Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e7d5503460f94b804b5/html5/thumbnails/10.jpg)