Wang Gang algorithms and data structures 2-- binary search, quick sort, merge sort

1, Storage Structure: Characteristics of Storage Structure linear table is a set of data element storage unit stores an arbitrary linear form, the set of memory cells may be continuous or may be discontinuous.
Types are single chain, single cycle list, doubly linked, circular list bis
Here Insert Picture Description

Singly linked list structure, messageQueue is a single list.
Here Insert Picture Description
Sort Mahjong (a little, see Mahjong ordering )
2, binary search
prerequisite data is already sorted.
Here Insert Picture Description
Good if a lot of data we have 1000, so we always divided by 2, up to 9 will be able to find the data. These divided by 2 time complexity of the algorithm is log2 ^ n, n is all of our data, for example, you have 1000 data, n is 1000.

/**
     * 二分查找
     * 返回找到的数据的下标。
     * 在数组的某段查找是否存在某个元素
     */
    public static int binarySearch(int[] array,int fromIndex,int toIndex,int key){
    //两个指针指向第一个数和最后一个数
        int low=fromIndex;
        int high=toIndex-1;//因为是左闭右开,所以要-1
        //当低位指针小于高位指针的时候
        while(low<=high){
        //分成两半
            int mid=(low+high)>>1;//取中间。>>1就是除以2
            //取到中间的值与key(要查找的数)做比较
            int midVal=array[mid];
            if(key>midVal){//去右边找
            //低位指针放到中间值的右边
                low=mid+1;
            }else if(key<midVal){//去左边找
            //高位指针放到中间值的左边
                high=mid-1;
            }else{
                return mid;
            }
        }
        return -(low+1);//low+1表示找不到时停在了第low+1个元素的位置,也可以写一个-1 
    }

Note that the left and right to open and close a repeat-free zone idea.
Our call to find 31 exists.

int[] array=new int[]{1,2,4,9,31,266,656,56666,888888};
int key=31;
binarySearch(array,0,array.length,key)

3 quick sort
Java Arrays.sort () is the use of quick sort
Here Insert Picture Description
we look at the code

//快速排序     31  21  59  68  12  40
    //    x=31
    public static void quickSort(int[] array,int begin,int end){
        if(end-begin<=0) return;
        //取到第一个值,
        int x=array[begin];
        int low=begin;
        int high=end;
        //由于会从两头取数据,一会左边,一会右边,需要一个变量控制方向
        boolean direction=true;
        L1:
        //只要左边小于右边,即指针不重合
        while(low<high){
            if(direction){//从右往左找
                for(int i=high;i>low;i--){
                    if(array[i]<=x){
                        //array[low++]=array[i];
						array[low]=array[i]//低指针的地方赋值
						low++;
                        high=i;
                        direction=!direction;
                        continue L1;//跳到标签L1的地方
                    }
                }
                high=low;//如果上面的if从未进入,让两个指针重合
            }else{
                for(int i=low;i<high;i++){
                    if(array[i]>=x){
                       // array[high--]=array[i];
                        array[high]=array[i];
                        high--
                        low=i;
                        direction=!direction;
                        continue L1;
                    }
                }
                low=high;
            }
        }
        //把最后找到的值 放入中间位置
        array[low]=x;
        //开始完成左右两边的操作
        quickSort(array,begin,low-1);
        quickSort(array,low+1,end);
    }
 

Guess you like

Origin blog.csdn.net/qczg_wxg/article/details/89423401