当前位置: 代码迷 >> C语言 >> [开源]明天考试,今天来几个程序
  详细解决方案

[开源]明天考试,今天来几个程序

热度:184   发布时间:2005-12-15 23:07:00.0
[开源]明天考试,今天来几个程序

//SelectSort
#include<stdio.h>

void SelectSort(int test[],int n)
{
int i,j,small;
int tmp;

for(i=0;i<n-1;i++)
{
small=i;
for(j=i+1;j<n;j++)
if(test[j]<test[small])
small=j;
if(small!=i)
{
tmp=test[i];
test[i]=test[small];
test[small]=tmp;
}
}
}

int main( )
{
int n,test[100];
int i;

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&test[i]);
SelectSort(test,n);
for(i=0;i<n;i++)
printf("%d ",test[i]);
printf("\n");

return 0;
}


//QuickSort
#include<stdio.h>

void QuickSort(int test[],int low,int high)
{
int i,j;
int tmp;

i=low;
j=high;
tmp=test[low];

while(i<j)
{

while(i<j && tmp<=test[j])
j--;
if(i<j)
{
test[i]=test[j];
i++;
}

while(i<j && tmp>test[i])
i++;
if(i<j)
{
test[j]=test[i];
j--;
}
}

test[i]=tmp;

if(i-1>low)
QuickSort(test,low,i-1);
if(high>j+1)
QuickSort(test,j+1,high);
}

int main( )
{
int test[100],n;
int i;

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&test[i]);
QuickSort(test,0,n-1);
for(i=0;i<n;i++)
printf("%d ",test[i]);
printf("\n");
return 0;
}


//InsertSort
#include<stdio.h>

void InsertSort(int test[],int n)
{
int i,j;
int tmp;

for(i=0;i<n-1;i++)
{
tmp=test[i+1];
j=i;
while(j>=0 && test[j]>tmp)
{
test[j+1]=test[j];
j--;
}
test[j+1]=tmp;
}
}

int main( )
{
int test[100],n;
int i;

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&test[i]);
InsertSort(test,n);
for(i=0;i<n;i++)
printf("%d ",test[i]);
printf("\n");
return 0;
}

//BubbleSort
#include<stdio.h>

void BubbleSort(int test[],int n)
{
int i,j,flag=1;
int tmp;

for(i=1;i<n && flag==1;i++)
{
flag=0;
for(j=0;j<n-i;j++)
if(test[j]>test[j+1])
{
flag=1;
tmp=test[j];
test[j]=test[j+1];
test[j+1]=tmp;
}
}
}

int main( )
{
int test[100],n;
int i;

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&test[i]);
BubbleSort(test,n);
for(i=0;i<n;i++)
printf("%d ",test[i]);
printf("\n");
return 0;
}




搜索更多相关的解决方案: int  test  考试  开源  

----------------解决方案--------------------------------------------------------
什么程序?
----------------解决方案--------------------------------------------------------
~~~~~~han
----------------解决方案--------------------------------------------------------

不解……


----------------解决方案--------------------------------------------------------
看起来累,最好来个目的先。
----------------解决方案--------------------------------------------------------

怎麽没写heapsort,shellsort和inordersort


----------------解决方案--------------------------------------------------------
几种排序嘛!
----------------解决方案--------------------------------------------------------
  相关解决方案