诚求用C语言编一个实现常见排序算法的代码。
发布网友
发布时间:2022-11-09 09:03
我来回答
共2个回答
热心网友
时间:2023-11-17 19:46
快速排序,给出两种写法,
写法一:分组和排序放在一起
#include <stdio.h>
void Qsort(int start,int length,int a[])
{
int x = a[start];
int i,j;
i = start;
j = length -1;
while(i < j)
{
if(x < a[j])
j--;
else if(x > a[j])
{
a[i] = a[j];
a[j] = x;
i++;
}
else if(x < a[i])
{
a[j] = a[i];
a[i] = x;
j--;
}
else
i++;
}
if(start < length-1)
{
Qsort(start,i,a);
Qsort(i+1,length,a);
}
}
void main()
{
int a[10] = {8,7,3,4,2,9,11,6,5,10};
int i;
Qsort(0,10,a);
for(i = 0;i < 10;i++)
printf("%d ",a[i]);
}
写法二:分别定义分组函数和排序函数
#include <stdio.h>
int partitiont(int start,int length,int a[])
{
int x = a[start];
int i,j;
i = start;
j = length -1;
while(i < j)
{
if(x < a[j])
j--;
else if(x > a[j])
{
a[i] = a[j];
a[j] = x;
i++;
}
else if(x < a[i])
{
a[j] = a[i];
a[i] = x;
j--;
}
else
i++;
}
return i;
}
void Qsort(int start,int length,int a[])
{
int q = partition(start,length,a);
if(start < length-1)
{
Qsort(start,q,a);
Qsort(q+1,length,a);
}
}
void main()
{
int a[10] = {8,7,3,4,2,9,11,6,5,10};
int i;
Qsort(0,10,a);
for(i = 0;i < 10;i++)
printf("%d ",a[i]);
}
热心网友
时间:2023-11-17 19:46
快速排序,给出两种写法,
写法一:分组和排序放在一起
#include <stdio.h>
void Qsort(int start,int length,int a[])
{
int x = a[start];
int i,j;
i = start;
j = length -1;
while(i < j)
{
if(x < a[j])
j--;
else if(x > a[j])
{
a[i] = a[j];
a[j] = x;
i++;
}
else if(x < a[i])
{
a[j] = a[i];
a[i] = x;
j--;
}
else
i++;
}
if(start < length-1)
{
Qsort(start,i,a);
Qsort(i+1,length,a);
}
}
void main()
{
int a[10] = {8,7,3,4,2,9,11,6,5,10};
int i;
Qsort(0,10,a);
for(i = 0;i < 10;i++)
printf("%d ",a[i]);
}
写法二:分别定义分组函数和排序函数
#include <stdio.h>
int partitiont(int start,int length,int a[])
{
int x = a[start];
int i,j;
i = start;
j = length -1;
while(i < j)
{
if(x < a[j])
j--;
else if(x > a[j])
{
a[i] = a[j];
a[j] = x;
i++;
}
else if(x < a[i])
{
a[j] = a[i];
a[i] = x;
j--;
}
else
i++;
}
return i;
}
void Qsort(int start,int length,int a[])
{
int q = partition(start,length,a);
if(start < length-1)
{
Qsort(start,q,a);
Qsort(q+1,length,a);
}
}
void main()
{
int a[10] = {8,7,3,4,2,9,11,6,5,10};
int i;
Qsort(0,10,a);
for(i = 0;i < 10;i++)
printf("%d ",a[i]);
}
热心网友
时间:2023-11-17 19:47
#include<stdio.h>
main()
{
int i,j,a[100],t,n,sum=0;
printf("How many numbers do you want to order?\n");
scanf("%d",&n); //输入你想排的数的个数
printf("please input %d numbers to order:\n",n);
for(j=1;j<=n;j++)
scanf("%d",&a[j-1]);
for(i=1;i<n;i++)
for(j=n-1;j>0;j--)
if(a[j-1]>a[j])
{
t=a[j-1];
a[j-1]=a[j];
a[j]=t;
sum++; //统计循环次数
}
for(j=1;j<=n;j++)
printf("%d ",a[j-1]);
printf("\n");
printf("Total cycle is %d",sum); //输出循环次数
getch();
}
热心网友
时间:2023-11-17 19:47
#include<stdio.h>
main()
{
int i,j,a[100],t,n,sum=0;
printf("How many numbers do you want to order?\n");
scanf("%d",&n); //输入你想排的数的个数
printf("please input %d numbers to order:\n",n);
for(j=1;j<=n;j++)
scanf("%d",&a[j-1]);
for(i=1;i<n;i++)
for(j=n-1;j>0;j--)
if(a[j-1]>a[j])
{
t=a[j-1];
a[j-1]=a[j];
a[j]=t;
sum++; //统计循环次数
}
for(j=1;j<=n;j++)
printf("%d ",a[j-1]);
printf("\n");
printf("Total cycle is %d",sum); //输出循环次数
getch();
}