4个回答
展开全部
#include <stdio.h>
int partions(int l[],int low,int high)
{
int prvotkey=l[low];
l[0]=l[low];
while (low<high)
{
while (low<high&&l[high]>=prvotkey)
--high;
l[low]=l[high];
while (low<high&&l[low]<=prvotkey)
++low;
l[high]=l[low];
}
l[low]=l[0];
return low;
}
void qsort(int l[],int low,int high)
{
int prvotloc;
if(low<high)
{
prvotloc=partions(l,low,high); //将第一次排序的结果作为枢轴
qsort(l,low,prvotloc-1); //递归调用排序 由low 到prvotloc-1
qsort(l,prvotloc+1,high); //递归调用排序 由 prvotloc+1到 high
}
}
void quicksort(int l[],int n)
{
qsort(l,1,n); //第一个作为枢轴 ,从第一个排到第n个
}
void main()
{
int a[11]={0,2,32,43,23,45,36,57,14,27,39};
for (int b=1;b<11;b++)
printf("%3d",a[b]);
printf("\n");
quicksort(a,11);
for(int c=1;c<11;c++)
printf("%3d",a[c]);
}
int partions(int l[],int low,int high)
{
int prvotkey=l[low];
l[0]=l[low];
while (low<high)
{
while (low<high&&l[high]>=prvotkey)
--high;
l[low]=l[high];
while (low<high&&l[low]<=prvotkey)
++low;
l[high]=l[low];
}
l[low]=l[0];
return low;
}
void qsort(int l[],int low,int high)
{
int prvotloc;
if(low<high)
{
prvotloc=partions(l,low,high); //将第一次排序的结果作为枢轴
qsort(l,low,prvotloc-1); //递归调用排序 由low 到prvotloc-1
qsort(l,prvotloc+1,high); //递归调用排序 由 prvotloc+1到 high
}
}
void quicksort(int l[],int n)
{
qsort(l,1,n); //第一个作为枢轴 ,从第一个排到第n个
}
void main()
{
int a[11]={0,2,32,43,23,45,36,57,14,27,39};
for (int b=1;b<11;b++)
printf("%3d",a[b]);
printf("\n");
quicksort(a,11);
for(int c=1;c<11;c++)
printf("%3d",a[c]);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
using namespace std;
void quicksort(int n[], int left, int right);
int partition(int n[], int left, int right);
int main()
{
int as[5] = {23, 43, 13, 56, 68};
quicksort(as, 0, 5);
for(int i = 0; i != 5; i++)
{
cout << as[i] << ' ';
}
}
void quicksort(int n[], int left, int right)
{
int dp;
if (left < right)
{
dp = partition(n, left, right);
quicksort(n, left, dp - 1);
quicksort(n, dp + 1, right);
}
}
int partition(int n[], int left, int right)
{
int lo,hi,pivot,t;
pivot = n[left];
lo = left - 1;
hi = right + 1;
while(lo+1 != hi)
{
if(n[lo + 1] <= pivot)
lo++;
else if(n[hi - 1] > pivot)
hi--;
else
{
t = n[lo + 1];
n[++lo] = n[hi - 1];
n[--hi] = t;
}
}
n[left] = n[lo];
n[lo] = pivot;
return lo;
}
//解释你在网上搜,很容易找到
using namespace std;
void quicksort(int n[], int left, int right);
int partition(int n[], int left, int right);
int main()
{
int as[5] = {23, 43, 13, 56, 68};
quicksort(as, 0, 5);
for(int i = 0; i != 5; i++)
{
cout << as[i] << ' ';
}
}
void quicksort(int n[], int left, int right)
{
int dp;
if (left < right)
{
dp = partition(n, left, right);
quicksort(n, left, dp - 1);
quicksort(n, dp + 1, right);
}
}
int partition(int n[], int left, int right)
{
int lo,hi,pivot,t;
pivot = n[left];
lo = left - 1;
hi = right + 1;
while(lo+1 != hi)
{
if(n[lo + 1] <= pivot)
lo++;
else if(n[hi - 1] > pivot)
hi--;
else
{
t = n[lo + 1];
n[++lo] = n[hi - 1];
n[--hi] = t;
}
}
n[left] = n[lo];
n[lo] = pivot;
return lo;
}
//解释你在网上搜,很容易找到
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
冒泡排序就挺快的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
void quicksort(int a[],int low,int high)
{
int t,i,j;
i=low;
j=high;
if(low<high)
{
t=a[low];
while(i!=j)
{
while(a[j]>t&&i<j)j--;
if(i<j)
{
a[i]=a[j];
i++;
}
while(a[i]<t&&i<j)i++;
if(i<j)
{
a[j]=a[i];
j--;
}
}
a[i]=t;
quicksort(a,low,i-1);
quicksort(a,i+1,high);
}
}
void main()
{
int i,a[20001],n;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
quicksort(a,1,n);
for(i=1;i<=n;i++)
{
if(i!=n)printf("%d ",a[i]);
else printf("%d\n",a[i]);
}
}
return 0;
}
void quicksort(int a[],int low,int high)
{
int t,i,j;
i=low;
j=high;
if(low<high)
{
t=a[low];
while(i!=j)
{
while(a[j]>t&&i<j)j--;
if(i<j)
{
a[i]=a[j];
i++;
}
while(a[i]<t&&i<j)i++;
if(i<j)
{
a[j]=a[i];
j--;
}
}
a[i]=t;
quicksort(a,low,i-1);
quicksort(a,i+1,high);
}
}
void main()
{
int i,a[20001],n;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
quicksort(a,1,n);
for(i=1;i<=n;i++)
{
if(i!=n)printf("%d ",a[i]);
else printf("%d\n",a[i]);
}
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询