Common ordering five categories

Initialize an array:

// initialize the array, a reference type packaging type Integer 
Private Final static Integer [] Array = {2,. 4,. 6,. 1,. 3,. 8,. 7, 10,. 9,. 5}; 

The basic idea of ​​(1) bubble sort: Duplicate the number of columns to sort through, a comparison of two elements, two adjacent elements are exchanged, until there is no need to exchange so far.

  a) comparing adjacent elements. If the first is greater than the second, the two of them exchanged;

  b) for the same work for each pair of adjacent elements, the last pair from the beginning to the end of the first pair. At this point, it should be the last element is the largest number;

  c) repeating the above steps for all elements, except the last one;

  d) continuing the above steps for each repetition of fewer and fewer elements, until there is no need to compare a pair of numbers;

/**
     * 冒泡排序
     */
    public static void bubbleSort() {
        int temp;
        int change = 1; //降低排序无用次数
        for (int i = 0; i < array.length - 1 && change == 1; i++) {
            change = 0;
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j+1]) {
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                    change = 1;
                }
            }
        }
    }

(2) select the basic idea of ​​the sort: Each data element to be sorted is selected from the minimum (or maximum) of an element, only in the starting position of the sequence, until all the data elements to be sorted row lasts!

/**
     * 简答选择排序
     */
    public static void changeSort() {
        int temp;
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if(array[i] > array[j]) {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
    }

(3) The basic idea of ​​insertion sort: a sort of recording each step to be, according to the size of key values ​​array row has already been inserted in the sorted place until fully inserted up!

/**
     * 插入排序
     */
    public static void insertSort() {
        int j, temp;
        for (int i = 1; i < array.length; i++) {
            temp = array[i];
            for (j = i - 1; j >= 0 && array[j] > temp; j--) {
                array[j+1] = array[j];
            }
            array[j+1] = temp;
        }
    }

The basic idea (4) way merge sort: the use of divide and conquer algorithm to divide a consolidated its array of groups divided until only one so far array element!

/ ** 
     * way merge sort 
     * @param Array 
     * @param left 
     * @param right 
     * / 
    public static void mergesort (Integer [] Array, int left, int right) { 
        IF (left == right) { 
            return; 
        } 
        MID = int (left + right) / 2; 
        mergesort (Array, left, MID); // recursive solution left 
        mergeSort (array, mid + 1, right); // recursive solution to the right of 

        int [] tempArray = new int [ array .length]; 
        int I = left; 
        int + J = MID. 1; 
        int K = left; 
        the while (I <J = MID || <= right) { 
            exists when the right interval // the comparison, or the left and the interval value smaller than the right interval value
            IF (J> right || (I <= MID && Array [I] <Array [J])) {
                tempArray [k ++] = array [ i ++]; // the value of the left section into a temporary array 
            } the else { 
                tempArray [K ++] = Array [J ++]; the presence of // the right interval values, and smaller values than the left section , into a temporary array 
            } 
        } 

        // the value of the temporary array are copied to the original array 
        for (K = left; K <= right; K ++) { 
            array [K] = tempArray [K]; 
        } 
    }

(5) The basic idea of ​​quicksort: A divide and conquer algorithm trip by ordering the data to be sorted into separate two parts, one part of all of the data smaller than any other part of the data, and then this way this part of the data for quick sorting, the entire sorting process can be recursive, and so reach the entire data programmed ordered sequence!

/ ** 
     * Quicksort 
     * @param Array 
     * @param left 
     * @param right 
     * / 
    public static void QUICKSORT (Integer [] Array, int left, int right) { 
        if left if (left> = right) {//> = right to organize a group of instructions have been completed 
            return; 
        } 
        int I = left; 
        int J = right; 
        int TEMP = Array [left]; // to find a stored value hub 
        the while (I <J) { 
            the while (I <J Array && [J]> = TEMP) { 
                J,; 
            } 
            Array [I] = Array [J]; 
            the while (I <J && Array [I] <= TEMP) { 
                I ++; 
            }
            Array [J] = Array [I];
        } 
        Array [I] = TEMP; 

        QUICKSORT (Array, left, I); // recursive left 
        quickSort (array, i + 1, right); // recursive the right 
    }
    

(6) output code:

  public static void main(String[] args) {
//        ManySort.bubbleSort();
//        changeSort();
//        insertSort();
//        mergeSort(array, 0, array.length - 1);
        quickSort(array, 0, array.length - 1);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

  Little progress every day, struggling to large companies, we continue to move forward!

 

Guess you like

Origin www.cnblogs.com/blogtech/p/11109559.html