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

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

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

    Ytl's Java Blog

    厚積而薄發---每一天都是一個全新的開始
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Sorting algorithms --Selection Sort

    Posted on 2011-05-06 16:16 ytl 閱讀(398) 評論(0)  編輯  收藏 所屬分類: Algorithms and programming concepts

    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在线无码影院观看| 亚洲AV无码久久精品狠狠爱浪潮| 亚洲VA中文字幕无码毛片| 成人国产网站v片免费观看 | 国产亚洲综合久久| h视频在线观看免费| 美女被cao免费看在线看网站| 国产免费看插插插视频| 青青草原精品国产亚洲av| 国产精品国产亚洲区艳妇糸列短篇| 毛片基地看看成人免费| 女人被弄到高潮的免费视频| 亚洲av成人无码久久精品| 美女黄频a美女大全免费皮| 噼里啪啦免费观看高清动漫4| jiz zz在亚洲| 国产一卡二卡四卡免费| 亚洲国产精品无码成人片久久| 亚洲国产欧美一区二区三区| 91麻豆国产免费观看| 国产亚洲精品a在线观看| 亚洲日韩国产二区无码| 国产一区二区三区免费视频| j8又粗又长又硬又爽免费视频| 亚洲乱亚洲乱妇无码麻豆| v片免费在线观看| 免费的一级片网站| 亚洲国产成人精品无码区在线秒播 | 亚洲精品国产精品乱码在线观看| 美女羞羞视频免费网站| 国产AV无码专区亚洲AV手机麻豆| 老司机午夜在线视频免费观| 中文字幕在亚洲第一在线 | 亚洲国产精品日韩av不卡在线| 亚洲精品一级无码鲁丝片| 亚洲成AV人影片在线观看| 久久久久亚洲AV成人网人人网站| 最近免费字幕中文大全视频 | 日韩成人毛片高清视频免费看| 午夜一级毛片免费视频|