快速排序

package sort;

public class Quicksort {
public static void main(String[] args) {

int[] s = { 13, 15, 7, 10, 20, 4, 8, 19 };
// s = quicksort(s, 0, s.length - 1);
s = quicks(s, 0, s.length - 1);
for (int i : s) {

System.out.print(i + " ");
}

}

public static int[] quickd(int[] num, int left, int right) {
if (left < right) {
int k = doublequick(num, left, right);
System.out.println("k=" + k);
quickd(num, left, k - 1);
quickd(num, k + 1, right);
}
return num;
}

public static int[] quicks(int[] num, int left, int right) {
if (left < right) {
int k = singlequick(num, left, right);
System.out.println("k=" + k);
quicks(num, left, k - 1);
quicks(num, k + 1, right);
}
return num;
}


public static int doublequick(int[] s, int left, int right) {
int i = left;
int j = right;
int temp;
int x = s[i];
while (i < j) {
while (i < j && s[j] >= x)
j--;
temp = s[j];
s[j] = s[i];
s[i] = temp;
while (i < j && s[i] <= x)
i++;
temp = s[j];
s[j] = s[i];
s[i] = temp;
}

return i;

}

public static int singlequick(int[] s, int left, int right) {
int i = left;
int j = right;
int x = s[left];
while (i < j) {
while (i < j && s[j] >= x)
j--;
s[i] = s[j];
while (i < j && s[i] <= x)
i++;
s[j] = s[i];
}
s[i] = x;
System.out.println("i=" + i);
return i;
}

public static int[] quicksort(int[] s, int left, int right) {

int i = left;
int j = right;
int middle = s[(i + j) / 2];
System.out.println(middle);
do {
while (i <= right && middle > s[i])
i++;
while (j >= left && middle < s[j])
j--;

if (i <= j) {

int temp = s[j];
s[j] = s[i];
s[i] = temp;
i++;
j--;

}

} while (i <= j);

for (int k : s) {

System.out.print(k + " ");
}

if (left < j) {


System.out.println("j=" + j);
quicksort(s, left, j);

}
if (right > i) {

System.out.println("i=" + i);
quicksort(s, i, right);

}

return s;

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值