【数据结构】 二分查找  + 冒泡选择 + 快排

二分查找 

package datastructure;



public class binary_search {

    public static void main(String[] args) {

        int array[] = new int[]{1,3,5,7,9,11,13,15,17,19};

        int number1 = 11;

        int number2 = 14;

        int result1 = binarySearch(array, number1);

        int result2 = binarySearch(array, number2);

        System.out.println(result1);

        System.out.println(result2);

    }

    

    public static int binarySearch(int array[], int num){

        int l,r,mid;

        l = 0;

        r = array.length -1;

        int res = -1;

        

        while(l <= r){

            

            mid = (l+r)/2;

            if(array[mid] > num) r = mid - 1;     

            else if(array[mid] < num) l = mid + 1; 

            else return mid + 1;

        }

        return res;    

    }    

}

 

运行结果:

6
-1

 

 

 

 

冒泡选择:

package datastructure;



public class sort {

    public static void main(String[] args) {

        

        int array[] = new int[]{3,2,1,8,9,6,7};

        

        bubbleSort(array);

        for(int i=0; i<array.length; i++){

            System.out.print(array[i]+" ");

        }

        System.out.println();

        System.out.println("------------------------");

        selectSort(array);

        for(int i=0; i<array.length; i++){

            System.out.print(array[i]+" ");

        }

    }

    

     

    



    public static void bubbleSort(int array[]){

        //冒泡排序

        for(int i=0; i<array.length-1;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;

                }

            }

        }

    }

    

    public static void selectSort(int array[]){

        //选择排序

        int min;

        for(int i=0; i<array.length-1; i++){

            min = i;

            for(int j=i+1; j<array.length; j++){

                if(array[j] < array[min]) min = j;

            }

            if(min != i){

                int tmp = array[i];

                array[i] = array[min];

                array[min] = tmp;

            }

        }

    }

}

 

运行结果:

1 2 3 6 7 8 9
------------------------
1 2 3 6 7 8 9

 

 

 

 

 

快速排序:

package datastructure;



public class quit_sort {



    public static void main(String[] args) {

         Integer[] list={34,3,53,2,23,7,14,10,10,10};

         quick(list);

         for(int i=0;i<list.length;i++){

             System.out.print(list[i]+" ");

         }

         System.out.println();

    }

    

    public static int getMiddle(Integer[] list, int low, int high) {

        int tmp = list[low];    //数组的第一个作为中轴

        while (low < high) {

            while (low < high && list[high] >= tmp) {

                high--;

            }

            list[low] = list[high];   //比中轴小的记录移到低端

            while (low < high && list[low] <= tmp) {

                low++;

            }

            list[high] = list[low];   //比中轴大的记录移到高端

        }

        list[low] = tmp;              //中轴记录到尾

        return low;                   //返回中轴的位置

    }



    public static void _quickSort(Integer[] list, int low, int high) {

        if (low < high) {

            int middle = getMiddle(list, low, high);  //将list数组进行一分为二

            _quickSort(list, low, middle - 1);        //对低字表进行递归排序

            _quickSort(list, middle + 1, high);       //对高字表进行递归排序

        }

    }



    public static void quick(Integer[] str) {

        if (str.length > 0) {    //查看数组是否为空

            _quickSort(str, 0, str.length - 1);

        }

    }



}

 

运行结果:

2 3 7 10 10 10 14 23 34 53 

 

你可能感兴趣的:(数据结构)