编程语言
首页 > 编程语言> > 冒泡排序、快速排序、二分查找java实现

冒泡排序、快速排序、二分查找java实现

作者:互联网

/**
 * 冒泡排序、快速排序、二分查找
 */
public class Sort {
    //快速排序,以边界值为基准
    public int[] quickSort(int[] array,int left,int right){
        int low = left;
        int high =  right;
        if(low >= high){
            return array;
        }
        //以最后一个元素为基准值
        int baseVaule = array[high];
        while(low < high){
            while ((low < high) && array[low] <=  baseVaule){
                low++;
            }
            array[high]=array[low];
 
            while((low < high) && baseVaule <= array[high]){
                high--;
            }
            array[low]=array[high];
        }
        array[low]=baseVaule;
        quickSort(array,left,low-1);
        quickSort(array,low+1,right);
        return array;
    }
 
    //快速排序,以中间元素为基准
    public int[] quickSort2(int[] array,int left,int right){
        int low = left;
        int high =  right;
        if(low >= high){
            return array;
        }
        //以中间值为基准值
        int tmp =0;
        int baseVaule = array[array.length/2];
        while(low < high){
            //从左向右找到第1个大于等于基准的值
            while (array[low] <  baseVaule){
                low++;
            }
            //从右向左找到第1个小于等于基准的值
            while(baseVaule < array[high]){
                high--;
            }
            if(low >= high){
                break;
            }
            //交换两个元素的值
            tmp=array[low];
            array[low]=array[high];
            array[high]=tmp;
            if(array[low] == baseVaule){
                high--;
            }
            if(array[high] == baseVaule){
                low++;
            }
        }
        if(low == high){
            low++;
            high--;
        }
        if(left < high){
            quickSort2(array,left,high);
        }
        if(low < right){
            quickSort2(array,low,right);
        }
 
        return array;
    }
 
    //冒泡排序
    public int[] bubbleSort(int[] array){
        for(int i=0;i<array.length;i++){
            for(int j=i+1;j<array.length;j++){
                if(array[i] > array[j]){
                    int tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
        return array;
    }
 
    //二分查找
    public int binarySearch(int[] array,int value){
        int low = 0;
        int high = array.length-1;
        while(low<=high){
            int middle = (low + high)/2;
            if(value == array[middle]){
                return middle;
            }
            if(value < array[middle]){
                high = middle -1;
            }
            if(value > array[middle]){
                low = middle + 1;
            }
        }
        return -1;
    }
 
    public static void main(String[] args) {
        Sort sort = new Sort();
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()){
            String[] arrayStr = sc.nextLine().split(",");
            int[] array = new int[arrayStr.length];
            for(int i =0; i < arrayStr.length; i++){
                array[i]= Integer.parseInt(arrayStr[i]);
            }
            int[] result = sort.quickSort2(array,0,array.length-1);
            System.out.println("quicksort:"+Arrays.toString(result));
            int[] result2 = sort.bubbleSort(array);
            System.out.println("bubblesort:"+Arrays.toString(result2));
            int searchResult = sort.binarySearch(result2,7);
            System.out.println("binarySearch:" + searchResult);
        }
    }
}

 运行结果:

13,34,12,3,1,7,5
quicksort:[1, 3, 5, 7, 12, 13, 34]
bubblesort:[1, 3, 5, 7, 12, 13, 34]
binarySearch:3

 

标签:二分,java,int,冒泡排序,high,while,low,array,baseVaule
来源: https://www.cnblogs.com/chensy-abby/p/16317337.html