11 Sorting
11 Sorting
COMP103 2006/T3 2
Log2(n ) :
The number of times you can divide a set of n things in half.
lg(1000) =10, lg(1,000,000) = 20, lg(1,000,000,000) =30
Every time you double n, you add one step to the cost!
Solve Solve
Solution
COMP103 2006/T3 3
ArraySet with Binary
Search
ArraySet: unordered
• All cost in the searching: O(n)
• contains: O(n )
• add: O(n )
• remove: O(n )
COMP103 2006/T3 4
Making SortedArraySet fast
• If you have to call add() and/or remove() many items,
then SortedArraySet is no better than ArraySet (we’ll see
how to do better later in the course)
• Both O(n )
• Either pay to search
• Or pay to keep it in order
• If you only have to construct the set once, and then many
calls to contains(),
then SortedArraySet is much better than ArraySet.
• SortedArraySet contains() is O(log(n )
COMP103 2006/T3 5
Alternative Constuctor
public SortedArraySet(Collection<E> c){
// Make space
count=c.size();
data = (E[]) new Object[count];
COMP103 2006/T3 6
Introduction to Sorting
• The General Sorting Problem.
• We are given:
• A list.
• Items are all of the same type.
• A comparison function.
• That, given two list items, determine which should
come first.
• And using this function is the only way we can make
such a determination.
• We return:
• A sorted list with the same items as the original list.
COMP103 2006/T3 7
Ways of Sorting:
• Selecting sorts (Selection):
• Find the next largest/smallest item and put in place
• Builds the correct list in order
• Radix Sorts
• Look at the item and work out where it should go.
• Only works on some kinds of values.
COMP103 2006/T3 8
Selecting Sorts
COMP103 2006/T3 9
Inserting Sorts
COMP103 2006/T3 10
Compare and Swap Sorts
COMP103 2006/T3 11
Other Sorts
COMP103 2006/T3 12
Analysis of Sorting
• We can analyze sorting algorithms according to five criteria:
• Efficiency
• What is the (worst-case) order of the algorithm?
• Is the algorithm much faster on average than its worst-case performance?
• Requirements on Data
• Does the algorithm need random-access data?
• What operations does the algorithm require the data to have?
• Of course, we always need “compare”. What else?
• Space Usage
• Can the algorithm sort in-place?
• In-place means the algorithm does not copy the data to a separate buffer.
• How much additional storage is used?
• Every algorithm uses at least a little.
• Stability
• Is the algorithm stable?
• A sorting algorithm is stable if it does not reverse the order of equivalent items.
• Performance on Nearly Sorted Data
• Is the algorithm faster when given sorted (or nearly sorted) data?
• All items close to where they should be, or a limited number of items out of order.
COMP103 2006/T3 13
Sorting Algorithms
• There is no known sorting algorithm that has all
the properties we would like one to have.
• We will examine a number of sorting algorithms.
Generally, these fall into two categories:
• Quadratic [O(n2)] Algorithms
• Bubble Sort
• Selection Sort
• Insertion Sort
• Quicksort
• Log-Linear [O(n log n)] Algorithms
• Merge Sort
• Heap Sort
COMP103 2006/T3 14
What to sort?
• Could sort Lists
⇒ general and flexible
• but efficiency depends on how the List is implemented
• Comparing items:
• require items to be comparable
• provide comparable
• handle both.
COMP103 2006/T3 15
Selection Sort
public void selectionSort(E[ ] data, int size, Comparator<E>
comp){
// for each position, from 0 up, find the next smallest item
// and swap it into place.
for (int i=0; i<size-1; i++){
int minIndex = i;
for (int j=i+1; j<size; j++){
if (comp.compare(data[j], data[minIndex]) < 0)
minIndex=j;
}
if(i != minIndex) swap(data, i, minIndex);
}
} Consumes wild when bear often humans a
COMP103 2006/T3 16
Selection Sort: Analysis
• Efficiency
• Selection Sort is O(n2).
• Selection Sort also has an average-case time of O(n2).
• Requirements on Data
• Selection Sort does not require random-access data.
• Operations needed: swap.
• Space Usage
• Selection Sort can be done in-place.
• It does not use significant additional storage.
• Stability
• Selection Sort is stable.
• As long as we are careful in our implementation!
• Performance on Nearly Sorted Data
• Not significantly faster on nearly sorted data: O(n2).
COMP103 2006/T3 17
Insertion Sort
public void insertionSort(E[] data, int size, Comparator<E>
comp){
// for each item, from 0, insert into place in the sorted region
(0..i-1)
for (int i=1; i<size; i++){
E item = data[i];
int place = i;
while (place > 0 && comp.compare(item, data[place-1])
< 0){
data[place] = data[place-1];
place--;
}
data[place]= item;
} Consumes wild when bear often humans a
}
COMP103 2006/T3 18
Insertion Sort: Analysis
• Efficiency
• Insertion Sort is O(n2).
• Insertion Sort also has an average-case time of O(n2).
• Requirements on Data
• Insertion Sort does not require random-access data.
• Operations needed: find in sorted list, remove, insert in sorted list.
• Space Usage
• Insertion Sort can be done in-place.
• It does not use significant additional storage.
• Stability
• Insertion Sort is stable.
• Performance on Nearly Sorted Data
• Insertion Sort can be written to be O(n) if only a constant number of
items are out of place.
• Insertion Sort can be written to be O(n) if each item is at most some
constant distance from where it “should” be.
COMP103 2006/T3 19
Bubble Sort
• Scan through the list
• Compare and swap adjacent elements if not in order
• ‘Bubbles’ the largest value to the last position.
COMP103 2006/T3 20
Bubble Sort — Analysis
• Efficiency
• Bubble Sort is O(n2).
• Bubble Sort also has an average-case time of O(n2).
• Requirements on Data
• Bubble Sort can work on data types without random access.
• Operations needed: (compare and) swap of consecutive items.
• Space Usage
• Bubble Sort can be done in-place.
• It does not require significant additional storage.
• Stability
• Bubble Sort is stable.
• Performance on Nearly Sorted Data /
• Bubble Sort is close to O(n) on nearly sorted data.
COMP103 2006/T3 21