Professional Documents
Culture Documents
Sorting Algorithms
Sorting Algorithms
Sorting Algorithms
Part II
Analysis of Merge Sort
mergeSort—Recursive
Cut the array in half
mergeSort the left half
mergeSort the right half
Merge the two sorted halves into one sorted array
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
8-5
Merge Sort Summary
Method mergeSort(first, last)
Definition: Sorts the array elements in ascending order.
pivot
j
i
j
i
j i
i
positio
n
swapReferences( a, i, high - 1 );
quicksort( a, low, i - 1 );
quicksort( a, i + 1, high );
}
}
[ 13 14 94 33 82 25 59 94 65 23 45 27 73 25 39 10 ]
• Start with a step-size of 5, visualize this as
breaking the list of numbers into a table
with 5 columns.
[ 10 14 73 25 23 13 27 94 33 39 25 59 94 65 82 45 ]
…..
}
a[ j ] = tmp;
Shell Sort Program
}
public static void main (String args []) {
Integer a [] = {8, 4, 0, 6, 74, 23, 14, 1, 0, 45, 4, 8};
shellsort(a);
for (int i=0; i<a.length;i++)
System.out.println (a[i]);
}
}
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
8-76
Shell Sort Implementation
http://www.cprogramming.com/tutorial/computersciencetheory/sortcomp.html