More Bi Go
More Bi Go
December 1, 2009
Sorting Algorithms
Sorting: putting a list of items in-order
Usually: numerical or lexicographical
Remember:
Length of a list (which could be stored in an Array, ArrayList, etc) is usually represented by the lowercase letter n
What if I know the number of elements?
Still do your analysis in terms of n.
Why Do We Care?
What is Analyzing Algorithms (and BigO notation) good for?
Comparison of algorithms Advertising a new algorithm Making decisions on which algorithm to use Determining the scalability of an algorithm
Common Types
O(1) O(log n) O(n) Constant Logarithmic Linear
O(n2)
O(n3)
Quadratic
Cubic
Random Thought
It is important when reporting the Big-O to use proper Big-O Notation If the Big-O of an algorithm is linear,
Then you should report the Big-O as O(n) You must include the letter O and the () Remember: n is not the same as O(n)
Why?
It is the notation that has always been used There are other symbols that are used to mean slightly different thingsbut we will not get into that in this course Plus, I am sure you want to get it right on your final exam
Stable Sort
A stable sort is a sort algorithm that maintains the relative order of data items in the list In other words, it there are multiple data items in the list that have the same value, then the sort completes these data items are in the same order as the original
8, 1, 5, 2, 3, 3, 4, 3, 6, 7 1, 2, 3, 3, 3, 4, 5, 6, 7, 8
Comparison Sorts
Sort algorithms that mainly use comparison operators (e.g., less than/greater than) Common Comparison Sorts:
Insertion Sort Selection Sort Merge Sort Bubble Sort Quick Sort
Bubble Sort
Another simple comparison sorting algorithm The basics:
Repeatedly walkthrough a list of data that needs to be sorted compare adjacent data items and swapping (if needed) Algorithm stops: when a walkthrough of the list does not require any swaps
Bubble Sort
Example:
Start: 5, 1, 3, 6, 4
1, 5, 3, 6, 4 1, 3, 5, 6, 4 1, 3, 5, 6, 4 (no change) 1, 3, 5, 4, 6 1, 3, 5, 4, 6 (no change) 1, 3, 5, 4, 6 (no change) 1, 3, 4, 5, 6 1, 3, 4, 5, 6 (no change)
It will check the array 1 last time and there will be no swaps, so the algorithm is done
The Do While controls the number of times the For Loop happens. Therefore, worst case, amount of work for the algorithm: n * (n-1)
n * (n-1) = n2 - n
Therefore, O(n2)
This algorithm would also work well for data that is close to being in the proper order
Quick Sort
A comparison sorting algorithm using a divide and conquer technique The basics:
Pick an element to be the pivot Partition Operation
Create sub-lists for above and below pivot Place pivot between sub-lists
QuickSort(A) {
Select pivot point (I used: pivot = A.Size-1) for(int x=0; x<A.Size-1; x++) { if(A.get(x) < pivot) SubLess.add(A.get(x)) else SubMore.add(A.get(x)) }//end of for loop Sorted = Concatenate(QuickSort(SubLess, QuickSort(SubMore))
}//end of QuickSort
O(n2)
Memory Complexity
Studying how much memory is needed beyond the list of items to complete the search
Most common sorts have O(1) memory complexity (space for swaps, etc)