首页 新闻 会员 周边

将快排算法添加随机选择数据,一直有错,希望大佬们帮忙看一下。

0
悬赏园豆:10 [待解决问题]

include<stdio.h>

include<time.h>

include<stdlib.h>

define n 5

//交换
void Swap(int arr[], int low, int high)
{
int temp;
temp = arr[low];
arr[low] = arr[high];
arr[high] = temp;
}

int Partition(int arr[], int low, int high)
{
int ch,base;
srand((unsigned)time(0));
ch=rand()%(high-low+1)+low;
base=arr[ch];
//int base = arr[low];
while(low < high)
{
while(low < high && arr[high] >= base)
{
high --;
}
Swap(arr, low, high);
while(low < high && arr[low] <= base)
{
low ++;
}
Swap(arr, low, high);
}
return low;
}
//分治
void QuickSort(int arr[], int low, int high)
{
if(low < high)
{
int base = Partition(arr, low, high);
QuickSort(arr, low, base - 1);
QuickSort(arr, base + 1, high);
}
}

int main()
{
int arr[n];
int i ,x, j;
x=n;
printf("请输入%d个整数:\n",x);
for(i = 0; i < n; i ++)
{
scanf("%d",&arr[i]);
}
printf("\n");
QuickSort(arr, 0, n-1);
for(j = 0; j < n; j ++)
{
printf("%4d",arr[j]);
}
return 0;
}

轻松玩编程的主页 轻松玩编程 | 初学一级 | 园豆:189
提问于:2020-03-06 21:17
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册