快速排序(java排序)

基本思想:选择一个基准元素,通常选择第一个元素或者最后一个元素,通过一趟扫描,将待排序列分成两部分,一部分比基准元素小,一部分大于等于基准元素,此时基准元素在其排好序后的正确位置,然后再用同样的方法递归地排序划分的两部分。

public class QuickSort {
	public int getMiddle(int[] list,int low,int high)
	{
		int temp = list[low];
		while(low < high)
		{
			while(low < high && list[high] >= temp)
			{
				high--;
			}
			list[low] = list[high];
			while(low < high && list[low] <= temp)
			{
				low++;
			}
			list[high] = list[low];
		}
		list[low] = temp;
		return low;
	}
	
	public int[] quickSort(int[] list,int low,int high)
	{
		if(low < high)
		{
			int middle = getMiddle(list,low,high);
			quickSort(list, low, middle);
			quickSort(list, middle+1, high);
		}
		return list;
	}
	public static void main(String args[])
	{
		int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99,
				98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
		int[] result = new QuickSort().quickSort(a, 0, a.length-1);
		for(int i=0;i<result.length;i++)
		{
			System.out.println(result[i]);
		}
	}
}


你可能感兴趣的:(java,list,String,Class)