Java implementation of selection sort and insertion sort

1, selection sort (Selection Sort)

       The performance of one of the most stable sorting algorithm , because no matter what the data go in all the time complexity of O (n2) of , so use it when the data size as small as possible. The only advantage is probably not take up extra memory space bar. In theory, may also choose to sort usually Sort method most people think of most of it.

       Selection Sort (Selection-sort) works: First, find the smallest sequence of unsorted (large) element, the starting position is stored in the sorted sequence, and then continue to find the minimum (large) elements from the remaining unsorted elements, then sorted into the end of the sequence. And so on, until all the elements are sorted. 

(1) Algorithm Description

Direct selection sort n records may be n-1 times via direct selection orderly sorting result. The algorithm is described as follows:

  • Initial state: disordered region is R [1..n], ordered regions is empty;
  • Run ordering the i (i = 1,2,3 ... n-1) starts, the current ordered regions and disordered regions were R [1..i-1] and R (i..n). The trip from the current sorting disordered zone - Minimum recording key selected R [K], it will exchange with the first record disordered region R, so that R [1..i] and R [i + 1 ..n) were changed to increase the number of records and the number of records a new order to reduce a new area of ​​disorder;
  • n-1 the end of the trip, the ordered array.

(2) Code

    /**
     * 选择排序
     */
    public static int[] selectionSort(int[] array) {
        if (array.length == 0)
            return array;
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i; j < array.length; j++) {
                if (array[j] < array[minIndex]) //找到最小的数
                    minIndex = j; //将最小数的索引保存
            }
            int temp = array[minIndex];
            array[minIndex] = array[i];
            array[i] = temp;
        }
        return array;
    }

Best case: T (n) = O (n2) worst case: the case of the average T (n) = O (n2): T (n) = O (n2)

2, insertion sort (Insertion Sort)

Insertion sort (Insertion-Sort) works by constructing an ordered sequence, for unsorted data, from a forward scan in the sorted sequence, and find the corresponding insertion positions. In the realization of insertion sort, sort commonly used in-place (i.e., only need to use the sort of extra space O (1)), and therefore in the forward scan from the process, the need to repeatedly sorted elements gradually move rearward position , the latest element inserted into the space provided.

(1) Algorithm Description

Generally, insertion sort are implemented using in-place on the array. The algorithm is described as follows:

  • Starting with the first element, which can be considered to have been sorted;
  • Remove the next element from the forward scan has been sorted in the sequence of elements;
  • If the element (sorted) is greater than the new element, the element to the next position;
  • Repeat step 3 until it finds the sorted elements less than or equal to the new position of the element;
  • After the new element is inserted into this position;
  • Repeat steps 2 -

(2) Code

    /**
     * 插入排序
     */
    public static int[] insertionSort(int[] array) {
        if (array.length == 0)
            return array;
        int current;
        for (int i = 0; i < array.length - 1; i++) {
            current = array[i + 1];
            int preIndex = i;
            while (preIndex >= 0 && current < array[preIndex]) {
                array[preIndex + 1] = array[preIndex];
                preIndex--;
            }
            array[preIndex + 1] = current;
        }
        return array;
    }

Best case: T (n) = O (n) worst case: the case of the average T (n) = O (n2): T (n) = O (n2)

 

 

 

 

 

 

 

Published 91 original articles · won praise 193 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_44057443/article/details/92016327