Sorting Algorithms

Sorting Algorithms Animations

The provided animations demonstrate the efficient sorting of data sets originating from various starting points, showcasing the effectiveness of different algorithms.

Click each image to see the animation take place.

Sorting Algorithm Insertion GIF
Insertion Sort
Sorting Algorithm Selection GIF
Selection Sort
Sorting Algorithm Selection GIF
Bubble Sort
Sorting Algorithm Selection GIF
Shell Sort
Sorting Algorithm Selection GIF
Merge Sort
Sorting Algorithm Selection GIF
Quick Sort

What is a sorting algorithm?

A sorting algorithm is a systematic approach used to arrange elements in a specific order within a collection, typically a list or an array. The primary objective of sorting is to organize data in a way that makes it easier to search, retrieve, and analyze. Sorting algorithms play a crucial role in computer science and various applications, ranging from database management to data visualization.

There are various sorting algorithms, each with its own set of advantages, disadvantages, and specific use cases. Here's a brief overview of the key concepts related to sorting algorithms:

Unsorted Array

9
1
3
2
7
7

Sorted Array

1
2
3
4
7
9

Insertion

Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.


To sort an array of size N in ascending order iterate over the array and compare the current element (key) to its predecessor, if the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.


Consider an example: arr[]: { 12, 11, 13, 5, 6 }

12
11
13
5
6


First Pass:

12
11
13
5
6
11
12
13
5
6


Second Pass:

11
12
13
5
6

Third Pass:

11
12
13
5
6
11
12
5
13
6
11
5
12
13
6
5
11
12
13
6

Fourth Pass:

5
11
12
13
6
5
11
12
6
13
5
11
6
12
13
5
6
11
12
13

5
6
11
12
13

Sorted array.

Selection


Selection sort is an in-place comparison sorting algorithm.

The algorithm divides the input list into two parts: a sorted sublist of items which is built up from left to right at the front (left) of the list and a sublist of the remaining unsorted items that occupy the rest of the list.

Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.


  • As we traverse through the array in an initial loop, we push forward through the array with a second pointer in a nested loop at the same time, comparing each value to the starting value (beginning with our first loop's initial index.) If we find a lower value, we set that new value as our new lowest value to be compared against, and continue pushing.
  • By doing this, we ensure that each time we traverse through the array we will always find the next lowest value. When we reach the end of our second loop, we swap the lowest value with our very first initial index's value, and proceed to the next step.
  • This could also be done in reverse order, by searching for the largest values, if we were interested in sorting from highest to lowest.

How efficient is it?

Selection Sort, while relatively simple to comprehend and implement, unfortunately lags behind other sorting algorithms like Quick Sort, Heap Sort, and Merge Sort for larger data sets.

However, since Selection Sort functions in-place and requires no auxiliary memory, it does have a space advantage over some other more complicated algorithms.

Generally speaking, Insertion Sort is likely to be a more performant alternative, although Selection Sort is still important to know and understand as a programmer and computer scientist.

Selection Sort has a best case, worst case, and average case runtime complexity of O(n^2), meaning it will always be quadratic in nature.

Bubble


Bubble sort algorithm is a simple sorting technique that compares two adjacent elements in an array and swaps them if they are in the wrong order. It keeps repeating this process until the array is sorted.

For example, the diagram below illustrates the different swaps/bubble that happens when the element on the left is greater than the element on the right.

Also, at the end of the array iteration, it checks to see if any swaps occurred; if so, the process is repeated; otherwise, the array is sorted.


Unsorted Array

[ 12, 3, 7, 4, 10 ]
[ 12, 3, 7, 4, 10 ]
Swap since 12 > 3
[ 3, 12, 7, 4, 10 ]
[ 3, 12, 7, 4, 10 ]
Swap since 12 > 7
[ 3, 7, 12, 4, 10 ]
[ 3, 7, 12, 4, 10 ]
Swap since 12 > 4
[ 3, 7, 4, 12, 10 ]
[ 3, 7, 4, 12, 10 ]
Swap since 12 > 10
[ 3, 7, 4, 10, 12 ]

Repeat the process since elements has been swapped.


[ 3, 7, 4, 10, 12 ]
[ 3, 7, 4, 10, 12 ]
No swap since 3 < 7
[ 3, 7, 4, 10, 12 ]
Swap since 7 > 4
[ 3, 4, 7, 10, 12 ]
[ 3, 4, 7, 10, 12 ]
No swap since 7 < 10
[ 3, 4, 7, 10, 12 ]
No swap since 10 < 12

Repeat the process since elements has been swapped.


[ 3, 4, 7, 10, 12 ]
[ 3, 4, 7, 10, 12 ]
No swap since 3 < 4
[ 3, 4, 7, 10, 12 ]
No swap since 4 < 7
[ 3, 4, 7, 10, 12 ]
No swap since 7 < 10
[ 3, 4, 7, 10, 12 ]
No swap since 10 < 12

End of array since no elements has swapped.


[ 3, 4, 7, 10, 12 ]

Sorted array.

Shell


The key idea behind Shell Sort is to repeatedly divide the input array into smaller subarrays and sort the elements within those subarrays using an insertion sort.

The algorithm works by defining a sequence of increment values (often referred to as the "gap" sequence), which determine the size of the subarrays. The elements that are a certain gap distance apart are compared and swapped if necessary. The gap is then reduced, and the process is repeated until the gap becomes 1. At this point, the algorithm performs a final insertion sort on the entire array.

Shell Sort has a time complexity that depends on the chosen gap sequence, and in practice, it exhibits better performance than simpler quadratic sorting algorithms. However, it is generally outperformed by more advanced sorting algorithms like quicksort and mergesort for larger datasets. Shell Sort is often used in scenarios where memory is limited, and more advanced algorithms may not be feasible.


Unsorted Array

8
6
7
2
1
4
5
3

Step 1

Step 2

8
6
7
2
1
4
5
3
8
1
6
4
7
5
2
3

Step 3

Now, the array looks like this.

1
4
5
2
8
6
7
3

Step 4

Step 5

1
4
5
2
8
6
7
3
1
5
8
7
4
2
6
3

Then these sublists will be sorted using insertion sort. After comparing and swapping the first sublist, the array would be the following.

1
4
5
2
7
6
8
3
4
2
6
3

After sorting the second sublist, the original array will be:

1
2
5
3
7
4
8
6

Then again, the interval will be decreased. Now the interval will be h=h/2 or 2/1 or 1. Hence we will use the insertion sort algorithm to sort the modified array.

Following is the step-by-step procedural depiction of insertion sort.


1
2
5
3
7
4
8
6
1
2
5
3
7
4
8
6
1
2
5
3
7
4
8
6
1
2
5
3
7
4
8
6
1
2
3
5
7
4
8
6
1
2
3
5
7
4
8
6
1
2
3
5
7
4
8
6
1
2
3
5
4
7
8
6
1
2
3
4
5
7
8
6
1
2
3
4
5
7
8
6
1
2
3
4
5
7
8
6
1
2
3
4
5
7
6
8
1
2
3
4
5
6
7
8

The interval is again divided by 2. By this time, the interval becomes 0.

As the interval is 0, the array is sorted

1
2
3
4
5
6
7
8

Sorted array.

Merge


Merge Sort is a popular and efficient divide-and-conquer sorting algorithm.

The primary idea behind Merge Sort is to divide the unsorted array into two halves, recursively sort each half, and then merge the sorted halves to produce a fully sorted array.

Merge Sort ensures stability, meaning that the order of equal elements in the original array is preserved in the sorted array. It has a guaranteed time complexity of O(n log n), making it efficient for large datasets. However, it comes with a space complexity of O(n) due to the need for additional space to store the merged subarrays.

Merge Sort is widely used in various applications and serves as a foundation for more advanced sorting algorithms. Its predictable and efficient performance makes it a reliable choice for sorting large datasets.


Unsorted Array

12
31
25
8
32
17
40
42

Step 1

12
31
25
8
32
17
40
42

Step 2

12
31
25
8
32
17
40
42

Step 3

12
31
25
8
32
17
40
42


Step 4

12
31
8
25
17
32
40
42

Step 5

8
12
25
31
17
32
40
42

Step 6


8
12
17
25
31
32
40
42

Sorted array.

Quick


QuickSort is a widely used sorting algorithm known for its efficiency and simplicity. It follows a divide-and-conquer approach to sort an array of elements. The algorithm works by selecting a "pivot" element from the array and partitioning the other elements into two sub-arrays based on whether they are less than or greater than the pivot. This process is then applied recursively to the sub-arrays. The key steps in QuickSort are:


Unsorted Array

8
7
6
1
0
9
2

Step 1

8
7
6
1
0
9
2

Step 2

1
0
2
8
7
9
6

Here's how we rearrange the array

8
7
6
1
0
9
2

8
7
6
1
0
9
2
(Second pointer)
(First pointer)

8
7
6
1
0
9
2
8
7
6
1
0
9
2
1
7
6
8
0
9
2
1
7
6
8
0
9
2
(Second pointer)
(First pointer)
1
0
6
8
7
9
2

1
0
6
8
7
9
2
1
0
2
8
7
9
6

Pivot elements are again chosen for the left and the right sub-parts separately. And, step 2 is repeated.

0
1
2
8
7
9
6
0
1
2
8
7
9
6
8
7
9
6
6
7
9
8
0
1
2
6
7
9
8
7
9
8
7
8
9
0
1
2
6
7
8
9
7
9

Finally you will be ended up with a sorted array.

0
1
2
6
7
8
9

Sorted array.