
非递归方式
void QuickSort(int* a, int low, int high){
int i=low, j=high, temp;
if(low<high){
temp=a[i];
while(i<j){
while(i<j && a[j]>=temp)
--j;
a[i]=a[j];
while(i<j && a[i]<temp)
++i;
a[j]=a[i];
}
a[i]=temp;
QuickSort(a, low, i-1);
QuickSort(a, i+1, high);
}
}
int* pondSizes(int** land, int landSize, int* landColSize, int* returnSize){
struct{
int row;
int col;
}st[landSize*landColSize[0]];
int top, i, j, k=0, row, col, p, q, count;
int visited[landSize][*landColSize];
int* res = (int*)malloc(landSize*landColSize[0]*sizeof(int));
int tmp[3] = {
-1,0,1};
for(i=0; i<landSize; ++i)
for(j=0; j<*landColSize; ++j)
visited[i][j]=0;
for(i=0; i<landSize; ++i)
for(j=0; j<*landColSize; ++j){
top=-1;
if(land[i][j]==0 && visited[i][j]==0){
++top; st[top].row=i, st[top].col=j; count=0; visited[i][j]=1;
while(top>-1){
row=st[top].row; col=st[top].col; --top; ++count;
for(p=0; p<3; ++p)
for(q=0; q<3; ++q){
if((row+tmp[p]<landSize) && (col+tmp[q]<*landColSize) && (row+tmp[p]>-1) &&
(col+tmp[q]>-1) && (land[row+tmp[p]][col+tmp[q]]==0) && visited[row+tmp[p]][col+tmp[q]]==0){
++top; st[top].row=row+tmp[p]; st[top].col=col+tmp[q]; visited[row+tmp[p]][col+tmp[q]]=1;
}
}
}
res[k]=count; ++k;
}
}
QuickSort(res, 0, k-1);
*returnSize = k;
return res;
}