这是我写的程序,不知道为什么最后打印出来是-1.明明在数组里面啊。郁闷public class ExamResult{
public static void BubbleSort(int b []){
int temp;
for(int i=0;i<b.length;i++) {
for(int j=0;j<b.length-1;j++) {
if(b[j]<b[j+1]) {
temp = b[j];
b[j] = b[j+1];
b[j+1] = temp;
}
}
}
for(int i=0;i<b.length;i++){
System.out.println("排序后的数组是"+b[i]+" ");
}
}
public static int BinarySearch(int a[],int key){
int low = 0;
int high = a.length-1;
while(low<=high){
int mid = (low+high)/2;
if(key==a[mid]){
System.out.println("找到了!!!");
return mid;
}
else if(key<a[mid]) {
high = mid-1;
}
else {
low = mid+1;
}
}
System.out.println("没有找到");
return -1;
}
public static void main(String[] args) {
int score[] = {568,487,635,598,521};
BubbleSort(score);
int key = 598;
int index = BinarySearch(score,598);
System.out.println("它的位置是score["+index+"]");
}
}
public static void BubbleSort(int b []){
int temp;
for(int i=0;i<b.length;i++) {
for(int j=0;j<b.length-1;j++) {
if(b[j]<b[j+1]) {
temp = b[j];
b[j] = b[j+1];
b[j+1] = temp;
}
}
}
for(int i=0;i<b.length;i++){
System.out.println("排序后的数组是"+b[i]+" ");
}
}
public static int BinarySearch(int a[],int key){
int low = 0;
int high = a.length-1;
while(low<=high){
int mid = (low+high)/2;
if(key==a[mid]){
System.out.println("找到了!!!");
return mid;
}
else if(key<a[mid]) {
high = mid-1;
}
else {
low = mid+1;
}
}
System.out.println("没有找到");
return -1;
}
public static void main(String[] args) {
int score[] = {568,487,635,598,521};
BubbleSort(score);
int key = 598;
int index = BinarySearch(score,598);
System.out.println("它的位置是score["+index+"]");
}
}
解决方案 »
- 初学者的Thread问题。求解啊
- 我打算每秒执行一个方法,如何设置定时器?
- jtable内绑定jcombox 产生的焦点问题 急求解
- 高手来帮觖决一上字符替换问题?
- 关于TIMER的问题.
- 在MyEclipse中运行下面代码出现的错误是:You have an error in your SQL syntax; check the manual t
- 请教一个Java测试类的“出现预期不符”的原因.
- 怎样使用Runtime的exec(String command)的函数?
- Cappuccino : Memory Leak & SoftReference
- 看泛型时遇到一个问题求解答
- 请大虾出手
- 网址中的中文转成字母与符号的java方法
public static void BubbleSort(int b[]) {
int temp;
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length - 1; j++) {
if (b[j] < b[j + 1]) {
temp = b[j];
b[j] = b[j + 1];
b[j + 1] = temp;
}
}
}
for (int i = 0; i < b.length; i++) {
System.out.println("排序后的数组是" + b[i] + " ");
}
} public static int BinarySearch(int a[], int key) {
int low = 0;
int high = a.length - 1; while (low <= high) {
int mid = (low + high) / 2;
if (key == a[mid]) {
System.out.println("找到了!!!");
return mid;
} else if (key < a[mid]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
System.out.println("没有找到");
return -1;
} public static void main(String[] args) {
int score[] = { 568, 487, 635, 598, 521 };
BubbleSort(score);
int key = 598;
int index = BinarySearch(score, 598);
System.out.println("它的位置是score[" + index + "]");
}
}key < a[mid] 和 key > a[mid] 的执行语句交换下位置就行了
冒泡排序改进一点:
1、可以只进行n-1趟就好了
2、每次选一个相对最大的往后面放了,所以内层循环可以让j<b.length-1-i,因为后面的i个已经放好了
改后如下: public static void BubbleSort(int b []){
int temp;
for(int i=0;i<b.length-1;i++) {
for(int j=0;j<b.length-1-i;j++) {
if(b[j]>b[j+1]) {
temp = b[j];
b[j] = b[j+1];
b[j+1] = temp;
}
}
}
for(int i=0;i<b.length;i++){
System.out.println("排序后的数组是"+b[i]+" ");
}
}