(C语言)任意输入一组无序数据,使用快速排序将其调整为一个递增数列并输出。

2023-12-13 13:19:31
#include<stdio.h>
#include<malloc.h>
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 pivot_postition = partition(a,low,high);
 		quicksort(a,low,pivot_postition-1);
 		quicksort(a,pivot_postition+1,high);
 		
	 }
 }
 int main()
 {
 	int length;
 	scanf("%d",&length);
 	int *a = (int *) malloc((length) * sizeof(int));
 	for(int i = 0;i < length; i ++)
 		scanf("%d",&a[i]);
 	quicksort(a,0,length);
 	for(int i = 0;i < length ;i ++)
 		printf("%d ",a[i]);
 	return 0;
 }

?运行截图:

注:侵权可删?

文章来源:https://blog.csdn.net/m0_57214074/article/details/134840059
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。