[英]Java having trouble figuring out Quicksort Median of 3
I am having a hard time doing Quicksort Median of 3 (with a cutoff). 我很难进行3的Quicksort中值(截止)。 I have to figure out Median of 5 after this but I need to figure out what I am doing wrong here because I am getting an unsorted answer. 在此之后,我必须找出5的中位数,但是我需要弄清楚我在做什么错,因为我得到的答案是未分类的。
public class QuicksortTest1
{
public static <AnyType extends Comparable<? super AnyType>> void
quicksort(AnyType [] a)
{
quicksort(a, 0, a.length - 1);
}
private static <AnyType extends Comparable<? super AnyType>> void
quicksort(AnyType [] a, int left, int right)
{
final int CUTOFF = 3;
if (left + CUTOFF <= right)
{
AnyType pivot = (AnyType) median3 (a, left, right);
int i = left, j = right - 1;
for( ; ;)
{
while( a[++i].compareTo(pivot) < 0) {}
while( a[--j].compareTo(pivot) < 0) {}
if(i < j)
swap(a, i, j);
else
break;
}
swap(a, i, right - 1);
quicksort(a, left, i - 1);
quicksort(a, i + 1, right);
}
else
insertionSort(a, left, right);
}
private static <AnyType extends Comparable<? super AnyType>> AnyType
median3(AnyType [] a, int left, int right)
{
int center = (left + right) /2;
if (a[center].compareTo(a[left]) < 0)
swap(a,left,center);
if (a[right].compareTo(a[left]) < 0)
swap(a, left, right);
if (a[right].compareTo(a[center]) < 0)
swap(a, center, right);
swap(a, center, right - 1);
return a[right - 1];
}
private static void swap(Comparable a[], int i, int j)
{
Comparable tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
private static void insertionSort(Comparable a[], int left, int right)
{
int j;
for (int p = left + 1; p <= right; p++)
{
Comparable tmp = a[p];
for (j = p; j > left && tmp.compareTo( a[j - 1]) < 0; j--)
a[j] = a[j-1];
a[j] = tmp;
}
}
public static void main (String [] args)
{
Comparable [] a = {1,53,86,21,49,32,90,65,33,11,
34,68,54,32,78,80,35,22,96,59,265,44324,123,3123,25435};
quicksort(a);
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
}
}
//result: 1 49 265 32 44324 21 53 123 11 3123 25435 33 34 32 59 96 22 86 90 35 65 68 54 78 80 //结果:1 49 265 32 44324 21 53 123 11 3123 25435 33 34 32 59 96 22 86 90 35 65 68 54 78 80
Changes to code noted in comments 注释中注释的代码更改
private static <AnyType extends Comparable<? super AnyType>> void
quicksort(AnyType [] a, int left, int right)
{
final int CUTOFF = 3;
if (left + CUTOFF <= right)
{
AnyType pivot = (AnyType) median3 (a, left, right);
int i = left - 1, j = right + 1; // fix
for( ; ;)
{
while( a[++i].compareTo(pivot) < 0) {}
while( a[--j].compareTo(pivot) > 0) {} // fix
if(i < j)
swap(a, i, j);
else
break;
}
// swap(a, i, right - 1); // delete
quicksort(a, left, j); // fix
quicksort(a, j + 1, right); // fix
}
else
insertionSort(a, left, right);
}
private static <AnyType extends Comparable<? super AnyType>> AnyType
median3(AnyType [] a, int left, int right)
{
int center = (left + right) /2;
if (a[center].compareTo(a[left]) < 0)
swap(a,left,center);
if (a[right].compareTo(a[left]) < 0)
swap(a, left, right);
if (a[right].compareTo(a[center]) < 0)
swap(a, center, right);
// swap(a, center, right - 1); // delete
return a[center]; // fix
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.