常用排序算法及其测试用例
import org. junit. Test;
public class Example {
public static void sort ( int [ ] a) {
}
private static boolean less ( int v, int w) {
return v- w < 0 ;
}
private static void exch ( int [ ] a, int i, int j) {
int t = a[ i] ;
a[ i] = a[ j] ;
a[ j] = t;
}
private static void show ( int [ ] a) {
for ( int i = 0 ; i < a. length; i++ ) {
System. out. print ( a[ i] + " " ) ;
}
}
public static boolean isSorted ( int [ ] a) {
for ( int i = 1 ; i < a. length; i++ ) {
if ( less ( a[ i] , a[ i- 1 ] ) ) return false ;
}
return true ;
}
public static void sectionSort ( int [ ] a) {
int N = a. length;
for ( int i = 0 ; i < N; i++ ) {
int min = i;
for ( int j = i+ 1 ; j < N; j++ ) {
if ( less ( a[ j] , a[ min] ) ) min = j;
}
exch ( a, i, min) ;
}
}
@Test
public void sectionTest ( ) {
int [ ] a = { 4 , 2 , 4 , 1 , 9 , 7 , 5 , 8 } ;
sectionSort ( a) ;
show ( a) ;
}
public static void insertionSort ( int [ ] a) {
for ( int i = 1 ; i < a. length; i++ ) {
for ( int j = i; j > 0 && less ( a[ j] , a[ j- 1 ] ) ; j-- ) {
exch ( a, j, j- 1 ) ;
}
}
}
@Test
public void insertionTest ( ) {
int [ ] a = { 4 , 2 , 6 , 1 , 9 , 7 , 5 , 8 } ;
insertionSort ( a) ;
show ( a) ;
}
public static void shellsort ( int [ ] a) {
int N = a. length;
int h = 1 ;
while ( h < N/ 3 ) h = h* 3 + 1 ;
while ( h >= 1 ) {
for ( int i = h; i < N; i++ ) {
for ( int j = i; j >= h && less ( a[ j] , a[ j- h] ) ; j -= h) {
exch ( a, j, j- h) ;
}
}
h = h/ 3 ;
}
}
@Test
public void shellTest ( ) {
int [ ] a = { 4 , 2 , 6 , 1 , 9 , 7 , 5 , 8 } ;
shellsort ( a) ;
show ( a) ;
}
public static void merge1 ( int [ ] a, int lo, int mid, int hi) {
int i = lo;
int j = mid + 1 ;
int [ ] aux = new int [ a. length] ;
for ( int k = lo; k <= hi; k++ ) {
aux[ k] = a[ k] ;
}
for ( int k = lo; k <= hi; k++ )
{
if ( i > mid) a[ k] = aux[ j++ ] ;
else if ( j > hi) a[ k] = aux[ i++ ] ;
else if ( aux[ j] < aux[ i] ) a[ k] = aux[ j++ ] ;
else a[ k] = aux[ i++ ] ;
}
}
@Test
public void mergeTest1 ( ) {
int [ ] a = { 1 , 2 , 5 , 7 , 4 , 6 , 8 , 9 } ;
merge1 ( a, 0 , 3 , 7 ) ;
show ( a) ;
}
public static void merge2 ( int [ ] a) {
int n = a. length;
int [ ] aux = new int [ n] ;
for ( int sz = 1 ; sz < n; sz = 2 * sz) {
for ( int lo = 0 ; lo < n- sz; lo += 2 * sz) {
merge1 ( a, lo, lo+ sz- 1 , Math. min ( n- 1 , lo+ 2 * sz- 1 ) ) ;
}
}
}
@Test
public void mergeTest2 ( ) {
int [ ] a = { 1 , 2 , 5 , 7 , 12 , 4 , 16 , 8 , 9 , 10 , 3 } ;
merge2 ( a) ;
show ( a) ;
}
public static void quick ( int [ ] a, int lo, int hi) {
if ( lo >= hi) return ;
int j = partition ( a, lo, hi) ;
quick ( a, lo, j- 1 ) ;
quick ( a, j+ 1 , hi) ;
}
private static int partition ( int [ ] a, int lo, int hi) {
int i = lo;
int j = hi+ 1 ;
int v = a[ lo] ;
while ( true ) {
while ( a[ ++ i] - v < 0 ) {
if ( i == hi) break ;
}
while ( a[ -- j] - v > 0 ) {
if ( j == lo) break ;
}
if ( i >= j) break ;
exch ( a, i, j) ;
}
exch ( a, lo, j) ;
return j;
}
@Test
public void quickTest ( ) {
int [ ] a = { 2 , 1 , 1 , 3 , 3 , 2 , 2 , 2 , 2 , 3 , 1 , 1 , 2 , 3 } ;
quick ( a, 0 , a. length- 1 ) ;
show ( a) ;
}
public static void quick3Way ( int [ ] a, int lo, int hi) {
if ( hi <= lo) return ;
int lt = lo, i = lo+ 1 , gt = hi;
int v = a[ lo] ;
while ( i <= gt) {
int cmp = a[ i] - v;
if ( cmp < 0 ) exch ( a, lt++ , i++ ) ;
else if ( cmp > 0 ) exch ( a, gt-- , i) ;
else i++ ;
}
quick3Way ( a, lo, lt- 1 ) ;
quick3Way ( a, gt+ 1 , hi) ;
}
@Test
public void quick3WayTest ( ) {
int [ ] a = { 2 , 1 , 1 , 3 , 3 , 2 , 2 , 2 , 2 , 3 , 1 , 1 , 2 , 3 } ;
quick3Way ( a, 0 , a. length- 1 ) ;
show ( a) ;
}
}