Talk about sorting and searching

Today, let's talk about sorting and searching in data structures

The first and most used sort I encountered was selection sort.

First, selection sort is a very basic sorting algorithm. The basic idea is to regard the linear table as two parts, ordered and disordered. The ordered sublist is a[0:i-1], and the disordered sublist is a[i:n-1]. During the sorting process, select the smallest element from the unordered sublist each time and add it to the end of the ordered sublist. The ordered sublist is kept in order and the length is increased by 1. The length of the unordered sublist is reduced by 1, and repeated. This process knows that the unordered sublist is empty.

    Not much to say, just look at the code:

public void selectSort ( int [] a){
     if (a== null )
         return;
     if (a. length == 0 )
         return;
 // The outer loop only needs to loop n-1 times
 for ( int i= 0 ; i< a. length - 1 ; i++){
         for ( int j = i+ 1 ; j<a. length ; j++){
             if (a[i]>a[j]){
                 // swap function
                        swap(a,i,j);
            }
        }
    }
}

Because every time the loop is looped, one number is already in order, so the outer loop only needs to be executed n-1 times.

2. Insertion sort: Similarly, the linear table is regarded as two parts, ordered and unordered. The ordered subtable is a[0:i-1], and the unordered subtable is a[i:n-1]. In the sorting process, one element is taken out from the unordered sub-table at a time, and it is inserted into the correct position of the ordered sub-table, so that the ordered sub-table is kept in order and gradually increased until all records are inserted into the ordered table.

    see code:

public void insertSort(int[] a){
    if (a==null)
        return;
    if (a.length==0)
        return;
    for (int i=1;i<a.length;i++){
        int x =a[i];
        int j;
        for (j=i-1;j>=0 && a[j]>x;j--)
            a[j+1] = a[j];
        a[j+1] = x;
    }
}

    The inner loop is to find the correct position for the elements in the unordered sublist without destroying the orderliness of the ordered sublist.

3. Bubble sort: Compared with the previous selection sort, the selection sort can find the smallest element in one pass, while the bubble sort can find the largest element in each pass. Bubble sort compares adjacent elements, and swaps them in reverse order until the loop is complete.

    see code:

public void bubbleSort(int[] a){
    if (a==null)
        return;
    if (a.length==0)
        return;
    int flag = 1;
    for (int i=0;i<a.length && flag!=0;i++){
        //这里减i是为了提高效率
        flag = 0;
        for (int j = 0;j<a.length-i-1;j++){
            if (a[j]>a[j+1]){
                swap(a,j,j+1);
                flag = 1;
            }
        }
    }
}

    Because each sorting will have an element ordered, in order to improve efficiency and avoid repeated scanning of elements, the inner loop will reduce the ordered number after each sorting.

4. Quick sort: Also known as partition exchange sort, it is currently known to be the fastest sorting method on average measured, and it is an improvement on bubble sort.

    look at the code

public void quickSort(int[] a){
    if (a==null)
        return;
    if (a.length==0)
        return;
    quickSort(a,0,a.length-1);
}

public void quickSort(int[] a,int low,int high){
    if (low>high)
        return;
    int k = divide(a,low,high);
    quickSort(a,low,k-1);
    quickSort(a,k+1,high);
}

private int divide(int[] a, int low, int high) {
    int x = a[low];
    while (low<high){
        while (low<high && a[high]>=x)
            high--;
        a[low] = a[high];
        while (low<high && a[low]<x)
            low++;
        a[high] = a[low];
    }
    a[low] = x;
    return low;
}

    先从后面遍历,如果比第一个值小,则停止,再从前面开始遍历,如果比第一个值大,则停止,知道数组有序。

五、归并排序:归并排序是将两个有序的序列的有序合并。因此将两个有序子序列合并成一个有序序列是归并排序的基础算法。

    看代码。。。

public void quickSort(int[] a){
    if (a==null)
        return;
    if (a.length==0)
        return;
    quickSort(a,0,a.length-1);
}

public void quickSort(int[] a,int low,int high){
    if (low>high)
        return;
    int k = divide(a,low,high);
    quickSort(a,low,k-1);
    quickSort(a,k+1,high);
}

private int divide(int[] a, int low, int high) {
    int x = a[low];
    while (low<high){
        while (low<high && a[high]>=x)
            high--;
        a[low] = a[high];
        while (low<high && a[low]<x)
            low++;
        a[high] = a[low];
    }
    a[low] = x;
    return low;
}

    归并排序很稳定,而且效率也很高。

六、堆排序:堆排序是在直接选择排序的基础上借助于堆而形成的一种排序方法。

    看代码...

public void heapSort(int[] a){
    if (a==null)
        return;
    if (a.length==0)
        return;
    int len = a.length;
    buildHeap(a,len);
    while (len>1){
        swap(a,0,len-1);
        len--;
        adjustHeap(a,0,len);
    }
}

private void adjustHeap(int[] a, int i, int len) {
    int maxIndex = i;
    if (2*i < len && a[2*i]>a[maxIndex])
        maxIndex = 2*i;
    if (2*i+1 < len && a[2*i+1]>a[maxIndex])
        maxIndex = 2*i+1;
    if (maxIndex != i){
        swap(a,i,maxIndex);
        adjustHeap(a,maxIndex,len);
    }
}

private void buildHeap(int[] a, int len) {
    for (int i = (len-1)/2;i>=0;i--)
        adjustHeap(a,i,len);
}

    其实堆排序只要了解原理就很容易写出来,反正我不喜欢它。

七、计数排序:基数排序就是找出线性表中的最小值和最大值,然后将之间出现的元素放到一个数组中计数,当计数完成,再遍历一遍不就是已经排好序的数据了吗

    再说一遍,看代码。。。

public void countSort(int[] a){
    if (a==null)
        return;
    if (a.length==0)
        return;
    int max = a[0],min = a[0];
    for (int i=0;i<a.length;i++){
        if (a[i]<min)
            min = a[i];
        if (a[i]>max)
            max = a[i];
    }

    int[] t = new int[max-min+1];
    int abs = 0-min;
    for (int i=0;i<a.length;i++){
        t[a[i]+abs]++;
    }

    int i = 0,index = 0;
    while (index<a.length){
        if (t[i]!=0){
            a[index++] = i-abs;
            t[i]--;
        }else {
            i++;
        }
    }
}

    计数排序很长适用于稳定在某一区间的数据,在这种情况下,时间复杂对和空间复杂度都很惊人

八、二分查找:博主目前能拿出手的就是二分查找了

    不说了,心酸,看代码吧

public int binSearch(int[] a,int x){
    if (a==null)
        return -1;
    if (a.length==0)
        return -1;
    int left = 0;
    int right = a.length-1;
    int mid = (left+right)/2;
    while (a[mid]!=x){
        if (a[mid]<x){
            left = mid+1;
        }else {
            right = mid-1;
        }

        if (left>right)
            return -1;
        mid = (left+right)/2;
    }
    return mid;
}
    Note that the premise array of binary search is ordered in advance.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325705337&siteId=291194637