Sorting algorithms: Difference between revisions

From Wiki :-)
Jump to navigation Jump to search
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Definition ==
'''Stable''' sort: two elements with equal keys are in the same order after sorting. For instance, insertion sort, merge sort, bubble sort are stable. Quick sort, heap sort are '''unstable'''.
== Complexity ==
{| class="wikitable"
{| class="wikitable"
!Sorting algoritm
!Sorting algoritm
Line 6: Line 10:
!Worst-case space complexity
!Worst-case space complexity
|-
|-
|Bubblesort
|Bubble sort
|O(n<sup>2</sup>)
|O(n<sup>2</sup>)
|O(n)
|O(n)
Line 12: Line 16:
|O(1)
|O(1)
|-
|-
|Mergesort
|Merge sort
|O(n log n)
|O(n log n)
|O(n log n)
|O(n log n)
Line 18: Line 22:
|O(n) / O(1) with linked list
|O(n) / O(1) with linked list
|-
|-
|Quicksort
|Quick sort
|O(n<sup>2</sup>)
|O(n<sup>2</sup>)
|O(n log n)
|O(n log n)
|O(n log n)
|O(n log n)
|O(n) / O(1) with linked list
|O(n) / O(1) with linked list
|-
|Radix sort
|
|
|O(kn)
|
|}
|}


== References ==
== Resources ==

Latest revision as of 07:07, 3 October 2020

Definition

Stable sort: two elements with equal keys are in the same order after sorting. For instance, insertion sort, merge sort, bubble sort are stable. Quick sort, heap sort are unstable.

Complexity

Sorting algoritm Worst-case performance Best-case performance Average performance Worst-case space complexity
Bubble sort O(n2) O(n) O(n2) O(1)
Merge sort O(n log n) O(n log n) O(n log n) O(n) / O(1) with linked list
Quick sort O(n2) O(n log n) O(n log n) O(n) / O(1) with linked list
Radix sort O(kn)

Resources