JAVA多种排序算法时间实测(算法课做的)

代码都貌似很容易找到,这里大致把我测试的结果放下,用matlab做了一个统计。

import java.util.Random;
public class sort{

public static void insertionSort(int[] arr) {
  int i, j, newValue;
  for (i = 1; i < arr.length; i++)
  {  
    newValue = arr[i];
    j = i;
    while (j > 0 && arr[j - 1] > newValue) 
    {
      arr[j] = arr[j - 1];
      j--;
     }
    arr[j] = newValue;
  }
}

public static void selectionSort(int[] arr) {

      int i, j, minIndex, tmp;

      int n = arr.length;

      for (i = 0; i < n - 1; i++) {

            minIndex = i;

            for (j = i + 1; j < n; j++)

                  if (arr[j] < arr[minIndex])

                        minIndex = j;

            if (minIndex != i) {

                  tmp = arr[i];

                  arr[i] = arr[minIndex];

                  arr[minIndex] = tmp;

            }

      }

}

public static void bubbleSort(int[] arr) {

      boolean swapped = true;

      int j = 0;

      int tmp;

      while (swapped) {

            swapped = false;

            j++;

            for (int i = 0; i < arr.length - j; i++) {                                       

                  if (arr[i] > arr[i + 1]) {                          

                        tmp = arr[i];

                        arr[i] = arr[i + 1];

                        arr[i + 1] = tmp;

                        swapped = true;

                  }

            }                

      }

}

public static int partition(int arr[], int left, int right)

{

      int i = left, j = right;

      int tmp;

      int pivot = arr[(left + right) / 2];

     

      while (i <= j) {

            while (arr[i] < pivot)

                  i++;

            while (arr[j] > pivot)

                  j--;

            if (i <= j) {

                  tmp = arr[i];

                  arr[i] = arr[j];

                  arr[j] = tmp;

                  i++;

                  j--;

            }

      };

     

      return i;

}

 
public static void quickSort(int arr[], int left, int right) {

      int index = partition(arr, left, right);

      if (left < index - 1)

            quickSort(arr, left, index - 1);

      if (index < right)

            quickSort(arr, index, right);

}

public static void mergeSort(int[] data, int left, int right) {  
    // TODO Auto-generated method stub  
    if(left<right){  
        //找出中间索引  
        int center=(left+right)/2;  
        //对左边数组进行递归  
        mergeSort(data,left,center);  
        //对右边数组进行递归  
        mergeSort(data,center+1,right);  
        //合并  
        merge(data,left,center,right);  
          
    }  
}  
public static void merge(int[] data, int left, int center, int right) {  
    // TODO Auto-generated method stub  
    int [] tmpArr=new int[data.length];  
    int mid=center+1;  
    //third记录中间数组的索引  
    int third=left;  
    int tmp=left;  
    while(left<=center&&mid<=right){  
 
   //从两个数组中取出最小的放入中间数组  
        if(data[left]<=data[mid]){  
            tmpArr[third++]=data[left++];  
        }else{  
            tmpArr[third++]=data[mid++];  
        }  
    }  
    //剩余部分依次放入中间数组  
    while(mid<=right){  
        tmpArr[third++]=data[mid++];  
    }  
    while(left<=center){  
        tmpArr[third++]=data[left++];  
    }  
    //将中间数组中的内容复制回原数组  
    while(tmp<=right){  
        data[tmp]=tmpArr[tmp++];  
    }  
}  

public static void main(String args[])
{  
  long t0,t1;
  int i,N=1000;
  int a[] = new int[N];
  for(i=0;i<a.length;i++)
  {
     Random random = new Random();
     a[i] = random.nextInt(N);
  }
  for(i=0;i<a.length;i++)
     System.out.print(a[i]+" ");
  System.out.println(" ");
  t0 = System.nanoTime();
  insertionSort(a);
  //selectionSort(a);
  //bubbleSort(a);
  //quickSort(a,0,a.length-1);
  //mergeSort(a,0,a.length-1);
  t1 = System.nanoTime();
  for(i=0;i<a.length;i++)
     System.out.print(a[i]+" ");
  System.out.println(" ");
  System.out.println("t0:"+t0+" t1:"+t1+" t1-t0:"+(t1-t0));
}

}

测试结果












©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页