fw ->>>>>>> 2013年12月13日23:16:30 ->> quick_sort

本文详细介绍了使用C++实现快速排序算法的过程,包括分区函数partition的实现和递归调用快速排序函数的过程。通过实例代码展示如何对整型数组进行排序。

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

#include <iostream>
using namespace std;
int partition(int a[], int low, int high)
{
int pivot = high;
int first_high=low;


for (int i = low; i < high; ++i)
{
if (a[i] < a[pivot])
{
int tem = a[i];
a[i] = a[first_high];
a[first_high] = tem;


first_high++;
}
}
int tem2 = a[pivot];
a[pivot] = a[first_high];
a[first_high] = tem2;

return first_high;
}


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


int main()
{
int a[] = { 1, 23, 34, -5411, 111, -3333 };
//cout << partition(a, 0, 5) << endl;
quickSort(a, 0, 5);
for (int i = 0; i < 6; ++i)
{
cout << a[i] << " ";
}
system("pause");
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值