在軟件開發中,對於開發人員危害最大的是Ctrl+ C和Ctrl+ V,自己嘗試寫寫快速排序法,編碼期間發現了一個死循環,後續終於找到原因,看來編碼不容易,還得多練手。
package myjava.ds.sort;
import java.util.Random;
public class QuickSort {
private int[] soureDataArray;
/**
* 方法getMiddle()主要以數組中第一個指針指向的值作爲排序中間值,然後對於整個數組進行一次比較和交換,
使得所有低於排序中間值的整數放在該值左側,高於中間值整數放在該值右側。
完成一次比較和交換的中止條件是低位指針和高位指針相等,此時,指針指向的數組位放入這個排序中間值
* @param testArray
* @param low
* @param high
* @return
*/
public int getMiddle(int[] testArray,int low,int high){
//獲取此次排序過程中,數組的最低位作爲排序中間值
int middleValue;
//獲取排序中間值,同時也對於該低位數據進行存儲,使得後續可以使用低位位置存儲找到的高位數據值
middleValue=testArray[low];
//最高標誌位和最低標誌位對比,直到最低位不高於最高位時,循環結束,
//循環結束,說明已完成一次數據比較,已使把數據按照中間值拆分爲2個部分,
//以中間值所在的數組位置爲基點,所有低於中間值數據,其位置在中間值的左側,高於中間值數據在中間值的右側
while(low<high){
//從高位開始比較,找到第一個小於排序中間值的數值,
//如果找到就進行移動,否則繼續移動高位指針
while(low<high&&testArray[high]>=middleValue){
high--;
}
//這裏的難點理解小於才能移動數據位置,如果等於,不能移動數據,會導致出現數值相等時,一直循環,無法結束整個排序過程
//移動數據到低位
testArray[low]=testArray[high];
//從低位開始比較,找到第一個大於排序中間值的數值,
//如果沒有找到,就繼續移動低位指針
while(low<high&&testArray[low]<=middleValue){
low++;
}
//滿足條件後,把找到數值移動到之前高位存儲位置,
//這裏的難點理解大於才能移動數據位置,如果等於,不能移動數據,會導致出現數值相等時,一直循環,無法結束整個排序過程
//完成一次高位和地位的數據交換
testArray[high]=testArray[low];
}
testArray[low]=middleValue;
return low;
}
//quickSort 主方法接口;
public void quickSort(int[] testArray,int low,int high){
if(low<high){
int middleInt;
middleInt=getMiddle(testArray, low, high);
quickSort(testArray,low,middleInt-1);
quickSort(testArray,middleInt+1,high);
}
}
public int[] getArray(){
return soureDataArray;
}
public void pirntArray(int[] arrayInt){
int count=arrayInt.length;
int i=0;
System.out.println("ArrayValue print:");
while(count>0){
System.out.print(arrayInt[i]+" ");
i++;
count--;
if((count%10)==0){
System.out.println(" ");
}
}
}
/**
* 該模擬方法完成數據的模擬,產生出指定長度的整型數組
* @param size
*/
public void simulateData(int size){
int count=size;
int beginSize=0;
int tempIntvalue;
if(count>0){
Random dataRandom= new Random();
soureDataArray = new int[count];
while(count>0){
tempIntvalue=dataRandom.nextInt(1000);
soureDataArray[beginSize]=Math.abs(tempIntvalue);
beginSize++;
count--;
}
}
}
public static void main(String args[]){
QuickSort quickSort= new QuickSort();
quickSort.simulateData(100000);
int[] testArray=quickSort.getArray();
//排序之前先打印一下
//quickSort.pirntArray(testArray);
//long begintime=System.nanoTime();
long begintime=System.currentTimeMillis();
quickSort.quickSort(testArray, 0, testArray.length-1);
long endtime=System.currentTimeMillis();
System.out.println("Arraysize :"+testArray.length+" Sort Time:"+(endtime-begintime)+"ms");
quickSort.simulateData(500000);
testArray=quickSort.getArray();
begintime=System.currentTimeMillis();
quickSort.quickSort(testArray, 0, testArray.length-1);
endtime=System.currentTimeMillis();
System.out.println("Arraysize :"+testArray.length+" Sort Time:"+(endtime-begintime)+"ms");
quickSort.simulateData(1000000);
testArray=quickSort.getArray();
begintime=System.currentTimeMillis();
quickSort.quickSort(testArray, 0, testArray.length-1);
endtime=System.currentTimeMillis();
System.out.println("Arraysize :"+testArray.length+" Sort Time:"+(endtime-begintime)+"ms");
//排序之後再打印一下
//quickSort.pirntArray(testArray);
}
}