Java 根据三个元素的指数求中值
作为快速排序算法的一部分,我试图从给定第一个、中间和最后一个元素索引的Arraylist中找到中值。我使用了几个if/else语句作为查找它的一种方法,但它是不正确的,而且逻辑相当复杂 注意:排序数组不是一个选项Java 根据三个元素的指数求中值,java,algorithm,Java,Algorithm,作为快速排序算法的一部分,我试图从给定第一个、中间和最后一个元素索引的Arraylist中找到中值。我使用了几个if/else语句作为查找它的一种方法,但它是不正确的,而且逻辑相当复杂 注意:排序数组不是一个选项 public static int findMedian(ArrayList <Integer> A, int first, int mid, int last) { if(first == mid || first == last || last == mid)
public static int findMedian(ArrayList <Integer> A, int first, int mid, int last) {
if(first == mid || first == last || last == mid) {
return first;
}
if(A.get(first) >= A.get(last)) {
if(A.get(first) <= A.get(mid)) {
return first;
}
else if(A.get(last) >= A.get(mid)) {
return last;
}
return mid;
}
else {
if(A.get(first) > A.get(mid)) {
return first;
}
else if(A.get(mid) > A.get(last)) {
return last;
}
return mid;
}
}
公共静态int-findMedian(数组列表A、int-first、int-mid、int-last){
if(first==mid | | first==last | | last==mid){
先返回;
}
如果(A.get(first)>=A.get(last)){
如果(A.get(first)=A.get(mid)){
最后返回;
}
中途返回;
}
否则{
如果(A.get(第一)>A.get(中间)){
先返回;
}
如果(A.get(mid)>A.get(last)){
最后返回;
}
中途返回;
}
}
一件看起来很奇怪的事情是,如果last==mid,则首先返回
如果您只需删除以下行,可能效果会更好:
if(first == mid || first == last || last == mid) {
return first;
}
这个方法怎么样
public static int findMedian(ArarayList<Integer> A, int first, int mid, int \last) {
if (first == mid || first == last || last == mid)
return first;
int v = (A.get(first) >= A.get(mid) ? 1 : 0) +
(A.get(first) >= A.get(last) ? 2 : 0) +
(A.get(mid) >= A.get(last) ? 4 : 0);
switch (v) {
case 0: /* a < b && a < c && b < c */
return mid;
case 1: /* a >= b && a < c && b < c */
return first;
case 2: /* a < b && a >= c && b < c -> not possible */
return -1;
case 3: /* a >= b && a >= c && b < c */
return last;
case 4: /* a < b && a < c && b >= c */
return last;
case 5: /* a >= b && a < c && b >= c -> not possible */
return -1;
case 6: /* a < b && a >= c && b >= c */
return first;
case 7: /* a >= b && a >= c && b >= c */
return mid;
}
/* won't come here */
return first;
}
publicstaticintfindmedian(ArarayList A,intfirst,intmid,int\last){
if(first==mid | | first==last | | last==mid)
先返回;
intv=(A.get(first)>=A.get(mid)?1:0+
(A.get(first)>=A.get(last)2:0)+
(A.get(mid)>=A.get(last)?4:0;
开关(v){
案例0:/*a=b&&a=c&&b不可能*/
返回-1;
案例3:/*a>=b&&a>=c&&b=c*/
最后返回;
案例5:/*a>=b&&a=c->不可能*/
返回-1;
案例6:/*a=c&&b>=c*/
先返回;
案例7:/*a>=b&&a>=c&&b>=c*/
中途返回;
}
/*我不会来这里*/
先返回;
}
或者以更紧凑的形式
public static int findMedian(ArarayList<Integer> A, int first, int mid, int \last) {
if (first == mid || first == last || last == mid)
return first;
int result[] = new int[] { mid, first, -1, last, last, -1, first, mid };
int v = (A.get(first) >= A.get(mid) ? 1 : 0) +
(A.get(first) >= A.get(last) ? 2 : 0) +
(A.get(mid) >= A.get(last) ? 4 : 0);
return result[v];
}
publicstaticintfindmedian(ArarayList A,intfirst,intmid,int\last){
if(first==mid | | first==last | | last==mid)
先返回;
int result[]=新int[]{mid,first,-1,last,last,-1,first,mid};
intv=(A.get(first)>=A.get(mid)?1:0+
(A.get(first)>=A.get(last)2:0)+
(A.get(mid)>=A.get(last)?4:0;
返回结果[v];
}
你能在索引上使用数组。排序吗(我知道你不能在列表上使用排序)?如果是这样,您可以按值对索引进行排序,然后选择中间的一个:
int findMedian(List<Integer> list, int first, int mid, int last) {
int[] indices = {first, mid, last};
Arrays.sort(indices, (i1, i2) -> list.get(i1) - list.get(i2));
return indices[1];
}
实际上,去掉这些行会中断程序,但我明白你的观点,如果last==mid,首先返回。好的,那么我将添加一个替代方案。我找到了一些解决三个值都不同的情况的方法。但是,我必须考虑到这一点,因为我生成的数组具有随机值。
int findMedian(List<Integer> list, int first, int mid, int last) {
List<Integer> indices = new ArrayList<>();
for (int i: Arrays.asList(first, mid, last) {
int j = 0;
while (j < indices.size() && list.get(i) > list.get(j))
j++;
indices.add(j, i);
}
return indices.get(1);
}
if (list.get(first) >= list.get(mid) && list.get(first) <= list.get(last)) {
return first;
else if (list.get(mid) >= list.get(first) && list.get(mid) <= list.get(last)) {
return mid;
} else {
return last;
}