论坛首页 入门技术论坛

java的同样排序函数的执行效率

浏览 1809 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-06-11  

package com.liuxt.sort;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SortMain {
 
 public static void main(String[] args) throws Exception {
  
  SortUtil sortUtil=new SortUtil();
  while(true)
  {
   System.out.print("please input the length of the Array:");
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   String arrayLength=br.readLine();
   int dataLength=Integer.parseInt(arrayLength);
   sortUtil.dataLength=dataLength;
         int[] a=sortUtil.randomArray();
         System.out.print("please select an sort algorithm:1 select 2 bubble 3 quick,0 quit:");
         String sortSelect=br.readLine();
   int algorithm=Integer.parseInt(sortSelect);
   sortUtil.sortData(algorithm,a);
   if (algorithm==0){
          System.exit(0);
         }   
  }
 }
}

package com.liuxt.sort;

import java.util.Calendar;
import java.util.Random;

import com.liuxt.sort.impl.Bubble;
import com.liuxt.sort.impl.QuickSort;
import com.liuxt.sort.impl.Select;

public class SortUtil {
 
 public  final int SelectSort=1;
 public  final int BubbleSort=2;
 public  final int QuickSort=3;
 public  final int XierSort=4;
 public  int dataLength=0;

 int maxElement = 100000;
 
 //Calendar start_time=Calendar.getInstance();
 Calendar start_time=null;
 Calendar end_time=null;


 
 
 public  int[] randomArray() {
  int[] a = new int[dataLength];
  Random random = new Random();
  for (int i = 0; i < dataLength; i++) {
   a[i] = random.nextInt(maxElement);
  }
  return a;
 } 
 
 private boolean testSort(int[] a){
  
  for(int i=0;i<a.length-1;i++){
   if (a[i]>a[i+1]) return false;
  }
  return true;
 
 }
 
 public void sortData(int algorithm,int[] a){
        if (algorithm==SelectSort){
         DataSort dataSort = new Select();
         start_time=Calendar.getInstance();
         dataSort.sortData(a);
         end_time=Calendar.getInstance();
         long duration=end_time.getTimeInMillis()-start_time.getTimeInMillis();
         System.out.println("duration time:"+(duration));
         System.out.println("elapse time:"+(duration/1000.0)+"s");
         System.out.println("test result:==="+testSort(a));
         
        }
        if (algorithm==BubbleSort){
         DataSort dataSort = new Bubble();
         start_time=Calendar.getInstance();
         dataSort.sortData(a);
         end_time=Calendar.getInstance();
         long duration=end_time.getTimeInMillis()-start_time.getTimeInMillis();
         System.out.println("elapse time:"+(duration/1000.0)+"s");
         System.out.println("test result:==="+testSort(a));
           
        }
        if (algorithm==QuickSort){
         DataSort dataSort = new QuickSort();
         start_time=Calendar.getInstance();
         dataSort.sortData(a);
         end_time=Calendar.getInstance();
         long duration=end_time.getTimeInMillis()-start_time.getTimeInMillis();
         System.out.println("elapse time:"+(duration/1000.0)+"s");
         System.out.println("test result:==="+testSort(a));
        }  
 }
 
 
 
 

}

 

package com.liuxt.sort.impl;

import com.liuxt.sort.DataSort;

public class Bubble implements DataSort {

 public void sortData(int[] data) {

  int i = 0, j = 0, temp = 0;
  boolean exchange;
  for (i = 0; i < data.length; i++) // 设定执行的次数
  {
   exchange = false;// 每趟排序前设定 交换标志
   for (j = 0; j < data.length - i - 1; j++) {
    if (data[j] > data[j + 1]) {
     // 将j和j+1互换
     temp = data[j];
     data[j] = data[j + 1];
     data[j + 1] = temp;
     exchange = true;
    }
   }
   if (!exchange) // 本趟排序未发生交换,提前终止算法
    return;
  } // end for i
 }
}

package com.liuxt.sort.impl;

import com.liuxt.sort.DataSort;

public class Select implements DataSort {

 public void sortData(int[] data) {

  int i, j, temp, min;
  int n = data.length;
  for (i = 0; i < n - 1; i++) {
   min = i;
   for (j = i + 1; j < n; j++) {
    if (data[j] < data[min]) {
     min = j;
     // temp=data[i];data[i]=data[j];data[j]=temp;
    }
   }
   temp = data[i];
   data[i] = data[min];
   data[min] = temp;
  }

 }

}

package com.liuxt.sort.impl;

import com.liuxt.sort.DataSort;

public class QuickSort implements DataSort {

 public void sortData(int[] data) {
  try {
    quickSort3(data, 0, data.length - 1);

  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 void quickSort3(int data[],int low,int high){
  int i,j,privokey;
  if (low<high){
   privokey=data[low];i=low;j=high;
   while(i<j) {
    while(i<j&&data[j]>=privokey) j--;
    if (i<j) data[i++]=data[j];
    while(i<j&&data[i]<=privokey) i++;
    if(i<j) data[j--]=data[i];
   }
   data[i]=privokey;
   quickSort3(data,low,i-1);
   quickSort3(data,i+1,high);
  }
 }

}

run.bat

echo off
set java_home=C:\Program Files\Java\jdk1.5.0_14
set classpath="%java_home%/lib/dt.jar;%java_home%/lib/tool.jar;."

java -cp %classpath% com/liuxt/sort/SortMain

执行结果:

please input the length of the Array:50000
please select an sort algorithm:1 select 2 bubble 3 quick,0 quit:1
duration time:6984
elapse time:6.984s
test result:===true
please input the length of the Array:50000
please select an sort algorithm:1 select 2 bubble 3 quick,0 quit:2
elapse time:16.469s
test result:===true
please input the length of the Array:50000
please select an sort algorithm:1 select 2 bubble 3 quick,0 quit:3
elapse time:0.016s
test result:===true

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics