作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
二分查找方法用于从已排序的数组中查找未执行的值。我知道这个问题涉及按降序排序,但它不起作用有人可以帮我解决这个问题。如果有帮助的话,降序方法是一种选择排序。我认为问题在于该方法只能找到升序数组中的值,但不知道如何使其在降序数组上工作
package project;
import java.io.*;
import java.util.*;
import java.util.Scanner;
public class project {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int Size;//size of the array
int order; // ascending or descending order
System.out.println("Put in the amount of expenses you have");
Size = sc.nextInt();//User input for the amount of expenses
System.out.println("put in all your expenses");
int userInput[] = new int[Size];// what the users expenses are
for (int i = 0; i < userInput.length; i++)//This loop is for if the i value is smaller than user input then put in more values to complete the array
userInput[i] = sc.nextInt();
System.out.println("do you want it ascending or descending order. If you want it in ascending press 1 or if you want descending press 2");
order = sc.nextInt();// select if they wanted it in ascending or descending order
System.out.print("expenses not sorted : ");
printExpenses(userInput);
if (order == 1) {
expensesAscending(userInput);// If order is equal to one then sort in ascending else if it is equal to 2 then order it descending
} else if (order == 2) {
expensedescending(userInput);
}
int ans = binarySearch(userInput, 0, Size, 10);
System.out.println(ans);
if(ans == -1)
System.out.println("Key not found");
else
System.out.println("Key found at position " + (ans));
}
public static void printExpenses(int[] arr) {
// this is were it is printed
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] + "$");
}
}
public static void expensedescending(int arr[]) {
// This is were the selection sort starts
int N = arr.length;
for (int i = 0; i < N; i++) {
int small = arr[i];
int pos = i;
for (int j = i + 1; j < N; j++) {
if (arr[j] > small) {
small = arr[j];
pos = j;
}
}
int temp = arr[pos];
arr[pos] = arr[i];
arr[i] = temp;
System.out.println(": ");
// Printing array after pass
printExpenses(arr);
}
}
public static void expensesAscending(int arr[]) {
int N = arr.length;
for (int i = 1; i < N; i++) {
int j = i - 1;
int temp = arr[i];
while (j >= 0 && temp < arr[j]) {
arr[j + 1] = arr[j];
j--;
;
}
arr[j + 1] = temp;
System.out.println(": ");
// Printing array after pass
printExpenses(arr);
}
}
static int binarySearch(int[] array, int left, int right, int key) {
if (left > right) {
return -1;
}
int mid = (left + right) / 2;
if (array[mid] == key) {
return mid;
}
if (array[mid] > key) {
return binarySearch(array, left, mid - 1, key);
}
return binarySearch(array, mid + 1, right, key);
}
}
最佳答案
您正在尝试对降序排序数组进行升序排序数组的二分搜索。更改您的方法 header
和方法主体,如下所示:
int ans = binarySearch(userInput, 0, Size, 10,order); // order you got from user
现在,此二分搜索
将适用于数组的升序和降序:
static int binarySearch(int[] array, int left, int right, int key,int sorting)
{
if (left > right)
{
return -1;
}
int mid = (left + right) / 2;
if (array[mid] == key)
{
return mid;
}
if(sorting == 2) // if array is sorted in descending order
{
if (array[mid] > key)
{
return binarySearch(array, mid+1, right, key,sorting );
}
return binarySearch(array, left, mid-1, key,sorting );
}
else // if array is sorted in ascnending order
{
if (array[mid] > key)
{
return binarySearch(array, left, mid - 1, key,sorting );
}
return binarySearch(array, mid + 1, right, key,sorting );
}
}
关于java - 如果数组按降序排序,为什么二分查找方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55232972/
我正在尝试编写一个程序,在名为 items 的数组中进行顺序搜索和二分搜索,该数组具有 10000 个已排序的随机 int 值。第二个名为 targets 的数组加载了 1000 个 int 值(50
当我尝试使用图表并为其编写一些代码但没有成功时,我遇到了一个问题:/!! 我想创建一些东西来获取图形数据并检查它是否:1- 连接2-二分法3-有循环4-是一棵树 所以我想知道,例如,是否可以将其写入以
我是一名优秀的程序员,十分优秀!