我这个快速排序有什么问题吗?

人若无名 2013-06-02 05:42:42
找了半天没找出来。
请大家看看

main()
{
.....
if(sortNo == '4')
{
printf("快速排序\n");
QuickSort(&array, 0, length - 1);
PrintArray(&array, length);
}
}
void QuickSort(float **array,int start, int end)
{
int index;
if(end == start)
return;
index = Partition(array,start,end);
if(index > start)
QuickSort(array, 0 ,index - 1);
else if(index < end)
QuickSort(array, index + 1, end);
}

int Partition(float **array,int start, int end)
{
int index,pre;
float tmp;
pre = start - 1;
for(index = start;index < end; index++)
{
if((*array)[index] < (*array)[end])
{
pre ++;
if(pre != index )
{
tmp = (*array)[index];
(*array)[index] = (*array)[pre];
(*array)[pre] = tmp;
}
}
}
pre ++;
tmp = (*array)[pre];
(*array)[pre] = (*array)[end];
(*array)[end] = tmp;
//printf("%d\n",pre);
return pre;
}
...全文
148 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-06-03
  • 打赏
  • 举报
回复
http://www.microsoft.com/visualstudio/chs/downloads#d-2010-express 点开Visual C++ 2010 Express下面的语言选‘简体中文’,再点立即安装 再参考C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\qsort.c
bewinged 2013-06-02
  • 打赏
  • 举报
回复
再有你没有必要用二级指针去搞 给你个容易懂的代码

void quick_sort(int a[],int low,int high)
{
    int mid;

    if(low < high)
    {    
        mid = quick_pass(a,low,high);
        quick_sort(a,low,mid-1);
        quick_sort(a,mid+1,high);
    //    quick_sort(a+mid+1,0,high - mid -1);
    }
}

int quick_pass(int a[],int low,int high)
{
    datatype tmp = a[low];

    while(low < high)
    {
        while(low < high && tmp <= a[high])
            --high;
        if(low < high)
            a[low] = a[high];
        while( low  < high && tmp >= a[low])
            ++low;
        if(low < high)
            a[high] = a[low];
    }
    a[low] = tmp;
    return low;
}
bewinged 2013-06-02
  • 打赏
  • 举报
回复
首先你QuickSort的方法错了,你还没搞懂,快速排序的思想是什么。

void QuickSort(float *array,int start, int end)
{
	int index;
	if(end == start)
		return;
	
	if(start < end) 
    {
        index = Partition(&array,start,end);
        QuickSort(array, 0 ,index - 1);     
        QuickSort(array, index + 1, end);         
    }
}
然后我试了下好像就可以了

#include <stdio.h>
//#include <iostream>
void QuickSort(float *array,int start, int end);
int Partition(float **array,int start, int end);
void PrintArray(float *array,int length);
void PrintArray(float *array,int length)
{
     int i = 0;
     for(i = 0; i < length; i++)
           printf("%f\t",array[i]);
}
int main(void)
{
    float array[7]={10,7,8,12,14,13,6};
    int length = 7;
		printf("快速排序\n");
		QuickSort(array, 0, length - 1);
		PrintArray(array, length);
    //system("pause");
	return 0;
}
void QuickSort(float *array,int start, int end)
{
	int index;
	if(end == start)
		return;
	
	if(start < end) 
    {
        index = Partition(&array,start,end);
        QuickSort(array, 0 ,index - 1);     
        QuickSort(array, index + 1, end);         
    }
}
int Partition(float **array,int start, int end)
{
	int index,pre;
	float tmp;
	pre = start - 1;
	for(index = start;index < end; index++)
	{
		if((*array)[index] < (*array)[end])
		{
			pre ++;
			if(pre != index )
			{
				tmp = (*array)[index];
				(*array)[index] = (*array)[pre];
				(*array)[pre] = tmp;
			}
		}
	}
	pre ++;
	tmp = (*array)[pre];
	(*array)[pre] = (*array)[end];
	(*array)[end] = tmp;
	//printf("%d\n",pre);
	return pre;
}
walker沃克 2013-06-02
  • 打赏
  • 举报
回复

if(index > start)
        QuickSort(array, 0 ,index - 1);
    else if(index < end)
        QuickSort(array, index + 1, end);
这明显不对吗,分治的思想,你这成什么了。。。

void QuickSort(float **array,int start, int end)
{
    if(start >= end)
        return;
    int index = Partition(array,start,end);
    QuickSort(array, start ,index - 1);
    QuickSort(array, index + 1, end);
}
至于partition部分,没看。。。
qq120848369 2013-06-02
  • 打赏
  • 举报
回复
照着算法导论再看一下吧.
人若无名 2013-06-02
  • 打赏
  • 举报
回复
求解决啊,江湖告急

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧