// this is the bucket sort
#include<stdio.h>
int main()
{
int* BucketSort(int array[],int n,int array_Bucket[],int interval);
int array[10] = {100,78,9,10000,23,41,65,7,15,10};
int array_Bucket[10];
int * array_2;
int interval = 5;
array_2 = BucketSort(array,10,array_Bucket,interval);
for(int iter = 0;iter < 10;iter ++)
{
printf("%d ",array_2[iter]);
}
return 0;
}
int* BucketSort(int array[],int n,int array_Bucket[],int interval)
{
int min(int array[],int n);
int QuikSort(int array[],int low,int high);
int min_element;
min_element = min(array,n);
int flag = 0;
int times = 1;
int flag_2 = 0;
int low = 0,high = 0;
int iden = 0;
while(1)
{
iden = 0;
for(flag = 0;flag < n;flag ++ )
{
if(array[flag] < min_element + interval*times&&array[flag] >= min_element + interval*(times - 1)&&flag < n)
{
array_Bucket[flag_2] = array[flag];
high = flag_2;
flag_2 ++;
iden ++;
}
}
if(iden != 0&&flag_2 != n)
{
QuikSort(array_Bucket,low,high);
low = high + 1;
times ++;
}
else if(iden != 0 && flag_2 == n)
{
QuikSort(array_Bucket,low,n - 1);
break;
}
else if(iden == 0)
{
times ++;
}
}
return array_Bucket;
}
int min(int array[],int n)
{
int min_element = array[0];
for(int iter = 1;iter < n;iter ++)
{
if(array[iter] < min_element)
{
min_element = array[iter];
}
}
return min_element;
}
int QuikSort(int array[],int low,int high)
{
int i = low;
int high_2 = high;
int low_2 = low;
int temp;
if(low < high)
{
for(;low < high;)
{
if(array[i] >= array[high])
{
temp = array[i];
array[i] = array[high];
array[high] =temp;
i = high;
low ++;
//low和high的作用分别代表比枢轴值小/大的元素的边界
}
if(array[i] <= array[low])
{
temp = array[i];
array[i] = array[low];
array[low] =temp;
i = low;
high --;
}
}
QuikSort(array,low_2,i-1);
//递归求解子问题
QuikSort(array,i+1,high_2);
}
else
{
return 0;
}
}