Java quick sort and merge sort Detailed

Quick Sort

Outline

Fast sorting algorithms learn before traversing Binary Tree is the idea, eventually to sort the array.

advantage:

For large amount of data sorted array, since the binary tree having a binary thought employed, it is faster sorting

Limit

It applies only to the order of sorting data storage structure (an array, the ArrayList, etc.), not a data structure of the chain

Algorithm thinking

I. target array into such an array. All data in the array somewhere to the left than the location of the data is small, the data are larger than the right of the location of the position data.

Realization of ideas:

1. Remove the array of data 0

 
 

2. Starting from the far right to traverse the array, if the data traversing the position of small data than the 0th position, the position data assigned to the left under the pointer position.

 

3. Change the traversal direction and start traversing from the left, if it is found to the left of the data is greater than 0 position data, the position data assigned to the second step position stay down and change direction.

 

4. Step 3 loop traversed until approximately coincides subscript
The position of the extracted value is assigned to the 0 position of the left and right hands at the end of the cycle dwell

II. Before the draw order traversal of ideas, recursive, the final completion of the sort.

Code

private void quickSort(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        int key = array[start];
        int left = start;
        int right = end;
        boolean direction = true;
        L1:
        while (left < right) {
            if (direction) {
                for (int i = right; i > left; i--) {
                    if (array[i] < key) {
                        array[left++] = array[i];
                        right = i;
                        direction = !direction;
                        continue L1;
                    }
                }
                right = left;
            } else {
                for (int i = left; i < right; i++) {
                    if (array[i] > key) {
                        array[right--] = array[i];
                        left = i;
                        direction = !direction;
                        continue L1;
                    }
                }
                left = right;
            }
        }
        array[left] = key;
        quickSort(array, start, left - 1);
        quickSort(array, left + 1, end);

    }

Test results

@Test
    public void testQuickSort() {
        int[] array = new int[]{1, 3, 4, 10, 2, 5, 6, 9, 7, 8};
        quickSort(array, 0, array.length - 1);
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

Print

1
2
3
4
5
6
7
8
9
10

Merge sort

Outline

Merge sort and quick sort same, the same reference binary thinking, the time complexity of O (n), and as one of the best ways to quickly sort large amounts of data sorting.

Ideas analysis

Merge sort is the target array into two arrays around, about two arrays must be ordered, and then merge the two arrays in order to achieve the sort. For any array may be all the data into a number of arrays, each array element is only one, and then twenty-two combined. (Therefore, merge sort of memory overhead will be more than quick sort)

Code

private void mergeSort(int[] array, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = (left + right) >> 1;
        mergeSort(array, left, mid);
        mergeSort(array, mid + 1, right);
        merge(array, left, mid + 1, right);
    }

    private void merge(int[] array, int left, int mid, int right) {
        int leftSize = mid - left;
        int rightSize = right - mid + 1;
        int[] leftArray = new int[leftSize];
        int[] rightArray = new int[rightSize];
        System.arraycopy(array, left, leftArray, 0, leftSize);
        System.arraycopy(array, mid, rightArray, 0, rightSize);
        int index=left;
        int leftIndex = 0;
        int rightIndex = 0;
        while (leftIndex<leftSize&&rightIndex<rightSize){
            if(leftArray[leftIndex]<rightArray[rightIndex]){
                array[index++] = leftArray[leftIndex++];
            }else {
                array[index++] = rightArray[rightIndex++];
            }
        }
        while (leftIndex<leftSize){
            array[index++] = leftArray[leftIndex++];
        }
        while (rightIndex<rightSize){
            array[index++] = rightArray[rightIndex++];
        }
    }

Test code

@Test
    public void testMergeSort() {
        int[] array = new int[]{1, 3, 4, 10, 2, 5, 6, 9, 7, 8};
        mergeSort(array, 0, array.length - 1);
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

Print

2
4
6
8
10

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160014.htm