Professional Documents
Culture Documents
COMP103 2006/T3 2
Stable and Unstable Sorting
B A1 M J A2 H X C P
Sort
A1 A2 B C H J M P X
OR
A2 A1 B C H J M P X
}
COMP103 2006/T3 4
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 5
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 6
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 7
Insertion Sort:
Comments
• Insertion Sort is too slow for general-purpose use.
• However, Insertion Sort is used in very limited
settings.
• Insertion Sort is fast for some kinds of nearly sorted
data [O(n)].
• Insertion Sort is fast for small lists.
COMP103 2006/T3 8
Summary Slow Sorts
• Insertion sort, Selection Sort, Bubble Sort:
• All slow (except Insertion sort on almost sorted lists)
• Problem:
• Insertion and Bubble
• only compare adjacent items
• only move items one step at a time
• Selection
• compares every pair of items –
• ignores results of previous comparisons.
• Solution:
• Must compare and swap items at a distance
• Must not perform redundant comparisons
COMP103 2006/T3 9
Divide and Conquer Sorts
To Sort: Array
• Split
• Sort each part Split
(recursive)
• Combine
SubArray SubArray
Split Split
Sort
SubArray
Sort
Sort
Sort Sort
Sort
happen? SortedSubArraySortedSubArray
Combine
SortedSubArraySortedSubArray
Combine
COMP103 2006/T3 10
Mergesort
• How can we use divide-and-conquer to
build a better sorting algorithm?
• Suppose we split a list into two equal-sized (or
nearly so) pieces, and sort each piece
recursively.
• Ultimately we end up with pieces of size 1.
• Then merge the two parts into a single sorted
list.
• The resulting sorting algorithm is Merge Sort.
COMP103 2006/T3 11
Mergesort
• If we want an O(n log n) sort, how
long can a Merge operation take?
• Spliting the list O(log(n)) (dividing into
½)
• therefore Merge is allowed O(n).
COMP103 2006/T3 12
MergeSort : Split
COMP103 2006/T3 13
Merging
• To make merging more efficient we use a separate
destination array.
• Check which item comes first, and copy that to the buffer.
Advance the indexes as appropriate.
7 12
7 12
13 13
17 17 1 15 5
1115
11 15
COMP103 2006/T3 14
Mergesort Pseudocode
mergesort(data)
if data have at least 2 elements
mergesort (left half of data)
mergesort (right half of data)
merge(both halves into sorted
list)
COMP103 2006/T3 15
Example
• Use merge sort to sort the array:
1 8 6 4 10 5 3 2 22
1 8 6 4 10 5 3 2 22
COMP103 2006/T3 16
Merge Sort: Init Code
• Initalisation wrapper
• create temporary array
• fill with a copy of the original data.
• then, recurse (swapping between copy and
original)
public static <E> void mergeSort(E[] data, int size,
Comparator<E> comp){
E[] other = (E[])new Object[size];
for (int i=0; i<size; i++)
other[i]=data[i];
mergeSort(data, other, 0, size, comp);
}
COMP103 2006/T3 17
MergeSort Code
private static <E> void mergeSort(E[] data, E[] temp, int low, int high,
Comparator<E> comp){
// sort items from low..high-1 using temp array
COMP103 2006/T3 18
Merge Code
private void merge(E[] temp, E[] data, int lo, int mid, int hi,
Comparator<E> comp){
COMP103 2006/T3 19