JAVA数组的快速排序

package test;

public class ArrayTest {
	
	//交换数组元素
	private static void swop(int[] array ,int low,int high) {
		
		int temp=array[low];
		array[low]=array[high];
		array[high]=temp;
	}

	//快速排序方法
	private static void arrayRank(int[] array,int lowIndex,int highIndex) {
		int low=lowIndex;
		int high=highIndex;
		int temp;
		if(lowIndex<highIndex) {
			while (low<=high) {
				temp = array[(lowIndex + highIndex) / 2];
				while ((low<highIndex)&&(array[low]<temp)) {
					++low;
					
				}
				while ((high>lowIndex)&&(array[high]>temp)) {
					--high;
				}
				if(low<=high) {
					swop(array, low, high);
					++low;
					--high;
				}
			}
			if(low<highIndex) {
				
				arrayRank(array, low, highIndex);
			}
			if(high>lowIndex) {
				arrayRank(array, lowIndex, high);
			}
		}
		
	}
	
	
	public static void main(String[] args) {
		
		int [] array= {9,5,7,4,20,2,6,8};
		arrayRank(array, 0, array.length-1);
		for (int i : array) {
		System.out.println(i);
	}	
		
		
	}
	
	
}

猜你喜欢

转载自blog.csdn.net/qq_38146131/article/details/82228838