快速排序手撕简洁版

一趟划分
挖坑法

int Partition(int a[], int low, int high)
{
	int pivot = a[low];    //以低位为基准
	while (low < high)
	{
		while (low < high && a[high] >= pivot)
			--high;
		a[low] = a[high];
		while (low < high && a[low] <= pivot)
			++low;
		a[high] = a[low];
	}
	a[low] = pivot;
	return low;
}

左右指针法

int Partition1(int a[], int low, int high)
{
	int &pivot = a[low];    //以低位为基准,若想以高位为基准,
	while (low < high)        //只需要交换(1)(2)两个while循环先后顺序
	{
		while (low < high && a[high] >= pivot)    //(1)
			--high;
		while (low < high && a[low] <= pivot)    //(2)
			++low;
		
		swap(a[low], a[high]);
	}
	swap(a[low], pivot);
	return low;
}

前后指针法

int Partition2(int a[], int low, int high)
{
	int pivot = a[high]; // 以高位为基准
	int cur = low;
	int pre = cur - 1;

	while (cur < high)
	{
		while (a[cur] < pivot && ++pre != cur)
			swap(a[cur], a[pre]);
		++cur;
	}
	swap(a[++pre], a[high]);

	return pre;
}
 

递归实现

void quickSort(int a[], int low, int high)
{
	if (low < high)
	{
		int pivotPos = Partition2(a, low, high);
		quickSort(a, low, pivotPos - 1);
		quickSort(a, pivotPos + 1, high);
	}
}

非递归实现

//基本思想:边界入栈,一次划分
void quickSort1(int a[], int low, int high)
{
	assert(a);

	stack<int> s;
	s.push(low);
	s.push(high);

	while (!s.empty())
	{
		int low = s.top();
		s.pop();
		int high = s.top();
		s.pop();

		int pivotPos = Partition(a, low, high);

		if (pivotPos - 1 > low)
		{
			s.push(low);
			s.push(pivotPos - 1);
		}

		if (pivotPos + 1 < high)
		{
			s.push(pivotPos + 1);
			s.push(high);
		}
	}
}

 

### 快速排序算法的代码实现 快速排序是一种基于分治法的高效排序算法,其核心思想是通过选取一个基准值(pivot),将数组划分为两部分:一部分小于等于基准值,另一部分大于基准值。随后递归地对这两部分进行相同的操作。 以下是使用 Java 实现快速排序的具体代码: ```java public class QuickSort { public static void main(String[] args) { int[] arr = {8, 4, 7, 12, 3, 6, 9}; quickSort(arr, 0, arr.length - 1); System.out.println("Sorted array:"); for (int num : arr) { System.out.print(num + " "); } } public static void quickSort(int[] arr, int low, int high) { if (low < high) { // Partitioning index int pi = partition(arr, low, high); // Recursively sort elements before and after partition quickSort(arr, low, pi - 1); // 左边排序 [^2] quickSort(arr, pi + 1, high); // 右边排序 } } private static int partition(int[] arr, int low, int high) { int pivot = arr[high]; // 基准值设为最后一个元素 [^1] int i = (low - 1); // 较小元素索引初始化 for (int j = low; j < high; j++) { // 当前元素小于或等于 pivot if (arr[j] <= pivot) { i++; // 交换 arr[i] 和 arr[j] swap(arr, i, j); } } // 交换 arr[i+1] 和 arr[high] (即 pivot) swap(arr, i + 1, high); return i + 1; } private static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } ``` #### 解析 1. **`quickSort` 方法** 这是一个递归方法,用于执行快速排序的核心逻辑。它接收三个参数:待排序数组 `arr`、起始索引 `low` 和结束索引 `high`。如果 `low` 小于 `high`,则继续划分并递归处理左右子数组 。 2. **`partition` 方法** 此方法负责选定基准值并将数组划分为两部分。基准值通常选作当前分区中的最后一个元素 [^1]。遍历过程中,所有小于等于基准值的元素会被移动到左侧,最终返回基准值所在位置的索引。 3. **`swap` 方法** 提供了一个简单的工具来交换数组中的两个元素。 --- ### 性能分析 - 时间复杂度:平均情况下为 \(O(n \log n)\),最坏情况为 \(O(n^2)\)[^3]。 - 空间复杂度:由于递归栈的存在,空间复杂度为 \(O(\log n)\) 到 \(O(n)\) 不等 [^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值