選擇排序

選擇排序是一個簡單的排序演算法。這個排序演算法是一個基於就地比較演算法,其中列表被分為兩部分,排序部分,左端和右端未分類的一部分。最初排序的部分是空的,未分類的部分是整個列表。

最小的元素是從無序數組選擇並交換,使用最左邊的元素,以及元素變成有序數組的一部分。此過程繼續由一個元素向右移動無序數組的邊界。

該演算法是不適合大的數據集,作為它平均值和最壞情況的複雜性是 O(n2) 其中n是專案的數量。

偽代碼

Selection Sort ( A: array of item)
   procedure selectionSort( A : array of items )
   int indexMin

   for i = 1 to length(A) - 1 inclusive do:
      /* set current element as minimum*/
      indexMin = i
      /* check the element to be minimum */

      for j = i+1 to length(A) - 1 inclusive do:
         if(intArray[j] < intArray[indexMin]){
            indexMin = j;
         }

      end for

      /* swap the minimum element with the current element*/
      if(indexMin != i) then
         swap(A[indexMin],A[i])
      end if

   end for
end procedure

要查看C編程語言選擇排序的實現,請點擊這裏


上一篇: 插入排序 下一篇: 選擇排序實例程式(C語言)