排序和使用Java二进制搜索

编程入门 行业动态 更新时间:2024-10-25 09:31:31
本文介绍了排序和使用Java二进制搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我被要求进行排序和搜索的数组。排序数组很简单,我的code的工作,但随后每当我试图把它适用于数组中的第一个元素,但给了我-1,结果

二进制搜索方法

我的全code是如下:

公共静态无效的主要(字串[] args){    INT []数组=新INT [5];    数组[0] = 50;    阵列[1] = 40;    阵列[2] = 10;    阵列[3] = 20;    数组[4] = 100;排序(数组,(array.length - 1));      为(中间体X = 0; X&下; array.length; X ++){        的System.out.println(+阵列[X]);    }    的System.out.println();    的System.out.println(二进制搜索(R):+ rBsearch(数组,0,(array.length),20));}    公共静态无效的排序(INT []一,INT最后){    如果(最后一个大于0){        INT最大= findMax(一个,最后一个);        掉期(A,最后,最大值);        排序(A,最后 - 1);    }}公共静态INT rBsearch(INT [] L,INT低,诠释高,诠释K){    INT中旬=(低+高)/ 2;    如果(低>的高点){        返回-1;    }否则如果(L [MID] == K){        返回中旬;    }否则如果(L [MID]< K){        返回rBsearch(L,K,中+ 1,高);    }其他{        返回rBsearch(L,K,低,中 - 1);    } }公共静态INT findMax(INT []编曲,诠释即止){    INT最大= 0;    的for(int i = 0; I< =最后,我++){        如果(ARR [I]>常用3 [MAX]){            最大= I;        }    }    返回最大值;    }公共静态无效掉期(INT []改编,INT最后,诠释最大值){    INT TEMP = ARR [最后]    ARR [最后] = ARR [MAX];    ARR [MAX] =温度;}

解决方案

您疯玩了二进制搜索间隔

公共静态INT rBsearch(INT [] L,INT低,诠释高,诠释K){    INT中旬=(低+高)/ 2;    如果(低>的高点){        返回-1;    }否则如果(L [MID] == K){        返回L [MID];    }否则如果(L [MID]< K){        返回rBsearch(L,中+ 1,高,K);    }其他{        返回rBsearch(L,低,中 - 1,K);    } }

I was asked to sort and search an array. The sorting the array was simple and my code worked but then whenever I try to call the binary search method it works for the first element in the array but gives me "-1" as a result

My full code is as follows:

public static void main(String[] args) { int[] array = new int[5]; array[0] = 50; array[1] = 40; array[2] = 10; array[3] = 20; array[4] = 100; sort(array, (array.length - 1)); for (int x = 0; x < array.length; x++) { System.out.println(" " + array[x]); } System.out.println(""); System.out.println("Binary search (R): " + rBsearch(array, 0, (array.length), 20)); } public static void sort(int[] a, int last) { if (last > 0) { int max = findMax(a, last); swap(a, last, max); sort(a, last - 1); } } public static int rBsearch(int[] L, int low, int high, int k) { int mid = (low + high) / 2; if (low > high) { return -1; } else if (L[mid] == k) { return mid; } else if (L[mid] < k) { return rBsearch(L, k, mid + 1, high); } else { return rBsearch(L, k, low, mid - 1); } } public static int findMax(int[] arr, int last) { int max = 0; for (int i = 0; i <= last; i++) { if (arr[i] > arr[max]) { max = i; } } return max; } public static void swap(int[] arr, int last, int max) { int temp = arr[last]; arr[last] = arr[max]; arr[max] = temp; }

解决方案

You goofed up the binary search intervals

public static int rBsearch(int[] L, int low, int high, int k) { int mid = (low + high) / 2; if (low > high) { return -1; } else if (L[mid] == k) { return L[mid]; } else if (L[mid] < k) { return rBsearch(L, mid + 1, high, k); } else { return rBsearch(L, low, mid - 1, k); } }

更多推荐

排序和使用Java二进制搜索

本文发布于:2023-11-30 10:53:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649716.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!