对一个随机数组进行排序,然后对其实行折半查找,可是返回的值总是能从该数组中找到查找的值!!??
程序如下:
import java.util.*;
public class recursion{
public static void main(String args[]){
int[] a=new int[10];
Random myRandom=new Random();
for(int i=0;i<a.length;i++){
a[i]=(int)(myRandom.nextDouble()*10.0);
}
System.out.print(" Initial data:");
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
recursion(a,0,a.length-1);
System.out.print("After sorting the data:");
for(int i=0;i<a.length;i++){
System.out.print(a[i]+",");
}
System.out.println();
int[] b=new int[a.length];
System.arraycopy(a, 0, b, 0,a.length);
System.out.print("copydata: ");
int maxSize = 100;
recursion arr = new recursion(maxSize);
for(int i=0;i<b.length;i++){
arr.insert(b[i]);
}
arr.display();
int searchKey =5;
if (arr.find(searchKey) != arr.size())
System.out.println("Found " + searchKey);
else
System.out.println("Can't find " + searchKey);
}
static int quicksortStep(int[] A, int lo, int hi){
int pivot = A[lo];
while (hi > lo) {
while (hi > lo && A[hi] > pivot) {
hi--;
}
if (hi == lo) break;
A[lo] = A[hi];
lo++;
while (hi > lo && A[lo] < pivot) {
lo++;
}
if (hi == lo) break;
A[hi] = A[lo];
hi--;
}
A[lo] = pivot;
return lo;
}
static void recursion(int[] A,int lo,int hi){
if(hi<=lo){
return;
}
else{
int pivotposition=quicksortStep(A,lo,hi);
recursion(A,lo,pivotposition-1);
recursion(A,pivotposition+1,hi);
}
}
private int[] a;
private int n;
public recursion(int max) {
a = new int[max];
n = 0;
}
public int size() {
return n;
}
public int find(int searchKey) {
return recFind(searchKey, 0, n - 1);
}
private int recFind(int searchKey, int loIndex, int hiIndex) {
if (loIndex > hiIndex) {
return -1;
}
else {
int middle = (loIndex + hiIndex) / 2;
if (searchKey == a[middle])
return middle;
else if (searchKey <a[middle])
return recFind(searchKey, loIndex, middle - 1);
else
return recFind(searchKey, middle + 1, hiIndex);
}
}
public void insert(int value)
{
a[n] = value;
n++;
}
public void display() {
for (int j = 0; j < n; j++)
System.out.print(a[j] + " ");
System.out.println("");
}
}
------解决方案--------------------
第一眼就看不下去了.
------解决方案--------------------
我晕,你写的是程序吗?
------解决方案--------------------
连注释也没有怎么看啊
------解决方案--------------------
- Java code
import java.util.*; public class recursion{ public static void main(String args[]){ int[] a=new int[10]; Random myRandom=new Random(); for(int i=0;i <a.length;i++){ a[i]=(int)(myRandom.nextDouble()*10.0); } System.out.print( " Initial data: "); for(int i=0;i <a.length;i++){ System.out.print(a[i]+ " "); } System.out.println(); recursion(a,0,a.length-1); System.out.print( "After sorting the data: "); for(int i=0;i <a.length;i++){ System.out.print(a[i]+ ", "); } System.out.println(); int[] b=new int[a.length]; System.arraycopy(a, 0, b, 0,a.length); System.out.print( "copydata: "); int maxSize = 100; recursion arr = new recursion(maxSize); for(int i=0;i <b.length;i++){ arr.insert(b[i]); } arr.display(); int searchKey =5; if (arr.find(searchKey) != arr.size()) System.out.println( "Found " + searchKey); else System.out.println( "Can 't find " + searchKey); } static int quicksortStep(int[] A, int lo, int hi){ int pivot = A[lo]; while (hi > lo) { while (hi > lo && A[hi] > pivot) { hi--; } if (hi == lo) break; A[lo] = A[hi]; lo++; while (hi > lo && A[lo] < pivot) { lo++; } if (hi == lo) break; A[hi] = A[lo]; hi--; } A[lo] = pivot; return lo; } static void recursion(int[] A,int lo,int hi){ if(hi <=lo){ return; } else{ int pivotposition=quicksortStep(A,lo,hi); recursion(A,lo,pivotposition-1); recursion(A,pivotposition+1,hi); } } private int[] a; private int n; public recursion(int max) { a = new int[max]; n = 0; } public int size() { return n; } public int find(int searchKey) { return recFind(searchKey, 0, n - 1); } private int recFind(int searchKey, int loIndex, int hiIndex) { if (loIndex > hiIndex) { return -1; } else { int middle = (loIndex + hiIndex) / 2; if (searchKey == a[middle]) return middle; else if (searchKey <a[middle]) return recFind(searchKey, loIndex, middle - 1); else return recFind(searchKey, middle + 1, hiIndex); } } public void insert(int value) { a[n] = value; n++; } public void display() { for (int j = 0; j < n; j++) System.out.print(a[j] + " "); System.out.println( " "); } }