gpt4 book ai didi

java - 对数组进行排序并使用二分查找

转载 作者:行者123 更新时间:2023-12-01 11:40:59 25 4
gpt4 key购买 nike

我已经让这个程序运行了,它几乎给了我我想要的答案。运行整个程序后,它应该打印出键的索引以及键的相等值。我一生都无法弄清楚如何将索引放入控制台中的最终消息中。例如,如果你有一个数组 {0,1,2,3,4,5,6,7,8,9} 并且你的键是 0,它应该返回:list[0] = 0。我标记我认为我的问题在于“(index???)”的部分

import java.util.Scanner;
public class PartA {
public static void main(String[] args){
System.out.println("Please enter 10 double values:");
double [] array = inputArray();
selectionSort(array);
printArray(array);
System.out.println("Please enter a search key:");
Scanner input = new Scanner(System.in);
double key = input.nextDouble();
double x = binarySearch(array,key);
if (x != -1)
System.out.println("list[" + (index???) + "] = " + key);
else
System.out.println(key + " is not on the list");
}

public static double[] inputArray(){
Scanner array = new Scanner(System.in);
double [] list = new double[10];
for(int i = 0; i < list.length; i++){
array.hasNextDouble();
list[i] = array.nextDouble();
}
return list;
}
public static void selectionSort(double[] list){
double array[] = new double [list.length];
for(int i = 0; i < list.length; i++){
for(int j = 0; j < list.length; j++){
if (list[i] < list[j]){
double x = list[i];
list[i] = list[j];
list[j] = x;
}
}
}
}
public static void printArray(double[] list){
for (int i = 0; i < list.length; i++){
System.out.println("list[" + i + "] = " + list[i]);
}
System.out.println();
}
public static int binarySearch(double []list, double key){
int low = 0, high = list.length - 1;
while(high >= low){
int mid = (low + high)/2;
if(key < list[mid])
high = mid - 1;
else if(key == list[mid])
return mid;
else low = mid + 1;
}
return -1;
}
}

最佳答案

我还在问题的评论中提到了解决方案,但为了完整起见,我发布了这个答案。

public class Main {
public static void main(String[] args){
System.out.println("10 double values:");
double [] array = {0,1,2,3,4,5,6,7,8,9};
selectionSort(array);
printArray(array);
double key = 4;
System.out.println("Searched Key: " + key);
int idx = binarySearch(array,key); //use int instead of double
if (idx != -1)
//use the variable identifier to print the index
System.out.println("list[" + idx + "] = " + key);
else
System.out.println(key + " is not on the list");
}

public static void selectionSort(double[] list){
double array[] = new double [list.length];
for(int i = 0; i < list.length; i++){
for(int j = 0; j < list.length; j++){
if (list[i] < list[j]){
double x = list[i];
list[i] = list[j];
list[j] = x;
}
}
}
}
public static void printArray(double[] list){
for (int i = 0; i < list.length; i++){
System.out.println("list[" + i + "] = " + list[i]);
}
System.out.println();
}
public static int binarySearch(double []list, double key){
int low = 0, high = list.length - 1;
while(high >= low){
int mid = (low + high)/2;
if(key < list[mid])
high = mid - 1;
else if(key == list[mid])
return mid;
else low = mid + 1;
}
return -1;
}
}

使用 Ideone 的工作示例: http://ideone.com/WXrJPi

关于java - 对数组进行排序并使用二分查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29531419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com