You are on page 1of 19

Sorting

COMP 103 #12


Menu
• Slow Sorting
• Insertion Sort
• Bubble Sort
• Divide and Conquer
• Fast Sorting
• Merge Sort

COMP103 2006/T3 2
Stable and Unstable Sorting

• What happens if two items are equal?

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

• Stable: equal items left in original order (InsertionSort,


MergeSort)

• Unstable: equal items may be rearranged (Selection Sort,


Quicksort)
COMP103 2006/T3 3
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 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.

public void bubbleSort(E[ ] data, int size, Comparator<E> comp){ 

for (int pos=data.length-1; pos >= 0; pos--)


    for (int scan = 0; scan <= pos -1; scan++)
        if (comp.compare(data[scan], data[scan+1]) > 0)
         swap(data, scan, scan+1);
}

Consumes wild when bear often humans a

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.

• So, Insertion Sort is often included as part of other


algorithms.
• Most good sorting methods switch to Insertion Sort for
small lists.
• Some good sorting algorithms get the data nearly sorted,
and then finish with an Insertion Sort.

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

Where does the work SubArray SubArray SubArray

Sort
SubArray

Sort
Sort
Sort Sort
Sort
happen? SortedSubArraySortedSubArray
Combine
SortedSubArraySortedSubArray
Combine

• MergeSort: SortedSubArray SortedSubArray


• split trivial
• combine does all the work Combine
• QuickSort:
• split does all the work Sorted Array
• combine trivial

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

Analysis of the Merge Cost = O(n)

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

if (high == low+1) // only one item to sort.


      data[low] = temp[low];   // copy the item over.
else if (high > low){
      int mid = (low+high)/2;   // mid = low of upper half, = high of lower half.

      mergeSort(temp, data, low, mid, comp);


      mergeSort(temp, data, mid, high, comp);

      merge(data, temp, low, mid, high, comp);


 }
}

Note how we swap temp and data each recursive call

COMP103 2006/T3 18
Merge Code
private void merge(E[] temp, E[] data, int lo, int mid, int hi,
Comparator<E> comp){

int t_lo = lo, t_hi = mid;

for (int i = lo; i < hi; i++) /* Merge sorted sublists */


if ((t_lo < mid) && ((t_hi >= hi) || (data[t_lo] < data[t_hi])))
temp[i] = data[t_lo++];
else
temp[i] = data[t_hi++];
}

COMP103 2006/T3 19

You might also like