请各路英雄,女侠看看,这个快速排序有什么问题。
以下是小弟最近写的一个C实现的快速排序:
#include <stdio.h>
void main()
{
int i,a[8],j;
int quickSort(int a[],int low,int high);
printf("Please input 8 numbers :");
for(i=0;i<8;i++)
{
scanf_s("%d",&a[i]);
}
i=0;j=7;
quickSort(a,i,j);
printf("The result of quicksort is :");
for(i=0;i<7;i++)
printf("%d",a);
}
void quickSort(int a[],int low,int high)
{
int key;
int sort(int a[],int low ,int high);
if(low<high)
{
key=sort( a, low, high);
quickSort(a,low,key-1);
quickSort(a,key+1,high);
}
}
int sort(int a[],int low,int high)
{
int i,j;int key1=a[low],t;
i=low;j=high;
while(i<j)
{
do
{
if(a[j]<key1)
{
t=a[j];
a[j]=a[i];
a[i]=t;
break;
}
else
j--;
}while(i=j);
do
{
if(i>7)
{printf("出现异常!");break;}
if(a[i]>key1)
{
t=a[j];
a[j]=a[i];
a[i]=t;
break;
}
else
i++;
}while(i=j);
}
return(j);
}
这是运行时的错误描述:
Error1error C2371: 'quickSort' : redefinition; different basic type
!!!!!!!!!!!!!!!!!!!!~~~~~~~~~~~~~~~~~~~~~~~~~
求帮助瓦、还有 :i指针越界怎么巧妙的解决。
[解决办法]
你的qsort也太长了,这个是我写的,你可以参考一下。
void qsort(int a[], int begin, int end){ if (begin<end) { int b=begin; int e=end; int temp=a[b]; while(b<e) { while (b<e && a[e]>=temp) { e--; } a[b]=a[e]; while (b<e && a[b]<=temp) { b++; } a[e]=a[b]; } a[b]=temp; qsort(a, begin, e-1); qsort(a, e+1, end); } return; }
[解决办法]
俩人半斤八两,看看《算法导论》里的快排。
另外,C标准库有qsort,不用自己写。
[解决办法]
先
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express
右边Visual C++ 2010 Express下面的Select language...下拉选‘简体中文’,再按Install Now按钮
再参考
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\qsort.c
[解决办法]
#include <stdio.h>void quickSort(int a[],int low,int high);int sort(int a[],int low,int high);void main(){ int i,a[8],j; printf("Please input 8 numbers :"); for(i=0;i<8;i++) { scanf_s("%d",&a[i]); } i=0;j=7; quickSort(a,i,j); printf("The result of quicksort is :"); for(i=0;i<7;i++) printf("%d",a);}void quickSort(int a[],int low,int high){ int key; int sort(int a[],int low ,int high); if(low<high) { key=sort( a, low, high); quickSort(a,low,key-1); quickSort(a,key+1,high); }}int sort(int a[],int low,int high){ int i,j;int key1=a[low],t; i=low;j=high; while(i<j) { do { if(a[j]<key1) { t=a[j]; a[j]=a[i]; a[i]=t; break; } else j--; }while(i=j); do { if(i>7) {printf("出现异常!");break;} if(a[i]>key1) { t=a[j]; a[j]=a[i]; a[i]=t; break; } else i++; }while(i=j); } return(j);}
[解决办法]
/***
*qsort.c - quicksort algorithm; qsort() library function for sorting arrays
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* To implement the qsort() routine for sorting arrays.
*
*******************************************************************************/
#include <stdlib.h>
#include <search.h>
#include <internal.h>
/* Always compile this module for speed, not size */
#pragma optimize("t", on)
#if defined (_M_CEE)
#define __fileDECL __clrcall
#else /* defined (_M_CEE) */
#define __fileDECL __cdecl
#endif /* defined (_M_CEE) */
/* when building Managed Run time dll, we should be defining __cdecl
* to __clrcall. Also note that when compiling for MRT, we are compiling
* as C++ file.
*/
.............................................