gpt4 book ai didi

java - quicksort3有什么问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:39:51 25 4
gpt4 key购买 nike

我的 3 向分区快速排序有什么问题?当输入数据小于 100000 时正常工作。当输入数据 = 100000 时,它工作大约 9 秒。我使用 Dijkstra 3 向分区。如果输入数据由大量相同的元素组成,一切正常,当输入数据随机时工作太慢。

 static void randomizedQuickSort(int[] a, int l, int r) {
if (l >= r) {
return;
}

int[] m = Partition3(a, l, r);
randomizedQuickSort(a, l, m[0] - 1);
randomizedQuickSort(a, m[1] + 1, r);
}

private static int[] Partition3(int[] nums, int l, int r) {
Random random = new Random();
int k = random.nextInt(r - l + 1) + l;
int mid = nums[k];
int m1 = 0;
int i = 0;
int m2 = r;


while (m1 <= m2) {
if (nums[m1] < mid) {
swap(nums, i, m1);
i++;
m1++;
} else if (nums[m1] > mid) {
swap(nums, m1, m2);
m2--;
} else {
m1++;
}
}

return new int[]{i, m2};
}

最佳答案

你做错了,因为交换的次数是 O(n^2) 如果你计算调用 swap 的次数,你会得到类似的东西(第一个数字是元素的数量)

10000: Took 0.078000 seconds, and 33432534 swaps
20000: Took 0.291000 seconds, and 166934755 swaps
40000: Took 1.102000 seconds, and 702291723 swaps
80000: Took 4.482000 seconds, and 2837543629 swaps
160000: Took 17.590000 seconds, and 11373050608 swaps

问题是线条

int m1 = 0;
int i = 0;

每次排序时从数组开头排序的位置。

int m1 = l; // sort from the start of the section.
int i = l;

完整版是....

public static void main(String[] args) {
for (int t = 100_000; t <= 100_000_000; t *= 10) {
int[] nums = new int[t];
for (int i = 0; i < nums.length; i++) {
nums[i] = random.nextInt();
}
long start = System.currentTimeMillis();
swaps = 0;
randomizedQuickSort(nums, 0, nums.length - 1);
long time = System.currentTimeMillis() - start;
for (int i=0;i<nums.length-1;i++)
if (nums[0] > nums[1])
throw new AssertionError();
System.out.printf("%d: Took %f seconds, and %d swaps%n", t, time / 1e3, swaps);
}
}

static void randomizedQuickSort(int[] a, int l, int r) {
if (l >= r) {
return;
}

long m = Partition3(a, l, r);
int m0 = (int) (m >> 32);
int m1 = (int) m;
randomizedQuickSort(a, l, m0 - 1);
randomizedQuickSort(a, m1 + 1, r);
}

static final Random random = new Random();
static long swaps = 0;

private static long Partition3(int[] nums, int l, int r) {
int k = random.nextInt(r - l + 1) + l;
int mid = nums[k];
int m1 = l;
int i = l;
int m2 = r;


while (m1 <= m2) {
if (nums[m1] < mid) {
swap(nums, i, m1);
i++;
m1++;
} else if (nums[m1] > mid) {
swap(nums, m1, m2);
m2--;
} else {
m1++;
}
}

return ((long) i << 32) | m2;
}

private static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
swaps++;
}

打印

100000: Took 0.018000 seconds, and 2032183 swaps
1000000: Took 0.168000 seconds, and 24872604 swaps
10000000: Took 1.709000 seconds, and 287681791 swaps
100000000: Took 19.015000 seconds, and 3353327832 swaps

关于java - quicksort3有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52651650/

25 4 0
文章推荐: javascript - 函数中的递归调用是如何工作的?
文章推荐: java - 一个与Spring boot devtools相关的dozer map异常
文章推荐: java - BigDecimal stripTrailingZeros 不适用于零
文章推荐: java - 如何将 ArrayList 写入 csv 文件