java day07 数组的线性查找,二分法查找,冒泡排序

package helloworld;

import java.util.Scanner;

/**
 * 、
 * @Description
 * @author 
 * @date 2020年7月25日上午9:42:23
 */
public class HelloWorld {
    public static void main(String[] args) {
    //线性查找:
    String[] a = new String[] {"a","b","c"};
    String b = "b";
    boolean isFlag = true;
    for(int i = 0;i < a.length;i++) {
    	if(b.equals(a[i])) {
    		System.out.println("找到了!  " + i);
    		isFlag = false;
    		break;
    	}
    }
    if(isFlag ) {
    	System.out.println("没有找到!");
    }
    
    //二分法查找
    boolean is = true;
    int[] array = new int[] {1,2,3,4,5,6,7};
    int c = 13;
    int head = array.length - 1;
    int end = 0;
    for(int i = 0;i < array.length;i++) {
    	int middle = (head + end) / 2;
    	if(middle > c) {
    		end = middle -1;
    	}else if(c == array[middle]) {
    		System.out.println("找到了  " + i);
    		is = false;
    		break;
    	}else {
    		head = middle + 1;
    	}
    	
    }
    if(is) {
		System.out.println("找不到");
	}
    
    //冒泡排序
    int[]bubbleSort = new int[] {-9,1,3,0,9};
    for(int i = 0;i < bubbleSort.length - 1;i++) {
    	for(int j = 0;j < bubbleSort.length - 1 - i;j++) {
    		if(bubbleSort[j] > bubbleSort[j+1]) {
    			int temp = bubbleSort[j];
    			bubbleSort[j] = bubbleSort[j+1];
    			bubbleSort[j+1] = temp;
    		}
    	}
    }
    for(int i = 0;i < bubbleSort.length - 1;i++) {
    	System.out.println(bubbleSort[i]);
    }
    }
}
  

猜你喜欢

转载自blog.csdn.net/weixin_46381608/article/details/107574920
今日推荐