首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

那位前辈帮小弟我看看程序错在什么地方,内部排序有关问题

2012-03-06 
那位前辈帮我看看程序错在什么地方,内部排序问题。编译,连接都没有错误但是运行结果错误//---符号常量-类型

那位前辈帮我看看程序错在什么地方,内部排序问题。
编译,连接都没有错误但是运行结果错误


//---符号常量-类型定义-头文件-----
#include     <stdio.h>
#define   max   100

//-----------------顺序表的直接插入排序-------------
void   InsertSort(int   a[],int   n)
{
int   i,j;
for(i=2;i <=n;++i)
if(a[i] <a[i-1])
{
a[0]=a[i];
a[i]=a[i-1];
for(j=i-2;a[0] <a[j];--j)
                              a[j+1]=a[j];
a[j+1]=a[0];
}

}//InsertSort
//-------------起泡排序-------------------
void   BubbleSort(int   a[],int   n){
int   i,j,flag;
flag=1;
for(i=1;(i <n-1)&&flag;i++)
{
flag=0;
for(j=n-1;j> =i;j--)
if(a[j] <a[j-1])
{
a[0]=a[j-1];
a[j-1]   =a[j];
a[j]=a[0];
flag=1;
}
}//
}//   BubbleSort
//-------------------一趟快速排序-----------
int   Partition(int   a[],int   low,int   high){
          int   temp;
  a[0]=     a[low];
temp=   a[high]   ;
while(low <high)
{
while(low   <   high     &&     a[high]> =temp)--high;
a[low]=a[high];
while(low   <   high     &&   a[low] <=temp)++low;
a[high]=a[low];
}
a[low]=a[0];
return   low;
}//Partition
//-----------递归调用Partition--------
void   Qsort(int   a[],int   low,int   high){

int   m;
if(low <high){
m=   Partition(a,low,high);
Qsort(a,low,m-1);
Qsort(a,m+1,high);
}
}//QSort
//------------
void   Quicksort(int   a[],int   n){

Qsort(a,1,n);
}//Quicksort
//------主函数------------
void   main()
{int   a[max];
int   t,i,n;
        printf( "how   many   nodes   if   you   want   to   create   a   list?\n ");
scanf( "%d ",&n);
        printf( "If   you   want   create   a   list,input   n   numbers:   \n ");
for(i=1;i <=n;i++)
scanf( "%d ",&a[i]);
printf( "\n ");

printf( "     1.This   is   the   straight   InsertSort   sort.     \n ");
printf( "     2.This   is   the       Bubble   Sort   .\n ");
        printf( "     3.This   is   the     Quicksort.   \n ");
        printf( "     Please   choose     a     kind     of     sort:\n ");
                    scanf(   "%d "   ,   &t   );
switch       (t)
      {
case   1:   InsertSort(a,n);       break;
case   2:   BubbleSort(a,n);       break;
case   3:     Quicksort(   a,n);     break;
default:   printf( "   The   error!\n ");
      }

        printf( "The   result   of   Sort   is:\n ");


        for   (i=1;i <=n;i++);
        printf( "%d ",a[i]);
printf( "\n ");
}

[解决办法]
printf( "The result of Sort is:\n ");
for (i=1;i <=n;i++) //去掉这里的分号
printf( "%d ",a[i]);
printf( "\n ");

[解决办法]
冒泡这样吧。。。
//下标从0开始:‘
//-------------起泡排序-------------------
void BubbleSort(int a[],int n){
int i,j,flag;
//flag=1;
for(i=0;i <n;i++)
{
//flag=0;
for(j=i+1;j <n;j++)
if(a[i] <a[j])
{
flag=a[i];
a[i] =a[j];
a[j]=flag;
//flag=1;
}
}//
}// BubbleSort
[解决办法]
while(1)
{
fflush(stdin);//加头文件,stdlib.h
ch=getchar();
if(ch== 'q ')
break;
switch(ch)
{
default 后面的return 0改为 break;其他一样,
}

}

热点排行