Sorting Algorithms in a few Words

Terry Tsang @ 青鳥脈博
3 min readFeb 27, 2021

This note contains the basic ideas of different sorting algorithms in brief and plain words. It is for my own revision purpose and does not guarantee to be fully accurate.

Selection Sort

  • Start from A[0] (subject), find the smallest number among all elements
  • Swap the subject element with the smallest number that was found
  • Repeat the above steps start from the next position (A[1])
  • Keep on repeating until the last cycle
  • It is basically the worst sorting algorithm as you must check until the last cycle and can’t skip checking any element
  • Runtime: O(n²)

Bubble Sort

  • Start from A[0], compare A[0] with A[1]. Swap them if A[0] is larger
  • Shift the comparison window by 1. That’s mean A[1] and A[2] are to be compared in the next step
  • Repeat until the last 2 elements are checked. By then the largest element will be at the last position and fixed
  • Restart the checking from A[0]. Do the comparison and swap from A[0] until the last unfixed element
  • Keep on repeating until no element is swapped in a cycle or reach the last cycle
  • Runtime: O(n²)

Insertion Sort

  • Start from A[1], compare A[1] with A[0]. Swap them if A[1] is smaller

--

--

Terry Tsang @ 青鳥脈博

正在德國深造的90後香港電腦工程師。偶爾寫寫電腦技術、時政、分享攝影作品等。| 本站: https://bluebirdbeats.com/ | fb: fb.me/archerindigo | ig: terrytsang.indigo