快速排序的两个JAVA实现

本文详细介绍了快速排序算法的两种实现方式,包括标准快速排序和改进后的快速排序算法,通过对比实验证明了它们在不同场景下的性能差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

快速排序的两个实现, 差别在分段函数不同的分段函数

import java.util.Arrays;

public class SortTest {
	public static void main(String[] args) {
		Integer[] list = new Integer[100000];
//		Integer[] list = new Integer[10000];
		for (int i = 0; i < list.length; i++) {
			list[i] = (int) (Math.random() * 100000);
		}
		long start = System.currentTimeMillis();
//		maopao(list);
		 quickSort(list,0,list.length);
//		QuickSort.sort(list, 0, list.length - 1);
		long end = System.currentTimeMillis();
		System.out.println(end - start);

		// list = new Integer[5000];
		// for(int i=0; i<list.length;i++)
		// {
		// list[i] = (int)(Math.random()*100000);
		// }
		// start = System.currentTimeMillis();
		// maopao(list);
		// Arrays.sort(list);
		// end = System.currentTimeMillis();
		// System.out.println(end - start);

		// list = new Integer[10000];
		// for(int i=0; i<list.length;i++)
		// {
		// list[i] = (int)(Math.random()*100000);
		// }
		// start = System.currentTimeMillis();
		// maopao(list);
		// end = System.currentTimeMillis();
		// System.out.println(end - start);

//		 System.out.println(Arrays.deepToString(list));
	}

	public static <T extends Comparable<T>> void maopao(T[] list) {
		for (int i = 0; i < list.length; i++) {
			for (int j = i + 1; j < list.length; j++) {
				if (list[i].compareTo(list[j]) > 0) {
					swap(list, i, j);
				}
			}
		}

	}

	/**
	 * 把数组安装一个数 分成两部分
	 * 
	 * @param list
	 */
	public static <T extends Comparable<T>> void quickSort(T[] list, int from,
			int to) {
		if (from == to) {
			return;
		}
		int position = partition(list, from, to);

		quickSort(list, from, position);
		quickSort(list, position + 1, to);

	}

	/**
	 * 
	 * @param list
	 */
	public static <T extends Comparable<T>> int partition(T[] list, int from,
			int to) {
		int position = from;
		
		int mid = from + (to - from)/2;
		T key = list[mid];
		SortTest.swap(list, from, mid);
		
		for (int i = from + 1; i < to; i++) {
			if (key.compareTo(list[i]) > 0) {
				//把中值交换到比较位置
				swap(list, position, i);
				//移回到原来位置的下一个
				swap(list, ++position, i);

			}
		}
		return position;
	}

	public static <T> void swap(T[] list, int left, int right) {
		T temp = list[left];
		list[left] = list[right];
		list[right] = temp;
	}

}

class QuickSort {


	private static <T extends Comparable<T>> int partition(T[] array, int low,
			int high) {

		int stack = low;
		int position = low + (high - low)/2;
		T key = array[position];
		SortTest.swap(array, low, position);
		while (low < high) {
			// 从后向前搜索比key小的值
			while (array[high].compareTo(key) >= 0 && high > low) {
				--high;
			}
			// 从前向后搜索比key大的值,比key大的放右边
			while (array[low].compareTo(key) <=0 && high > low) {
				++low;
			}
			if(low<high)
			{
				SortTest.swap(array, low, high);	
			}			
			// System.out.println(low + "," + high);
		}
		SortTest.swap(array, stack, low);	
		// System.out.println(Arrays.toString(array));
		return high;
	}

	/**
	 * 快速排序
	 * 
	 * @param arry
	 * @return
	 */
	public static <T extends Comparable<T>> void sort(T[] array, int low,
			int high) {
		if (low >= high) {
			return;
		}
		// 完成一次单元排序
		int index = partition(array, low, high);
		// 对左边单元进行排序
		sort(array, low, index - 1);
		// 对右边单元进行排序
		sort(array, index + 1, high);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值