选择排序是一个简单的排序算法。这个排序算法是一个基于就地比较算法,其中列表被分为两部分,排序部分,左端和右端未分类的一部分。最初排序的部分是空的,未分类的部分是整个列表。
最小的元素是从无序数组选择并交换,使用最左边的元素,以及元素变成有序数组的一部分。此过程继续由一个元素向右移动无序数组的边界。
该算法是不适合大的数据集,作为它平均值和最坏情况的复杂性是 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语言)