69,371
社区成员
发帖
与我相关
我的任务
分享
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;
}
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;
}
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;
}
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部分,没看。。。