sorting cs221 – 3/2/09. recursion recap use recursion to improve code clarity make sure the...
Post on 19-Dec-2015
222 views
TRANSCRIPT
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
Intro to Sorting
• One of the fundamental problems of Computer Science
• How do we get from this:
• To this:
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
The Basics
• To sort we need:– A set of items to sort– A way of comparing items– An algorithm to produce the sort
How do you Compare?
• Primitive types can be compared automatically – int, char, float, etc
• Any Java object with comparable interface– String, Date, Integer, etc
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
ComparablePublic class Customer implements Comparable<Customer>{
…public int compareTo(Customer customer) {
if (this.getSocialSecurity() < customer.getSocialSecurity()){return -1;}else if (this.getSocialSecurity() == customer.getSocialSecurity()){return 0;}else{return 1;}
}…
}
ComparatorPublic class CompareCustomer implements Comparator<Customer>{
…public int compare(Customer left, Customer right) {
if (left.getSocialSecurity() < right.getSocialSecurity()){
return -1;}else if (left.getSocialSecurity() == right.getSocialSecurity()){
return 0;}else{
return 1;}
}…
}
Sort Algorithms
• Selection Sort• Bubble Sort• Insertion Sort• Shell Sort• Merge Sort• Heap Sort• Quicksort
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?
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)
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)
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?
Data Structure Considerations
• Almost all sort analysis assumes an array data structure
• What happens if you use a linked list instead?
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
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
Selection Sort
Find the smallest item in the listSwap with the first item in the listRepeat for remainder of the list
Selection Sort PseudoCodeFor 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
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?