Java 使用Quickselect算法查找未排序数组中的中值
我正在这里编程这个练习:。 我使用quickselect算法,但不用于排序数组。我用它来划分数组并找到medican。但我的代码仍然不能正常工作,至少在网站上进行了示例测试Java 使用Quickselect算法查找未排序数组中的中值,java,algorithm,quicksort,Java,Algorithm,Quicksort,我正在这里编程这个练习:。 我使用quickselect算法,但不用于排序数组。我用它来划分数组并找到medican。但我的代码仍然不能正常工作,至少在网站上进行了示例测试 7 0 1 2 4 6 5 3 我试图寻找我的问题,但我找不到。 我的代码在某些情况下返回true结果。 例: 这是我的密码: /** * file FindMedian.java */ import java.util.Scanner; class Number{ int n; public int
7
0 1 2 4 6 5 3
我试图寻找我的问题,但我找不到。
我的代码在某些情况下返回true结果。
例:
这是我的密码:
/**
* file FindMedian.java
*/
import java.util.Scanner;
class Number{
int n;
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
}
public class FindMedian {
public static int partition(int[] a, int low, int high, int n){
int i=low+1, j= high;
while (true){
while(a[i]<a[low]){
i++;
if (i==high) break;
}
while (a[j]>a[low]){
j--;
if (j==low) break;
}
if (i>=j)
break;
swap(a, i, j);
i++;
j--;
}
swap(a, low, j);
return j;
}
public static void progress(int[] a, int low, int high,int n, Number i){
if (low>=high)
return;
int j= partition(a, low, high, n);
if (j==n){
i.setN(j);
return;
}
else
if (j>n)
progress(a, low, j-1, n, i);
else if (j<n)
progress(a, j+1, high, n, i);
}
public static void swap(int[] a, int i, int j){
int temp= a[i];
a[i]= a[j];
a[j]= temp;
}
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
int a[]= new int[sc.nextInt()];
int n= (a.length-1)/2;
for (int index=0; index< a.length; index++)
a[index]= sc.nextInt();
Number i= new Number();
i.setN(-1);
progress(a, 0, a.length-1, n, i);
if (i.getN()!=-1)
System.out.println(a[i.getN()]);
else System.out.println("Can't find");
}
}
/**
*文件FindMedian.java
*/
导入java.util.Scanner;
班号{
int n;
公共int getN(){
返回n;
}
公共无效设置(int n){
这个,n=n;
}
}
公共类FindMedian{
公共静态int分区(int[]a,int-low,int-high,int-n){
int i=低+1,j=高;
while(true){
而(低){
j--;
如果(j==低)断开;
}
如果(i>=j)
打破
互换(a、i、j);
i++;
j--;
}
互换(a、低、j);
返回j;
}
公共静态无效进度(int[]a、int-low、int-high、int-n、数字i){
如果(低>=高)
返回;
int j=分区(a、低、高、n);
如果(j==n){
i、 setN(j);
返回;
}
其他的
如果(j>n)
进展(a,低,j-1,n,i);
else if(j我认为您需要获得枢轴的第k个位置,并通过执行数组中变量low的“int k=j-low+1;”来交叉检查枢轴是否位于与中值相同的位置。例如,第二个索引处的值将是数组中第三个最小的元素
同样对于第二个递归调用,因为我们知道中位数位于枢轴的右侧(在第k个位置),所以我们希望结果位于右侧子阵列的第(n-k)个位置
public static int progress(int[] a, int low, int high,int n){
if (low==high)
return a[low];
//partition array and return index of pivot
int j= partition(a, low, high, n);
//find the kth position of the pivot
int k=j-low+1;
//if the kth position of the pivot is the same as the required ith smallest int return pivot.
if (k==n){
return a[j];
}
else
if (n<k)
return progress(a, low, j-1, n, i);
else if (n>k)
return progress(a, j+1, high, n-k, i);
}
应将其更改为int n=(a.length+1)/2
,因为具有奇数个元素的数组的中值位于(n+1)/2
点(n=a.length)。例如,对于具有7个元素的数组,中值应位于(7+1)/2=4
位置。此算法称为QuickSelect。噢,谢谢。我不知道它的名称:Dthank u,但我发现了我的问题。它在返回值中,并且条件是停止递归方法
public static int progress(int[] a, int low, int high,int n){
if (low==high)
return a[low];
//partition array and return index of pivot
int j= partition(a, low, high, n);
//find the kth position of the pivot
int k=j-low+1;
//if the kth position of the pivot is the same as the required ith smallest int return pivot.
if (k==n){
return a[j];
}
else
if (n<k)
return progress(a, low, j-1, n, i);
else if (n>k)
return progress(a, j+1, high, n-k, i);
}
int n= (a.length-1)/2;