栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

基于C语言的快速排序

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于C语言的快速排序

C语言快速排序

快速排序算法的基本思想是基于分治法的:在待排序表L[1…n]中任取一个元素pivot作为枢轴(基准),通过一趟排序将待排序表划分为独立的两部分L[1…k-1]和L[k+1…n],使L[1…k-1]中的所有元素小于等于pivot,L[k+1…n]中的所有元素大于等于pivot,最后pivot放在L[k]上,这个过程称为一趟快速排序。然后分别递归两个子表重复上述过程,直至每部分只有一个元素或空为止。

#define _CRT_SECURE_NO_WARNINGS
#include
#include
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;

}
void QuickSort(int a[],int low,int high)
{
	if (low < high)
	{
		int index = Partition(a, low, high);
		QuickSort(a, low, index - 1);
		QuickSort(a, index + 1, high);
	}

}
int main()
{

	int n;
	scanf("%d", &n);
	int* a = (int*)malloc(sizeof(int) * n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
	}
	
	QuickSort(a, 0, n - 1);
	for (int i = 0; i < n; i++)
	{
		printf("%dt", *(a + i));
	}
	return EXIT_SUCCESS;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1030127.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号