经典排序算法
1.快速排序
package com. hooware. sorttest. quick;
import com. hooware. RandomNumber;
import java. util. Arrays;
@SuppressWarnings ( "all" )
public class Quickly {
public static void main ( String[ ] args) {
int [ ] array = RandomNumber. getRandomArray ( 10000000 ) ;
Quickly quickly = new Quickly ( ) ;
long start = System. currentTimeMillis ( ) ;
quickly. quickSort ( array, 0 , array. length- 1 ) ;
long end = System. currentTimeMillis ( ) ;
System. out. println ( "================= \n The array is ordered? : " + RandomNumber. isOrderedArray ( array) + "! consume " + ( end- start) / 1000.0 + " s!" ) ;
}
public void quickSort ( int [ ] array, int l, int r) {
if ( l < r) {
int mid = partition ( array, l, r) ;
quickSort ( array, l, mid- 1 ) ;
quickSort ( array, mid+ 1 , r) ;
}
}
private int partition ( int [ ] array, int l, int r) {
int pivot = l;
int index = pivot + 1 ;
for ( int i = index; i <= r; i++ ) {
if ( array[ i] < array[ pivot] ) {
swap ( array, i, index) ;
index++ ;
}
}
swap ( array, pivot, index- 1 ) ;
return index- 1 ;
}
public void swap ( int [ ] array, int i, int j) {
int temp = array[ i] ;
array[ i] = array[ j] ;
array[ j] = temp;
}
}
2.希尔排序
package com. hooware. sorttest. shell;
import com. hooware. RandomNumber;
import com. hooware. sorttest. merge. MergeSort;
public class ShellSort {
public static void main ( String[ ] args) {
int [ ] array = RandomNumber. getRandomArray ( 10000000 ) ;
ShellSort shellSort = new ShellSort ( ) ;
long start = System. currentTimeMillis ( ) ;
shellSort. shellSort ( array) ;
long end = System. currentTimeMillis ( ) ;
System. out. println ( "================= \n The array is ordered? : " + RandomNumber. isOrderedArray ( array) + "! consume " + ( end - start) / 1000.0 + " s!" ) ;
}
private void shellSort ( int [ ] array) {
for ( int gap = array. length / 2 ; gap > 0 ; gap /= 2 ) {
for ( int i = gap; i < array. length; i++ ) {
int j = i;
while ( j - gap >= 0 && array[ j] < array[ j - gap] ) {
swap ( array, j - gap, j) ;
j -= gap;
}
}
}
}
public void swap ( int [ ] array, int i, int j) {
int temp = array[ i] ;
array[ i] = array[ j] ;
array[ j] = temp;
}
}
3.归并排序
package com. hooware. sorttest. merge;
import com. hooware. RandomNumber;
import java. util. Arrays;
public class MergeSort {
public static void main ( String[ ] args) {
int [ ] array = RandomNumber. getRandomArray ( 100000 ) ;
MergeSort mergeSort = new MergeSort ( ) ;
long start = System. currentTimeMillis ( ) ;
int [ ] sortArray = mergeSort. mergeSort ( array) ;
long end = System. currentTimeMillis ( ) ;
System. out. println ( "================= \n The array is ordered? : " + RandomNumber. isOrderedArray ( sortArray) + "! consume " + ( end- start) / 1000.0 + " s!" ) ;
}
private int [ ] mergeSort ( int [ ] array) {
int [ ] arr = Arrays. copyOf ( array, array. length) ;
if ( array. length < 2 ) {
return array;
}
int mid = array. length/ 2 ;
int [ ] left = Arrays. copyOfRange ( array, 0 , mid) ;
int [ ] right = Arrays. copyOfRange ( array, mid, array. length) ;
return merge ( mergeSort ( left) , mergeSort ( right) ) ;
}
private int [ ] merge ( int [ ] left, int [ ] right) {
int [ ] result = new int [ left. length+ right. length] ;
int i = 0 ;
while ( left. length > 0 && right. length > 0 ) {
if ( left[ 0 ] < right[ 0 ] ) {
result[ i] = left[ 0 ] ;
i++ ;
left = Arrays. copyOfRange ( left, 1 , left. length) ;
} else {
result[ i] = right[ 0 ] ;
i++ ;
right = Arrays. copyOfRange ( right, 1 , right. length) ;
}
}
while ( left. length > 0 ) {
result[ i] = left[ 0 ] ;
i++ ;
left = Arrays. copyOfRange ( left, 1 , left. length) ;
}
while ( right. length > 0 ) {
result[ i] = right[ 0 ] ;
i++ ;
right = Arrays. copyOfRange ( right, 1 , right. length) ;
}
return result;
}
}
4.冒泡排序
package com. mysort;
import java. util. Arrays;
public class BubbleSort {
private int [ ] array = { 23 , 11 , 7 , 29 , 33 , 59 , 8 , 20 , 9 , 3 , 2 , 6 , 10 , 44 , 83 , 28 , 5 , 1 , 0 } ;
public static void main ( String[ ] args) {
BubbleSort bubbleSort = new BubbleSort ( ) ;
System. out. println ( "Initial Array : " + Arrays. toString ( bubbleSort. array) ) ;
bubbleSort. bubble ( bubbleSort. array) ;
System. out. println ( "Sorted Array : " + Arrays. toString ( bubbleSort. array) ) ;
System. out. println ( "-----------------------" ) ;
int [ ] arr1 = SortAlgsCheck. getRandomArray ( 10000 ) ;
bubbleSort. bubble ( arr1) ;
boolean flag = SortAlgsCheck. testIsPass ( arr1) ;
System. out. println ( "test is success? " + flag) ;
}
public void bubble ( int [ ] array) {
int len = array. length;
for ( int i = 0 ; i < len - 1 ; i++ ) {
for ( int j = 0 ; j < len - 1 - i; j++ ) {
if ( array[ j] > array[ j+ 1 ] ) {
int temp = array[ j] ;
array[ j] = array[ j+ 1 ] ;
array[ j+ 1 ] = temp;
}
}
}
}
}
5.选择排序
package com. mysort;
import java. util. Arrays;
public class SelectSort {
private int [ ] array = { 23 , 11 , 7 , 29 , 33 , 59 , 8 , 20 , 9 , 3 , 2 , 6 , 10 , 44 , 83 , 28 , 5 , 1 , 0 } ;
public static void main ( String[ ] args) {
SelectSort selectSort = new SelectSort ( ) ;
System. out. println ( "Initial Array : " + Arrays. toString ( selectSort. array) ) ;
selectSort. select ( selectSort. array) ;
System. out. println ( "Sorted Array : " + Arrays. toString ( selectSort. array) ) ;
System. out. println ( "----------------------" ) ;
int [ ] arr1 = SortAlgsCheck. getRandomArray ( 10000 ) ;
selectSort. select ( arr1) ;
boolean flag = SortAlgsCheck. testIsPass ( arr1) ;
System. out. println ( "test is success? " + flag) ;
}
public void select ( int [ ] array) {
int len = array. length;
for ( int i = 0 ; i < len - 1 ; i++ ) {
for ( int j = i + 1 ; j < len; j++ ) {
if ( array[ i] > array[ j] ) {
int temp = array[ i] ;
array[ i] = array[ j] ;
array[ j] = temp;
}
}
}
}
}
6.插入排序
package com. mysort;
import java. util. Arrays;
public class InsertSort {
private int [ ] array = { 23 , 11 , 7 , 29 , 33 , 59 , 8 , 20 , 9 , 3 , 2 , 6 , 10 , 44 , 83 , 28 , 5 , 1 , 0 , 36 } ;
public static void main ( String[ ] args) {
InsertSort insert = new InsertSort ( ) ;
System. out. println ( "Initial Array : " + Arrays. toString ( insert. array) ) ;
insert. insertSort ( insert. array) ;
System. out. println ( "Sorted Array : " + Arrays. toString ( insert. array) ) ;
System. out. println ( "--------------" ) ;
int [ ] arr1 = SortAlgsCheck. getRandomArray ( 10000 ) ;
insert. insert ( arr1) ;
boolean flag = SortAlgsCheck. testIsPass ( arr1) ;
System. out. println ( "test is success? " + flag) ;
}
public void insertSort ( int [ ] arr) {
int temp;
for ( int i = 0 ; i < arr. length - 1 ; i++ ) {
for ( int j = i + 1 ; j > 0 ; j-- ) {
if ( arr[ j] < arr[ j - 1 ] ) {
temp = arr[ j] ;
arr[ j] = arr[ j - 1 ] ;
arr[ j - 1 ] = temp;
}
}
}
}
public void insert ( int [ ] array) {
for ( int i = 1 ; i < array. length; i++ ) {
for ( int j = i; j > 0 ; j-- ) {
if ( array[ j- 1 ] > array[ j] ) {
int temp = array[ j- 1 ] ;
array[ j- 1 ] = array[ j] ;
array[ j] = temp;
}
}
}
}
}
7.随机数组生成及校验数组有序函数
package com. mysort;
import java. util. Random;
public class SortAlgsCheck {
public static int [ ] getRandomArray ( int len) {
int [ ] array = new int [ len] ;
Random random = new Random ( ) ;
for ( int i = 0 ; i < array. length; i++ ) {
array[ i] = random. nextInt ( Integer. MAX_VALUE) ;
}
return array;
}
public static boolean testIsPass ( int [ ] array) {
int len = array. length;
for ( int i = 1 ; i < len; i++ ) {
if ( array[ i- 1 ] > array[ i] ) {
return false ;
}
}
return true ;
}
}