1 / 30

Sorting Algorithms Explained: Basics, Efficiency, Implementation

Understand sorting algorithms basics, time & space complexity, Java implementations, comparison methods, and data structure considerations.

garyparker
Download Presentation

Sorting Algorithms Explained: Basics, Efficiency, Implementation

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. Sorting CS221 – 3/2/09

  2. Recursion Recap • Use recursion to improve code clarity • Make sure the performance trade-off is worth it • Every recursive method must have a base case to avoid infinite recursion • Every recursive method call must make progress toward an eventual solution • Sometime a recursive method will do more work as the call stack unwinds

  3. Intro to Sorting • One of the fundamental problems of Computer Science • How do we get from this: • To this:

  4. Why does it matter? • Sorting is required by many other algorithms • For instance sorting makes searching more efficient • The sorting techniques themselves are interesting • Studying sorting helps you understand the tradeoffs and complexities of many other CS problems • You may be asked to implement a sorting algorithm in an interview question

  5. The Basics • To sort we need: • A set of items to sort • A way of comparing items • An algorithm to produce the sort

  6. What can you sort? • Array • Collection • Linked List? • Queue? • Stack?

  7. How do you Compare? • Primitive types can be compared automatically • int, char, float, etc • Any Java object with comparable interface • String, Date, Integer, etc

  8. How do you Compare? • You can implement the comparable interface on custom objects • CompareTo method • You can implement the comparator interface and pass to a sort algorithm • Compare method • Equals method

  9. Comparable Public class Customer implements Comparable<Customer> { … public intcompareTo(Customer customer) { if (this.getSocialSecurity() <customer.getSocialSecurity()) { return -1; } else if (this.getSocialSecurity() ==customer.getSocialSecurity()) { return 0; } else { return 1; } } … }

  10. Comparator Public class CompareCustomer implements Comparator<Customer> { … public intcompare(Customer left, Customer right) { if (left.getSocialSecurity() < right.getSocialSecurity()) { return -1; } else if (left.getSocialSecurity() == right.getSocialSecurity()) { return 0; } else { return 1; } } … }

  11. What’s the difference • When would you use Comparable? • When would you use Comparator?

  12. Sorting Java Objects Java arrays implement sort using comparable:

  13. Sorting Java Objects And using comparator:

  14. Sorting Java Objects Collections implement sort as well:

  15. Example

  16. Sort Algorithms • Selection Sort • Bubble Sort • Insertion Sort • Shell Sort • Merge Sort • Heap Sort • Quicksort

  17. How Would you Choose? • Think about time complexity • Think about space complexity • How does it relate to the size of your dataset? • How does it relate to the type of data structure you are using?

  18. Time Complexity • For each algorithm we will look at best case and worst case • Analyze the number of comparisons • Compare one item in the list to another • Analyze the number of exchanges • Move an item to progress towards the sort • Good: O(n log n) • Bad: O(n^2)

  19. Space Complexity • Space complexity depends on: • Recursive or iterative? • Is the data exchanged in-place or is temporary memory used? • Good: O(1) • Not as good: O(n)

  20. Size of Dataset • In general its best to use the sort algorithm with best time and space complexity • However – if you know you dataset will be small, you can use a simpler sort implementation with higher time complexity • For very large datasets, good space complexity will start to outweigh good time complexity • Why?

  21. Data Structure Considerations • Almost all sort analysis assumes an array data structure • What happens if you use a linked list instead?

  22. Linked List Considerations • Access to a specific element is harder • Easier to access elements directly before and after • Comparisons are the same • Exchanges (add/remove) require pointer changes instead of array modifications • Keep this in mind as we talk through each sort algorithm

  23. How Would Java Choose? • Java primitive type arrays use Quicksort • Fast and space efficient • Java object arrays use Mergesort • Fast but less space efficient • Stable • Collections use Mergesort • Copies the linked list to an array • Sorts the array • Copies the sorted array back to the list • Insertionsort is used if there are less than 7 elements

  24. Selection Sort • If you were to sort by hand, how would you do it?

  25. Selection Sort

  26. Selection Sort

  27. Selection Sort Find the smallest item in the list Swap with the first item in the list Repeat for remainder of the list

  28. Selection Sort PseudoCode For baseIndex from 0 to array.length – 1 smallestIndex = baseIndex For compareIndex from baseIndex +1 to array.length - 1 if array[compareIndex] < array[smallestIndex] smallestIndex = compareIndex temp = array[baseIndex] array[baseIndex] = array[smallestIndex] array[smallestIndex] = temp

  29. Selection Sort Complexity • What is the time complexity? • How many comparisons? • How many exchanges? • What is the space complexity? • Is the data exchanged in-place? • Does the algorithm require auxiliary storage?

  30. Can you do better? • How would you improve selection sort?

More Related