void quickSort(int [] nums, int l, int r) {
if (l >= r) {
return;
}
int x = nums[(l + r) >> 1];
int i = l - 1;
int j = r + 1;
while (i < j) {
while (n[++i] < x);
while (n[--j] > x);
if (i < j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
quickSort(nums, l, j);
quickSort(nums, j + 1, r);
}
快速选择
int quickSelect(int [] n, int l , int r, int k) {
if (l == r) {
return n[l];
}
int x = n[(l+r) >> 1];
int i = l - 1;
int j = r + 1;
while(i < j) {
while(n[++i] < x);
while(n[--j] > x);
if (i < j) {
int t = n[i];
n[i] = n[j];
n[j] = t;
}
}
int sl = j - l + 1;
if (k <= sl ) {
return qS(n, l, j, k);
} else {
return qS(n, j + 1, r, k - sl);
}
}
归并排序
public static void mS(int [] n, int l, int r) {
if (l >= r) {
return;
}
int m = (l + r) >> 1;
mS(n, l, m);
mS(n, m+1, r);
int i = l ;
int j = m + 1;
int k = 0;
int [] t = new int[r -l + 1];
while (i <= m && j <= r) {
if (n[i] <= n[j]) {
t[k++] = n[i++];
} else {
t[k++] = n[j++];
}
}
while (i <= m) {
t[k++] = n[i++];
}
while(j <= r) {
t[k++] = n[j++];
}
for (i = l , j = 0; i <= r; ++i, ++j) {
n[i] = t[j];
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)