Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates...

10
Sorting

Transcript of Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates...

Page 1: Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct.

Sorting

Page 2: Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct.

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.

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.

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.

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.

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.

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.

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.

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.