求C语言快排非递归算法解析。非递归。。

2024-11-17 08:51:15
推荐回答(2个)
回答1:

//快排非递归算法

void merge(int a[], int low, int center, int high){//这里的merge与教科书上有不同。我们用两个数组L[],R[]来存储a[]需要合并的两段

int i = 0;
int j = 0;
int k = 0;
int count = 0;

if(low >= high) return;

int m = center - low + 1;
int n = high - center;

int *L = (int *)malloc(sizeof(int)*SCALE);
int *R = (int *)malloc(sizeof(int)*SCALE);

if(!L || !R){
printf("归并排序merge()内存分配故障!");
exit(0);
}

for( count=0; count<=m; count++){
L[count] = a[low+count];
}
for( int count=0; count<=n; count++){
R[count] = a[low+count+m];
}

for(i=0,j=0,k=low; i if( L[i] <= R[j] ){
a[k] = L[i++];
}
else{
a[k] = R[j++];
}
}

while(i < m){
a[k++] = L[i++];
}
while(j < n){
a[k++] = R[j++];
}
free(L);
free(R);
}

回答2:

可以等一下嘛,,我这好像有,