Common sorting algorithm learning summary

This switched: http://www.codeceo.com/article/10-sort-algorithm-interview.html#0-tsina-1-10490-397232819ff9a47a7b7e80a40613cfe1

Only for personal learning record, invasion deleted!

 

Bubble Sort

To the top of the exchange to an adjacent element through a compare and swap small numbers.

This disordered sequence of 5,3,8,6,4 bubble sort.

First bubbling from back to front, 4 and 6 compared to the exchange 4 to the front, into the sequence 5,3,8,4,6.

Similarly exchange 4 and 8, become 5,3,4,8,6   

3 and 4 do not need to exchange.

Exchange 3 and 5, becomes 3,5,4,8,6,3.

Such a bubble would be finished, the smallest number 3 routed to the front.

The sequence of the rest of the bubble in turn will get an ordered sequence. Bubble sort time complexity is O (n ^ 2).

/ ** 
     * bubble sort algorithm 
     * @param ARR
      * / 
    public  static  void bubbleSort ( int [] ARR) { 
        System.out.println ( "Sort front:" );
         for ( int I: ARR) { 
            the System.out .print (I + "" ); 
        } 
        System.out.println (); 

        IF (ARR == null || arr.length == 0 ) {
             return ; 
        } 
        for ( int I = 0; I <arr.length; ++ I ) {
             for (int j = arr.length - 1; j > i; j--){
                if(arr[j] < arr[j - 1]){
                    int temp = arr[j - 1];
                    arr[j -1] = arr[j];
                    arr[j] = temp;
                }
            }
        }

        System.out.println("排序后:");
        for (int i : arr) {
            System.out.print(i+"  ");
        }
    }

 

Selection Sort

Select sort and bubble sort of thinking is actually somewhat similar, they are in a sort after the smallest element into the first. But different processes, by comparing the bubble sort is adjacent to and exchange. The selection sort is by choice of the whole. For chestnut, the random sequence of 5,3,8,6,4 simple selection sort, first select the minimum number of 5 to outside and exchange 5, 3 and 5 is switched to select, sort, it becomes a the 3,5,8,6,4 for the rest of the sequence and switching time, eventually you will get an ordered sequence. In fact, it can be seen as optimized selection sort bubble sort, because its the same purpose, but only in determining the selection sort only be exchanged under the premise of the minimum number of significantly reducing the number of exchanges. Selection sort time complexity of O (n ^ 2)

public static void selectSort(int[] arr){
        if(arr == null || arr.length == 0){
            return;
        }
        int minIndex = 0;
        for(int i = 0; i < arr.length-1; i++){
            minIndex = i;
            for(int j = i+1; j<arr.length; j++){
                if(arr[j] < arr[minIndex]){
                    minIndex = j;
                }
            }
            if(minIndex != i){
                swap(arr,i,minIndex);
            }
        }
    }

    public static void swap(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

 

Insertion Sort

By comparing the position of the insertion element found suitable to achieve the purpose of the sort.

This random sequence of 5,3,8,6,4 simple insertion sort, the first assumption is correct the position of the first number of

3 to 5 and then into the front, after 5 to move one into 3,5,8,6,4

And 8 do not move, 6 inserted in front of 8, 8 move after one, 4 is inserted in the front 5, starting from 5 are moved back one.

Note that inserting a number of times to ensure that the number in front of this number have been ordered. Simple insertion sort time complexity is O (n ^ 2).

public static void insertSort(int[] arr){
if(arr == null || arr.length == 0){
return;
}
for (int i = 1; i < arr.length; i++) {
int j = i;
int target = arr[i]; //待插入的数

while (j > 0 && target < arr[j-1]){
// swap(arr,j,j-1);
// j--;
arr[j] = arr[j-1];
j--;
}

arr[j] = target;
}
}

 

Quick Sort

Bubble Sort is by comparing adjacent elements and switching to the smallest bubble to the top, and the quick sort is to compare and exchange decimals and rounded, so that not only the decimal bubble to the top and also to the large numbers sink below.

For chestnut: this disordered sequence of 5,3,8,6,4 quickly sort, the idea is to find the right pointer less than the reference number, the pointer left to find larger than the reference number, exchange of.

5,3,8,6,4 5 as a baseline for comparison, will eventually move to the left a small 5 5 5 larger than move to the right 5.

5,3,8,6,4 first set i, j two pointers are pointing at both ends, j pointer scans (think about why?) 4 to 5 small stop. Then scan i, 8 than five stops. Exchange i, j positions.

5,3,4,6,8 pointer j is then scanned again, at this time the pointer j scan 4 two meet. stop. 4 and then swap benchmark.

4,3,5,6,8 left after the first division reached 5 is smaller than the right than five goals. After about recursive sequence ordering, finally get an ordered sequence.

The top left to the question why should j pointer to move it? First, this is not absolute, depending on the position of the reference number, because in the last two hands meet, to exchange to meet baseline position. The first number is generally chosen as a benchmark, it is on the left, so the final number to meet and exchange benchmark, it must meet several smaller than the reference number. So j pointer to move to first find a few less than the reference number.

Quick sort is unstable, its time average time complexity is O (nlgn).

public static void quickSort(int[] arr, int left, int right){
        if(left>=right){
            return;
        }
        int pivotPos = partition(arr,left,right);
        quickSort(arr,left,pivotPos-1);
        quickSort(arr,pivotPos+1,right);
    }

    private static int partition(int[] arr, int left, int right) {
        int pivoKey = arr[left];
        int pivoPointer = left;
         the while (left < right) {
             the while (left <right && ARR [right]> pivoKey =) { // if the number of sides greater than or equal to the reference continues until it finds a value smaller than the reference number 
                right-- ; 
            } 
            the while (left <right && ARR [left] <= pivoKey) { 
                left ++ ; 
            } 
            the swap (ARR, left, right); 
        } 
        the swap (ARR, pivoPointer, left); // last reference number and the left and right pointers exchange number meet 
        return left; 
    } 

    public  static  void the swap ( int [] ARR,int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

 

Guess you like

Origin www.cnblogs.com/763977251-sg/p/11373235.html