This is a refresher of sorting algorithms since I recently realized that I don’t remember how a lot of the most common sorting algorithms work. I’m only going to focus on arrays on this article since it is the most common structure for these kind of problems.
This is the first algorithm we learn at school. It is not very efficient(O(n ^ 2) in most of the cases) but it is pretty easy to implement.
- Grab the first two elements(0 and 1) in an array and compare them
- If the element in the left is higher than the elements in the right then swap them
- Grab the next two elements(1 and 2) and do the same
- Repeat until the greatest element is in the far right
- Do the same starting from the first two elements(0 and 1) but ending before reaching the last element(which is already sorted)
- At the end the array will be sorted