程序员

java实现快速排序算法

/*
* 快速排序算法,java实现
* 假设数组a的长度n
* 1,设置变量low为0,hight为n,midValue为a[0],i=low,j=hight
* 2,hight--,从hight位置开始找小于midValue的值
* 3,low++,从low位置开始找大于midValue的值
* 4,如果low<hight,交换a[low]和a[hight]
* 5,循环3,4
* 6,交换中枢,a[left]<-->a[j]
* 7,判断左边界,left<j,是则递归(left,j)
* 8,判断右边界,i<right,是则递归(i,right)
*/
package org.test;

public class QuickSort {

/**
* @param args
*/
public static int [] SORT = new int[]{};
public static void main(String[] args) {
test();
}
public static void test(){
for (int i : SORT){
System.out.print(i);
System.out.print("-");
}
partion(SORT,0,SORT.length);
System.out.println("=================================================");
for (int i : SORT){
System.out.print(i);
System.out.print("-");
}
}
public static void partion(int [] sorted, int low, int hight){
System.out.println();
for (int i : SORT){
System.out.print(i);
System.out.print("-");
}
System.out.println();
if (sorted.length<2 || low<0 || hight<0 || low>=hight)return;
int left = low;
int right = hight;
int first = sorted[low];
while (true){
while (low<(--hight) && sorted[hight]>first);
while ((++low)<hight && sorted[low]<first);

if (low>=hight)break;

first = sorted[hight];
sorted[hight] = sorted[low];
sorted[low] = first;

}
//交换中枢
sorted[left] = sorted[hight];
sorted[hight] = first;


if (left<hight)partion(sorted,left,hight);

if (right>low)partion(sorted,low,right);
}

}

你可能感兴趣的:(java,算法,J#)