You know and you do not know the sort of choice

1. What is the selection sort?

First affixed get down from the wiki definition of the sort of choice.

Selection Sort (Selection sort) is a straightforward sorting algorithm. It works as follows. 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) element from the remaining elements of unsorted and sorted into the end of the sequence. And so on, until all the elements are sorted.

More straightforward explanation is that every elected maximum or minimum element from an array, and then into the left side of the array.

2. Select the sorting process show

The old rules, we look at the sort of selection process by moving the map view. The following gif from the wiki.

Then we pass gif I made, coupled with data and then look at the process. Our hypothesis is still to be sorted array [5, 1, 3, 7, 6, 2, 4].

3. Select the minimum algorithm

We use Java to achieve the most common, choose a minimum of selection sort, its code is as follows.

private void selectionSort(int[] arr) {
  int min;
  int minIndex;
  for (int i = 0; i < arr.length - 1; i++) {
    min = arr[i];
    minIndex = -1;
    for (int j = i; j < arr.length; j++) {
      if (arr[j] < min) {
        min = arr[j];
        minIndex = j;
      }
    }
    // 排序结束 交换位置
    if (minIndex != -1) {
      exchange(arr, i, minIndex);
    }
  }
}

private void exchange(int arr[], int i, int j) {
  int temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
}

int[] arr = new int[]{5, 1, 3, 7, 6, 2, 4};
selectionSort(arr);
System.out.println(Arrays.toString(arr)); // [1, 2, 3, 4, 5, 6, 7]

7 is assumed that the length of the array, then the algorithm needs to be six. If the length of the array is n, then the algorithm needs to n - 1 round.

Each round, algorithms will be sorted from the remaining elements, the smallest element is selected, and the current array index which is the start position i is an ordered sequence of elements of the exchange. Thus, after repeated sorting, forming an ordered array.

4. The maximum value selection algorithm

Above to achieve a minimum of code selection, then we continue to achieve maximum code selection.

private void selectionSort(int[] arr) {
  int max;
  int maxIndex;

  for (int i = 0; i < arr.length - 1; i++) {
    max = Integer.MIN_VALUE;
    maxIndex = -1;
    for (int j = 0; j < arr.length - i; j++) {
      if (max < arr[j]) {
        max = arr[j];
        maxIndex = j;
      }
    }

    // 排序结束 交换位置
    if (maxIndex != -1) {
      exchange(arr, maxIndex, arr.length - i - 1);
    }
  }
}

private void exchange(int arr[], int i, int j) {
  int temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
}

int[] arr = new int[]{5, 1, 3, 7, 6, 2, 4};
selectionSort(arr);
System.out.println(Arrays.toString(arr)); // [1, 2, 3, 4, 5, 6, 7]

The thought and the choice of algorithm is exactly the same as the minimum value, just choose the maximum, every time the maximum value of the remaining sequence into the far left of the ordered sequence of array.

So this, select the two most common sort of writing we have already achieved. Some brothers might think, this is not the end of the blog. In fact, we can think of that can be optimized from the point above two algorithms.

Since we have two options, one option minimum, alternatively maximum. So why do not we do two operations at the same time?

Here we have to implement this algorithm.

The maximum and minimum values ​​simultaneously selected

private void selectionSort(int[] arr) {
  int min;
  int max;
  int minIndex;
  int maxIndex;

  for (int i = 0; i <= arr.length / 2; i++) {
    min = Integer.MAX_VALUE;
    max = Integer.MIN_VALUE;
    minIndex = -1;
    maxIndex = -1;
    for (int j = i; j < arr.length - i; j++) {
      if (arr[j] < min) {
        min = arr[j];
        minIndex = j;
      }
      if (arr[j] > max) {
        max = arr[j];
        maxIndex = j;
      }
    }
    // 排序结束 交换位置
    if (minIndex != -1) {
      if (maxIndex == i) {
        maxIndex = minIndex;
      }
      exchange(arr, i, minIndex);
    }

    if (maxIndex != -1) {
      exchange(arr, maxIndex, arr.length - 1 - i);
    }
  }
}

private void exchange(int arr[], int i, int j) {
  int temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
}

int[] arr = new int[]{5, 1, 3, 7, 6, 2, 4};
selectionSort(arr);
System.out.println(Arrays.toString(arr)); // [1, 2, 3, 4, 5, 6, 7]

Because the maximum and minimum values ​​simultaneously selected, with respect to the two algorithms above, while selection algorithm execution times on a 50% reduction compared to the previous two algorithms.

Selecting at run time relative to minimum and maximum values ​​were reduced by 39.22% and 62.20%.

6. Summary

The following is a case of using three algorithms scrambled random array with a length of 10,000.

[0--10000] the scrambled array Taking the minimum Whichever is greater While the maximum value minimum value
100 times the average execution time (ms) 51 82 31
Execution times (times) 50004999 50004999 25005000

Finally, we look at the selected time complexity of sorting algorithms.

  • The best case is O (n ^ 2). Even if the entire array is ordered, selection sort execution process will be finished selecting the maximum or minimum, but not to carry out the exchange element.
  • The worst case is O (n ^ 2). Supra, will complete execution of selected maximum or minimum, and each element needs to be exchanged.

Spatial complexity is O (n), the above three algorithms sorting algorithm belonging to place, in addition to switching elements using a secondary space, no additional space applications, while Selection Sort Sort unstable.

Past articles:

Related:

  • Public micro-channel number: SH full stack notes (or directly adding the public number of micro search interface signal LunhaoHu)

Guess you like

Origin www.cnblogs.com/detectiveHLH/p/11119734.html