<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Ytl's Java Blog

    厚積而薄發(fā)---每一天都是一個(gè)全新的開始

    Selection Sort

    Selection sort is one of the O(n2) sorting algorithms, which makes it quite inefficient for sorting large data volumes. Selection sort is notable for its programming simplicity and it can over perform other sorts in certain situations (see complexity analysis for more details).

    Algorithm

    The idea of algorithm is quite simple. Array is imaginary divided into two parts - sorted one and unsorted one. At the beginning, sorted part is empty, while unsorted one contains whole arrayAt every step, algorithm finds minimal element in the unsorted part and adds it to the end of the sorted one. When unsorted part becomes empty, algorithmstops.

    When algorithm sorts an array, it swaps first element of unsorted part with minimal element and then it is included to the sorted part. This implementation of selection sort in not stable. In case of linked list is sorted, and, instead of swaps, minimal element is linked to the unsorted part, selection sort is stable.

    Let us see an example of sorting an array to make the idea of selection sort clearer.

    Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort.

    Selection sort example

    Complexity analysis

    Selection sort stops, when unsorted part becomes empty. As we know, on every step number of unsorted elements decreased by one. Therefore, selection sort makes n steps (n is number of elements in array) of outer loop, before stop. Every step of outer loop requires finding minimum in unsorted part. Summing up, n + (n - 1) + (n - 2) + ... + 1, results in O(n2) number of comparisons. Number of swaps may vary from zero (in case of sorted array) to n - 1 (in case array was sorted in reversed order), which results in O(n) number of swaps. Overall algorithm complexity is O(n2).

    Fact, that selection sort requires n - 1 number of swaps at most, makes it very efficient in situations, when write operation is significantly more expensive, than read operation.

    Code snippets

    Java

    public void selectionSort(int[] arr) {

          int i, j, minIndex, tmp;

          int n = arr.length;

          for (i = 0; i < n - 1; i++) {

                minIndex = i;

                for (j = i + 1; j < n; j++)

                      if (arr[j] < arr[minIndex])

                            minIndex = j;

                if (minIndex != i) {

                      tmp = arr[i];

                      arr[i] = arr[minIndex];

                      arr[minIndex] = tmp;

                }

          }

    }

    Python

         for i in range(len(L)-1):
              minIndex = i
              minValue = L[i]
              j = i + 1
              while j< len(L):
                   if minValue > L[j]:
                        minIndex = j
                        minValue = L[j]
                   j += 1
              if minIndex != i:
                   temp       = L[i]
                   L[i]       = L[minIndex]
                   L[minIndex] = temp


    主站蜘蛛池模板: 色网站在线免费观看| 亚洲影院天堂中文av色| 一级毛片在线完整免费观看| 成人免费无码精品国产电影| 亚洲码欧美码一区二区三区| 成年女人免费v片| 亚洲日韩国产二区无码| 日本免费一区二区三区最新| 国产精品亚洲精品爽爽| 亚洲国产综合人成综合网站| 好吊色永久免费视频大全| 精品亚洲一区二区三区在线观看| 亚洲视频在线免费| 久久精品7亚洲午夜a| 最近2019免费中文字幕视频三| 亚洲色图.com| 卡1卡2卡3卡4卡5免费视频| 美女裸体无遮挡免费视频网站| 亚洲国产日韩在线观频| 久久福利青草精品资源站免费| 亚洲狠狠狠一区二区三区| 国产精品久久久久久久久久免费| 亚洲日本va一区二区三区 | 一区二区无码免费视频网站| 亚洲欧美综合精品成人导航| 亚洲熟妇少妇任你躁在线观看无码| jyzzjyzz国产免费观看| 337p欧洲亚洲大胆艺术| 丁香花免费高清视频完整版| 牛牛在线精品免费视频观看| 国产AV无码专区亚洲精品| 黄页网站免费观看| 国产亚洲视频在线观看| 亚洲高清国产AV拍精品青青草原| 91香蕉成人免费网站| 国产亚洲精品2021自在线| 亚洲国产人成网站在线电影动漫| 日韩精品成人无码专区免费 | 久久午夜伦鲁片免费无码| 亚洲精品乱码久久久久久蜜桃图片| 中文字幕亚洲一区二区va在线|