抛个没有count的;一次写对还是有难度的
grass的count的办法能简化代码
#include
int k = 0;
int pts[k+1]; //pts[0] for color 1; pts[k-1] for color k; pts[k] is the
current index in balls
void printArray(int *arr, int length)
{
for(int i = 0; i < length; i++)
printf("%d,",arr[i]);
printf("\n");
}
void KquickSortPartition(int *arr, int length)
{
if (arr == NULL || length == 0 || k <= 0) return;
printArray(arr, length);
for (int i = 0; i < k+1; i++)
pts[i] = 0;
int ball;
int color;
while (pts[k]< length)
{
// current color
color = arr[pts[k]];
for (int j = k; j > color; j--)
{
ball = arr[pts[j]];
arr[pts[j]] = arr[pts[j-1]];
arr[pts[j-1]] = ball;
}
for (int j = color; j <= k; j++)
pts[j]+=1;
// printArray(pts, k+1);
}
printArray(arr, length);
printf("\n");
}
int main ()
{
k = 4;
int balls1[] = {1,1,2,4,1,2,1,2,4,4,4,2,3,4,3,4,3,4,2,3,4,2,3,3,4,1};
KquickSortPartition(balls1, sizeof(balls1)/sizeof(int));
int balls2[] = {3,3,3,3,3,1,1,1,4,4,4,4};
KquickSortPartition(balls2, sizeof(balls2)/sizeof(int));
int balls3[] = {1,1,1};
KquickSortPartition(balls3, sizeof(balls3)/sizeof(int));
int balls4[] = {4,4,4};
KquickSortPartition(balls4, sizeof(balls4)/sizeof(int));
int balls5[] = {1,2,3,4,1,2,3,4,1,2,3,4};
KquickSortPartition(balls5, sizeof(balls5)/sizeof(int));
k = 1;
int balls6[] = {1,1,1,1,1};
KquickSortPartition(balls6, sizeof(balls6)/sizeof(int));
}