C语言排序问题,急~C语言或C++均可。
发布网友
发布时间:2022-10-09 15:18
我来回答
共2个回答
热心网友
时间:2023-11-24 11:28
/*各种排序操作*/
#include<stdio.h>
#define MAX 100 /* 函数声明 */
void charu_sort(int d[],int n);
void jiaohuan_sort(int d[],int n);
void xuanze_sort(int d[],int n);
void xier_sort(int d[],int n,int dd[],int t); /* 希尔排序 */
void kuaisu_sort(int d[],int min,int max); /* 快速排序 */
void i_sort(int d[ ],int n); /* 堆排序 */
/* 主函数*/
void main( )
{ int d[MAX],dd[MAX];
int s,q,t,n,k,m,ch;
/*线性表结点数*/
printf("How many nodes?\n");
scanf("%d",&n);
/*输入线性表各结点*/
for(s=0;s<n;s++)
{printf("Input No %d value\n",s+1);<br> scanf("%d",&d[s]);<br> }
/*选择排序方法*/
printf("Please input your choice(1-6):");
scanf("%d",&ch);
switch(ch)
{ case 1:
printf("This is straight insertion sort!\n");
s=1;
charu_sort(d,n);
getch();
break;
case 2:
printf("This is bubble sort!\n");
jiaohuan_sort(d,n);
getch();
break; case 3:
printf("This is simple selection sort!\n");
xuanze_sort(d,n);
getch();
break;
case 4:
printf("This is shell sort!\n");
/*initiate the array */
s=n;q=0;
while(s>1)
{
dd[q++]=s/2;
s=s/2;
}
/*the last increment is 1*/
xier_sort(d,n,dd,q);
getch();
break;
case 5:
printf("This is quick sort!\n");
kuaisu_sort(d,0,n-1);
getch();
break;
case 6:
printf("This is heap sort!\n");
i_sort(d,n);
getch();
break;
default:
printf("1--------------straight insertion sort!\n");
printf("2--------------bubble sort!\n");
printf("3--------------simple selection sort!\n");
printf("4--------------shell sort!\n");
printf("5--------------quick sort!\n");
printf("6--------------heap sort!\n");
return;
}
/*输出排序后的线性表*/
for(s=0;s<n-1;s++)
printf("%d,",d[s]);
printf("%d",d[s]);
printf("\n");
getch();
} /*各排序方法函数说明*//*直接插入排序*/void charu_sort(int d[],int n)
{
int t,q,s=1;
while(s<n)
{ t=d[s]; /*t is a temporary variable*/
for(q=s-1;q>=0&&t<d[q];q--)
d[q+1]=d[q]; /*the node move right*/
d[q+1]=t; /*if the value of q is -1,it does not exist a number
whose value is smaller than t,else,it must exist q+1 numbers whose value is
smaller than t at least*/
s++;
}
}
/*冒泡排序(交换排序)*/
void jiaohuan_sort(int d[],int n)
{
int q,s,t,m=0;
while(m<n-1)/*if m is equal to n-1,end the cycle*/
{ q=n-1;
for(s=n-1;s>=m+1;s--)
if(d[s]<d[s-1])
{ t=d[s];
d[s]=d[s-1];
d[s-1]=t;
q=s;/*j note the current location where the exchanging
happens this time*/
}
m=q;
}
}/*选择排序*/
void xuanze_sort(int d[],int n)
{
int s,t,q,m=0;
while(m<n-1)/*if m is equal to n-1,end the cycle*/
{ q=n-1;
for(s=n-1;s>=m+1;s--)
if(d[s]<d[s-1])
{ t=d[s];
d[s]=d[s-1];
d[s-1]=t;
q=s;/*j note the current location where the exchanging
happens this time*/
}
m=q;
}
}
/* 希尔排序 */void xier_sort(int d[],int n,int dd[],int t)
{ int s,x,k,h;
int y;
for(s=0;s<t;s++)
{h=dd[s];/*select increment*/<br> for(x=h;x<n;x++)/*j point out the location of the number inserted*/<br> { y=d[x];<br> for(k=x-h;k>=0&&y<d[k];k-=h)<br> d[k+h]=d[k];/*move back*/<br> d[k+h]=y;<br> }
}
}
/* 快速排序 */void kuaisu_sort(int d[],int min,int max)
{ int head,tail;
int t;
if(min<max)
{ head=min;
tail=max;
t=d[head];
while(head!=tail)
{ while(head<tail&&d[tail]>=t)
tail--;
if(head<tail)
d[head++]=d[tail];/*exchange the nodes*/
while(head<tail&&d[head]<=t)
head++;
if(head<tail)
d[tail--]=d[head];
}
d[head]=t;
kuaisu_sort(d,min,head-1);
kuaisu_sort(d,tail+1,max);
}
} /*堆调整函数*/void itiaozheng(int d[],int s,int n)
{ int q;
int t;
t=d[s];
while((q=2*s+1)<n)/*it has left child*/
{ if(q<n-1&&d[q]<d[q+1]) q++;
if(t<d[q])
{ d[(q-1)/2]=d[q];
s=q;
}
else
break;
}
d[(q-1)/2]=t;/*put it in the final location*/
}
/* 堆排序 */void i_sort(int d[],int n)
{ int s;int t;
s=(n-1)/2;
while(s>=0)
{ itiaozheng(d,s,n);
s--;
}
s=n-1;
while(s>0)
{ t=d[0];/*exchange the nodes*/
d[0]=d[s];
d[s]=t;
itiaozheng(d,0,s);/*after exchangeing£¬readjust the heap*/
s--;
}
}
热心网友
时间:2023-11-24 11:28
#include "stdio.h"
#include "stdlib.h"
#define N 1000
void main()
{
int i,j,k,l,x[N],p[N];
for(i=1;i<=N;i++){
x[i]=rand();
}
for(i=1;i<=N;i++){
for(j=i;j<=N;j++){
if(j==N){
break;
}else{
if(x[i] > x[j+1]){
l = x[i];
x[i] = x[j+1];
x[j+1] = l;
}
}
}
}
for(i=1;i<=N;i++){
printf("%d\t",x[i]);
}
} C语言写出来的!!不知道你那些数是什么,我用随机产生的数进行排序!!排序方法是冒泡排序法,弄了我那么多时间!!不知道你排序那是1000还是100,要100的话,就将#define N 1000 那里的1000改成100就行了!!