用java编程,给定一个数组:int[] array={12,1,3,34,121,565};,将
答案:1 悬赏:80 手机版
解决时间 2021-11-21 07:47
- 提问者网友:寂寞撕碎了回忆
- 2021-11-20 07:09
用java编程,给定一个数组:int[] array={12,1,3,34,121,565};,将
最佳答案
- 五星知识达人网友:轮獄道
- 2021-11-20 08:43
简单方式:
import java.util.Arrays;
class Test
{
public static void main(String[] args)
{
int[] test={12,1,3,34,121,565};
Arrays.sort(test);
for (int k : test)
System.out.println(k);
}
}
---------------------------
快速排序法:
public class Test {
public static void main(String[] args) throws InterruptedException {
int test[] = {12,1,3,34,121,565};
new Test().qSort(test, 0, test.length - 1);
for (int k : test)
System.out.println(k);
}
public void qSort(int[] array, int low, int high) {
if (low < high) {
int privot = partition(array, low, high);
qSort(array, low, privot - 1);
qSort(array, privot + 1, high);
}
}
public int partition(int[] array, int low, int high) {
int tmp = array[low]; // 数组的第一个作为中轴
while (low < high) {
while (low < high && array[high] >= tmp) {
high--;
}
array[low] = array[high]; // 比中轴小的记录移到低端
while (low < high && array[low] <= tmp) {
low++;
}
array[high] = array[low]; // 比中轴大的记录移到高端
}
array[low] = tmp; // 中轴记录到尾
return low; // 返回中轴的位置
}
}
import java.util.Arrays;
class Test
{
public static void main(String[] args)
{
int[] test={12,1,3,34,121,565};
Arrays.sort(test);
for (int k : test)
System.out.println(k);
}
}
---------------------------
快速排序法:
public class Test {
public static void main(String[] args) throws InterruptedException {
int test[] = {12,1,3,34,121,565};
new Test().qSort(test, 0, test.length - 1);
for (int k : test)
System.out.println(k);
}
public void qSort(int[] array, int low, int high) {
if (low < high) {
int privot = partition(array, low, high);
qSort(array, low, privot - 1);
qSort(array, privot + 1, high);
}
}
public int partition(int[] array, int low, int high) {
int tmp = array[low]; // 数组的第一个作为中轴
while (low < high) {
while (low < high && array[high] >= tmp) {
high--;
}
array[low] = array[high]; // 比中轴小的记录移到低端
while (low < high && array[low] <= tmp) {
low++;
}
array[high] = array[low]; // 比中轴大的记录移到高端
}
array[low] = tmp; // 中轴记录到尾
return low; // 返回中轴的位置
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯