-
- package paixu;
-
- public class PaiXu {
- final int MAX=20;
- int num[]=new int[MAX];
- {
- System.out.print("生成的随机数组是:");
- for(int i=0;i<20;i++){
- num[i]=(int)(Math.random()*100);
- System.out.print(num[i]+" ");
- }
- System.out.println();
- }
-
- int num2[]=new int[MAX];
- {
- System.out.print("合并排序法需要使用的数组2是:");
- for(int i=0;i<20;i++){
- num2[i]=(int)(Math.random()*100);
- System.out.print(num2[i]+" ");
- }
- System.out.println();
- }
-
- int num3[]=new int[MAX+MAX];
-
-
- public PaiXu(){
- selsort(num.clone());
- insort(num.clone());
- bubsort(num.clone());
- shellsort(num.clone());
- shakersort(num.clone());
- heapsort(num.clone());
- quicksort_one(num.clone());
- quicksort_two(num.clone());
- quicksort_three(num.clone());
- mergesort(num.clone(),num2.clone(),num3);
- basesort(num.clone());
- }
-
-
- public void selsort(int number[]) {
- int i, j, k, m, temp;
- long start,end;
-
- start=System.nanoTime();
- for(i = 0; i < MAX-1; i++) {
- m = i;
- for(j = i+1; j < MAX; j++){
- if(number[j] < number[m]){
- m = j;
- }
- }
- if( i != m){
- temp=number[i];
- number[i]=number[m];
- number[m]=temp;
- }
- }
- end=System.nanoTime();
-
- System.out.println("-----------------选择排序法------------------");
- System.out.print("排序后是:");
- for(i=0;i<=MAX-1;i++){
- System.out.print(number[i]+" ");
- }
- System.out.println();
- System.out.println("排序使用时间:"+(end-start)+" ns");
- }
-
-
-
-
- public void insort(int number[]){
- int i, j, k, temp;
- long start,end;
-
- start=System.nanoTime();
- for(j = 1; j < MAX; j++) {
- temp = number[j];
- i = j - 1;
- while(temp < number[i]) {
- number[i+1] = number[i];
- i--;
- if(i == -1){
- break;
- }
- }
- number[i+1] = temp;
- }
- end=System.nanoTime();
-
- System.out.println("-----------------插入排序法------------------");
- System.out.print("排序后是:");
- for(i=0;i<=MAX-1;i++){
- System.out.print(number[i]+" ");
- }
- System.out.println();
- System.out.println("排序使用时间:"+(end-start)+" ns");
- }
-
-
-
- public void bubsort(int number[]){
- int i, j, k, temp, flag = 1;
- long start,end;
-
- start=System.nanoTime();
- for(i = 0; i < MAX-1 && flag == 1; i++) {
- flag = 0;
- for(j = 0; j < MAX-i-1; j++) {
- if(number[j+1] < number[j]) {
- temp=number[j+1];
- number[j+1]=number[j];
- number[j]=temp;
- flag = 1;
- }
- }
- }
- end=System.nanoTime();
-
- System.out.println("-----------------冒泡排序法------------------");
- System.out.print("排序后是:");
- for(i=0;i<=MAX-1;i++){
- System.out.print(number[i]+" ");
- }
- System.out.println();
- System.out.println("排序使用时间:"+(end-start)+" ns");
- }
-
-
-
-
- public void shellsort(int number[]) {
- int i, j, k, gap, temp;
- long start,end;
-
- start=System.nanoTime();
- gap = MAX / 2;
- while(gap > 0) {
- for(k = 0; k < gap; k++) {
- for(i = k+gap; i < MAX; i+=gap) {
- for(j = i - gap; j >= k; j-=gap) {
- if(number[j] > number[j+gap]) {
- temp=number[j];
- number[j]=number[j+gap];
- number[j+gap]=temp;
- }else{
- break;
- }
- }
- }
- }
- gap /= 2;
- }
- end=System.nanoTime();
-
- System.out.println("-----------------shell(希尔)排序法(改进的插入排序法)------------------");
- System.out.print("排序后是:");
- for(i=0;i<=MAX-1;i++){
- System.out.print(number[i]+" ");
- }
- System.out.println();
- System.out.println("排序使用时间:"+(end-start)+" ns");
- }
-
-
-
-
- public void shakersort(int number[]) {
- int i, temp, left = 0, right = MAX - 1, shift = 0;
- long start,end;
-
- start=System.nanoTime();
- while(left < right) {
-
- for(i = left; i < right; i++) {
- if(number[i] > number[i+1]) {
- temp=number[i];
- number[i]=number[i+1];
- number[i+1]=temp;
- shift = i;
- }
- }
- right = shift;
-
-
- for(i = right; i > left; i--) {
- if(number[i] < number[i-1]) {
- temp=number[i];
- number[i]=number[i-1];
- number[i-1]=temp;
- shift = i;
- }
- }
- left = shift;
- }
- end=System.nanoTime();
-
- System.out.println("-----------------shake排序法(改进的冒泡排序法)------------------");
- System.out.print("排序后是:");
- for(i=0;i<=MAX-1;i++){
- System.out.print(number[i]+" ");
- }
- System.out.println();
- System.out.println("排序使用时间:"+(end-start)+" ns");
- }
-
-
-
-
-
- public void heapsort(int number[]) {
- int i, m, p, s, temp;
- long start,end;
-
- start=System.nanoTime();
- int number_temp[]=new int[MAX+1];
- for(int temp_i=1;temp_i<MAX+1;temp_i++){
- number_temp[temp_i]=number[temp_i-1];
- }
- createheap(number_temp);
- m = MAX;
- while(m > 1) {
- temp=number_temp[1];
- number_temp[1]=number_temp[m];
- number_temp[m]=temp;
- m--;
- p = 1;
- s = 2 * p;
- while(s <= m) {
- if(s < m && number_temp[s+1] > number_temp[s])
- s++;
- if(number_temp[p] >= number_temp[s])
- break;
- temp=number_temp[p];
- number_temp[p]=number_temp[s];
- number_temp[s]=temp;
- p = s;
- s = 2 * p;
- }
- }
- for(int temp_j=1;temp_j<MAX+1;temp_j++){
- number[temp_j-1]=number_temp[temp_j];
- }
- end=System.nanoTime();
-
-
- System.out.println("-----------------heap排序(堆排序法--改进的选择排序)------------------");
- System.out.print("排序后是:");
- for(i=0;i<=MAX-1;i++){
- System.out.print(number[i]+" ");
- }
- System.out.println();
- System.out.println("排序使用时间:"+(end-start)+" ns");
- }
-
-
- public void createheap(int number[]) {
- int i, s, p, temp;
- int heap[] = new int[MAX+1];
- for(i = 1; i <= MAX; i++) {
- heap[i] = number[i];
- s = i;
- p = i / 2;
- while(s >= 2 && heap[p] < heap[s]) {
- temp=heap[p];
- heap[p]=heap[s];
- heap[s]=temp;
- s = p;
- p = s / 2;
- }
- }
- for(i = 1; i <= MAX; i++){
- number[i] = heap[i];
- }
- }
-
-
-
-
-
-
-
- public void quicksort_one(int number[]){
- long start,end;
-
- start=System.nanoTime();
- quicksort_1(number,0,MAX-1);
- end=System.nanoTime();
-
- System.out.println("-----------------快速排序法( 一 )------------------");
- System.out.print("排序后是:");
- for(int i=0;i<=MAX-1;i++){
- System.out.print(number[i]+" ");
- }
- System.out.println();
- System.out.println("排序使用时间:"+(end-start)+" ns");
-
-
- }
-
-
- public void quicksort_1(int number[],int left,int right) {
- int i, j, s, temp;
- if(left < right) {
- s = number[left];
- i = left;
- j = right + 1;
- while(true) {
-
- while(i + 1 < number.length && number[++i] < s) ;
-
- while(j -1 > -1 && number[--j] > s) ;
- if(i >= j)
- break;
- temp=number[i];
- number[i]=number[j];
- number[j]=temp;
- }
- number[left] = number[j];
- number[j] = s;
- quicksort_1(number, left, j-1);
- quicksort_1(number, j+1, right);
- }
- }
-
-
-
-
- public void quicksort_two(int number[]){
- long start,end;
-
- start=System.nanoTime();
- quicksort_2(number,0,MAX-1);
- end=System.nanoTime();
-
- System.out.println("-----------------快速排序法( 二 )------------------");
- System.out.print("排序后是:");
- for(int i=0;i<=MAX-1;i++){
- System.out.print(number[i]+" ");
- }
- System.out.println();
- System.out.println("排序使用时间:"+(end-start)+" ns");
- }
-
-
- public void quicksort_2(int number[], int left, int right) {
- int i, j, s, temp;
- if(left < right) {
- s = number[(left+right)/2];
- i = left - 1;
- j = right + 1;
- while(true) {
- while(number[++i] < s) ;
- while(number[--j] > s) ;
- if(i >= j)
- break;
- temp=number[i];
- number[i]=number[j];
- number[j]=temp;
- }
- quicksort_2(number, left, i-1);
- quicksort_2(number, j+1, right);
- }
- }
-
-
-
-
-
- public void quicksort_three(int number[]){
- long start,end;
-
- start=System.nanoTime();
- quicksort_3(number,0,MAX-1);
- end=System.nanoTime();
-
- System.out.println("-----------------快速排序法( 三 )------------------");
- System.out.print("排序后是:");
- for(int i=0;i<=MAX-1;i++){
- System.out.print(number[i]+" ");
- }
- System.out.println();
- System.out.println("排序使用时间:"+(end-start)+" ns");
-
- }
-
-
- public int partition(int number[], int left, int right) {
- int i, j, s, temp;
- s = number[right];
- i = left - 1;
- for(j = left; j < right; j++) {
- if(number[j] <= s) {
- i++;
- temp=number[i];
- number[i]=number[j];
- number[j]=temp;
- }
- }
- temp=number[i+1];
- number[i+1]=number[right];
- number[right]=temp;
- return i+1;
- }
-
- public void quicksort_3(int number[], int left, int right) {
- int q;
- if(left < right) {
- q = partition(number, left, right);
- quicksort_3(number, left, q-1);
- quicksort_3(number, q+1, right);
- }
- }
-
-
-
-
-
-
-
- public void mergesort(int number1[],int number2[],int number3[]){
- long start,end;
-
- start=System.nanoTime();
- quicksort_3(number1,0,MAX-1);
- quicksort_3(number2,0,MAX-1);
- mergesort_merge(number1,MAX,number2,MAX,number3);
- end=System.nanoTime();
-
- System.out.println("-----------------合并排序法------------------");
- System.out.print("排序后是:");
- for(int i=0;i<=MAX+MAX-1;i++){
- System.out.print(number3[i]+" ");
- }
- System.out.println();
- System.out.println("排序使用时间:"+(end-start)+" ns");
- }
-
-
- public void mergesort_merge(int number1[], int M, int number2[], int N, int number3[]) {
- int i = 0, j = 0, k = 0;
- while(i < M && j < N) {
- if(number1[i] <= number2[j]){
- number3[k++] = number1[i++];
- }else{
- number3[k++] = number2[j++];
- }
- }
- while(i < M){
- number3[k++] = number1[i++];
- }
- while(j < N){
- number3[k++] = number2[j++];
- }
- }
-
-
-
-
-
- public void basesort(int number[]){
- int temp[][] = new int[MAX][MAX];
- int order[] = new int[MAX];
- int i, j, k, n, lsd;
- long start,end;
- k = 0;
- n = 1;
-
-
- start=System.nanoTime();
- while(n <= 10) {
- for(i = 0; i < MAX; i++) {
- lsd = ((number[i] / n) % 10);
- temp[lsd][order[lsd]] = number[i];
- order[lsd]++;
- }
-
- for(i = 0; i < MAX; i++) {
- if(order[i] != 0)
- for(j = 0; j < order[i]; j++) {
- number[k] = temp[i][j];
- k++;
- }
- order[i] = 0;
- }
- n *= 10;
- k = 0;
- }
- end=System.nanoTime();
-
- System.out.println("-----------------基数排序法------------------");
- System.out.print("排序后是:");
- for(int ii=0;ii<=MAX-1;ii++){
- System.out.print(number[ii]+" ");
- }
- System.out.println();
- System.out.println("排序使用时间:"+(end-start)+" ns");
- }
-
-
- public static void main(String[] args){
- System.out.println("以下的测试时间仅供参考...");
- new PaiXu();
- }
-
- }
以上代码的运行结果如下所示:
- 以下的测试时间仅供参考...
- 生成的随机数组是:53 82 61 70 75 31 30 68 22 56 48 23 12 74 13 85 69 62 21 55
- 合并排序法需要使用的数组2是:2 12 48 18 93 13 98 87 55 77 89 56 6 31 56 38 59 76 90 30
- -----------------选择排序法------------------
- 排序后是:12 13 21 22 23 30 31 48 53 55 56 61 62 68 69 70 74 75 82 85
- 排序使用时间:7815 ns
- -----------------插入排序法------------------
- 排序后是:12 13 21 22 23 30 31 48 53 55 56 61 62 68 69 70 74 75 82 85
- 排序使用时间:7475 ns
- -----------------冒泡排序法------------------
- 排序后是:12 13 21 22 23 30 31 48 53 55 56 61 62 68 69 70 74 75 82 85
- 排序使用时间:18008 ns
- -----------------shell(希尔)排序法(改进的插入排序法)------------------
- 排序后是:12 13 21 22 23 30 31 48 53 55 56 61 62 68 69 70 74 75 82 85
- 排序使用时间:11212 ns
- -----------------shake排序法(改进的冒泡排序法)------------------
- 排序后是:12 13 21 22 23 30 31 48 53 55 56 61 62 68 69 70 74 75 82 85
- 排序使用时间:14610 ns
- -----------------heap排序(堆排序法--改进的选择排序)------------------
- 排序后是:12 13 21 22 23 30 31 48 53 55 56 61 62 68 69 70 74 75 82 85
- 排序使用时间:15969 ns
- -----------------快速排序法( 一 )------------------
- 排序后是:12 13 21 22 23 30 31 48 53 55 56 61 62 68 69 70 74 75 82 85
- 排序使用时间:8834 ns
- -----------------快速排序法( 二 )------------------
- 排序后是:12 13 21 22 23 30 31 48 53 55 56 61 62 68 69 70 74 75 82 85
- 排序使用时间:9853 ns
- -----------------快速排序法( 三 )------------------
- 排序后是:12 13 21 22 23 30 31 48 53 55 56 61 62 68 69 70 74 75 82 85
- 排序使用时间:10533 ns
- -----------------合并排序法------------------
- 排序后是:2 6 12 12 13 13 18 21 22 23 30 30 31 31 38 48 48 53 55 55 56 56 56 59 61 62 68 69 70 74 75 76 77 82 85 87 89 90 93 98
- 排序使用时间:20387 ns
- -----------------基数排序法------------------
- 排序后是:12 13 21 22 23 30 31 48 53 55 56 61 62 68 69 70 74 75 82 85
- 排序使用时间:8495 ns
java几种常见的排序算法总结
原文:http://www.cnblogs.com/yumo1627129/p/7241979.html