Simple sort algorithm

Bubble Sort

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. It can be optimized by stopping the algorithm if inner loop didn’t cause any swap.

bubble-sort

Insertion Sort

Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands. It can also be useful when input array is almost sorted, only few elements are misplaced in complete big array.
We can use binary search to reduce the number of comparisons in normal insertion sort. Binary Insertion Sort uses binary search to find the proper location to insert the selected item at each iteration. In normal insertion, sorting takes O(i) (at i-th iteration) in worst case. We can reduce it to O(log i) by using binary search. The algorithm, as a whole, still has a running worst case running time of O ( n 2 ) O(n^2) O(n2) because of the series of swaps required for each insertion.

insertion-sort

Selection Sort

The selection sort algorithm first consider the data array as the combination of sorted part and unsorted part, then sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and swap it to the beginning of unsorted part, it also begins the ending of sorted part.
The good thing about selection sort is it never makes more than O(n) swaps and can be useful when memory write is a costly operation.
It can be made stable if instead of swapping the minimum element to its position but by pushing every element one step forward.

selection-sort

Algorithm nameStableIn-placeAverage Case Time complexitySpace complexity
Bubble SortYesYes O ( n 2 ) O(n^2) O(n2)O(1)
Insertion SortYesYes O ( n 2 ) O(n^2) O(n2)O(1)
Selection SortNoYes O ( n 2 ) O(n^2) O(n2)O(1)
Quick SortNoYesO(nlogn)O(1)
Merge SortYesNoO(nlogn)O(n)
Heap SortNoYesO(nlogn)O(1)
Bucket SortYesNoO(n)O(n)
Counting SortYesNoO(n)O(n+k)
Radix SortYesNoO(n)O(n+k)/O(n)
  • k: the max value of the input data/the length of the counting array
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值