C程序实现选择排序算法

Selection sort is an unstable, in-place sorting algorithm. The standard implementation is unstable but it can be made stable with a few modifications.

选择排序是一种不稳定的就地排序算法。 标准实现是不稳定的,但是可以通过一些修改使其变得稳定。

  1. A stable sorting algorithm is the one where two keys having equal values appear in the same order in the sorted output array as it is present in the input unsorted array.

    一种稳定的排序算法是一种具有相等值的键在排序后的输出数组中以与输入未排序数组中存在的键相同的顺序出现的算法

  2. An in-place sorting algorithm has various definitions but a more used one is - An in-place sorting algorithm does not need extra space and uses the constant memory for manipulation of the input in-place. Although, it may require some extra constant space allowed for variables.

    就地排序算法具有各种定义,但更常用的定义是-就地排序算法不需要额外的空间,并且使用常量内存来对就地输入进行操作。 虽然,它可能需要一些允许变量使用的额外常量空间。

It is an effective sorting algorithm with the worst time complexity of O(N^2) where N is the total number of elements. Selection sort functions by iteratively finding the smallest element and placing it at the start of the list. Thus, at the end of each iteration, the smallest element is placed at its current position in the sorted array.

这是一种有效的排序算法,具有最差的时间复杂度O(N ^ 2) ,其中N是元素总数。 选择排序通过迭代找到最小的元素并将其放置在列表的开头而起作用。 因此,在每次迭代结束时,最小的元素将被放置在排序数组中的当前位置。

Algorithm:

算法:

The algorithm maintains two sub-arrays. One sorted sub-array and another unsorted sub-array.

该算法维护两个子数组。 一个排序的子数组和另一个未排序的子数组。

  1. Find the smallest element in the unsorted array.

    在未排序的数组中找到最小的元素

  2. Swap this element with the element at the front of the whole array thus shifting the first smallest element to its correct position in the sorted array

    将该元素与整个数组前面的元素交换,从而将第一个最小的元素移动到其在已排序数组中的正确位置

  3. Repeat steps 1 - 2 for the next n-1 elements.

    对接下来的n-1个元素重复步骤1-2。

Pseudo-code:

伪代码:

1.	for i 

Example:

Input Array: 12 5 8 13

Iteration 1:

Minimum element in arr[0 - 3]: 5, shift it to the start of arr[0 - 3]
Array: 5 12 8 13 

Iteration 2:

Minimum element in arr[1 - 3]: 8, shift it to the start of arr[1 - 3]
Array: 5 8 12 13 

Iteration 3:

Minimum element in arr[2 - 3]: 12, shift it to the start of arr[2 - 3]
Array: 5 8 12 13

Last element gets sorted automatically.

Time Complexity: The time complexity of selection sort algorithm is

  1. Worst case: O(N^2) comparisons but O(N) swaps

  2. Average Case: Ɵ(N^2) comparisons but O(N) swaps

  3. Best case: Ω(N^2) comparisons but no swaps

  4. Space Complexity: Ɵ(1) constant

The best thing about this algorithm is that it always makes at most N swaps and hence, can be used when memory write is a costly operation.

Optimized Selection Sort Implementation:

Output:

Input Array:
12 8 5 10 13 9
Sorted Array:
5 8 9 10 12 13

Applications:

  1. Better than Insertion sort in performance as the number of swaps is at most N in selection as compared to N^2 in insertion sort.

  2. It can be used where writes are more expensive than reads, such as in EEPROM and Flash memory where the write operation lessens the lifespan of memory.

  3. It is not preferred over bubble sort and gnome sort due to higher time complexity.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.


翻译自: https://www.includehelp.com/c-programs/implement-selection-sort-algorithm.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值