You are on page 1of 10

Sorting Algorithms

A sorting algorithm arranges elements of an array or list in a specific order. Elements can be sorted in
ascending or descending order.

Unsorted array
12 4 18 1 16 7 10 15 2

Sorted array in ascending order.


1 2 4 7 10 12 15 16 18

Sorted array in descending order.


18 16 15 12 10 7 4 2 1

But, ascending order can mostly be seen. Therefore, the sorting algorithms are considered in ascending
order from here onward for convenience. Five sorting algorithms are manly focused in this article. They
are bubble sort, insertion sort, selection sort, merge sort, and quick sort.

Bubble sort
Bubble sort is the simplest sorting algorithm. It starts comparing adjacent elements from the left and
places the higher element on the right side. For the first pass, the largest element is placed at the
rightmost. For the next pass, the second largest element is placed at its right position. In this way, this
process iterates until all elements are sorted.

The method for bubble sorting:


static void bubbleSort(int arr[], int length)
{
int temp;
boolean swap;
for (int i = 0; i < length - 1; i++) {
swap = false;
for (int j = 0; j < length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {

// Swapping j and j+1th elements


temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swap = true;
}
}
if (swap == false)
break;
}
First pass
12 4 15 1 10 7

4 12 15 1 10 7

4 12 15 1 10 7

4 12 1 15 10 7

4 12 1 10 15 7

Second pass
4 12 1 10 7 15

4 12 1 10 7 15

4 1 12 10 7 15

4 1 10 12 7 15

Third pass
4 1 10 7 12 15

1 4 10 7 12 15

1 4 10 7 12 15

Forth pass
1 4 7 10 12 15

1 4 7 10 12 15

Fifth pass
1 4 7 10 12 15

Insertion sort
Insertion sort is similar to playing card game. In that way, the first element is considered as already
sorted and the current element which is in the unsorted part is compared with the predecessor. If the
element is less than the predecessor, those two elements are swapped (the smaller value is placed on
the left side). In this way, all cards are placed.

The method for insertion sorting:


void insertionSort(int arr[])
{
int size = arr.length;
for (int i = 1; i < size; ++i) {
int temp = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] > temp) {


arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}
}

First pass
12 4 15 1 10 7 Current element = 4

Second pass
4 12 15 1 10 7 Current element = 15

Third pass
4 12 15 1 10 7 Current element = 1

4 12 1 15 10 7

4 1 12 15 10 7

Forth pass
1 4 12 15 10 7 Current element = 10
1 4 12 10 15 7

1 4 10 12 15 7

Fifth pass
1 4 10 12 15 7 Current element = 7

1 4 10 12 7 15

1 4 10 7 12 15

1 4 7 10 12 15

1 4 7 10 12 15

Selection Sort
Selection sort selects the smallest element from the unsorted part of the list. Then, that element is
swapped with the element which is at the beginning of the unsorted part. In this way, all elements in the
unsorted part are sorted.

The method for selection sorting:


void selectionSort(int arr[])
{
int n = arr.length;

for (int i = 0; i < n-1; i++)


{
// Find the minimum element
int min = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[min])
min = j;
}

int temp = arr[min];


arr[min] = arr[i];
arr[i] = temp;
}
}
12 4 15 1 10 7 Min = 1

1 4 15 12 10 7 Min = 4

1 4 15 12 10 7 Min = 7

1 4 7 12 10 15 Min = 10

1 4 7 10 12 15 Min = 12

1 4 7 10 12 15

Merge sort
Merge sort follows the divide and conquer approach. If the list of elements is considered an array, It
divides the array into two halves from the middle(If there is an odd number of elements in the array, one
half holds an additional element). Then subarrays are sorted and sorted subarrays are merged together
for the final sorted array. Merge sort is a recursive algorithm that separated the array until subarrays are
having only one element and all are merged same as the steps that are splitted as before until the sorted
array arrives.

The method for merge sorting:

void merge(int arr[], int a, int b, int c)


{
//sizes of two subarrays
int n1 = b - a + 1;
int n2 = c - b;

// Create temporary arrays


int arr1[] = new int[n1];
int arr2[] = new int[n2];

// Copy data to temp arrays


for (int i = 0; i < n1; ++i)
arr1[i] = arr[a + i];
for (int j = 0; j < n2; ++j)
arr2[j] = arr[b + 1 + j];

// Merge the temp arrays


int i = 0, j = 0;
int k = a;
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j]) {
arr[k] = arr1[i];
i++;
}
else {
arr[k] = arr2[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = arr1[i];
i++;
k++;
}

while (j < n2) {


arr[k] = arr2[j];
j++;
k++;
}
}

void sort(int arr[], int l, int m)


{
if (l < m) {
int middle = l + (m - l) / 2;

// Sort first and second halves


sort(arr, l, middle);
sort(arr, middle + 1, m);

// Merge the sorted halves


merge(arr, l, middle, m);
}
}
12 4 15 1 10 7

12 4 15 1 10 7

12 4 15 1 10 7

12 4 15 1 10 7

4 12 15 1 10 7

4 12 15 1 7 10

1 4 7 10 12 15

Quick sort
Quick sort also follows the divide and conquer approach. It picks the pivot element and it is placed at the
correct position. Other elements are divided into two arrays, as one array holds smaller values than the
pivot value and the other array holds larger values than the pivot value. This process happens recursively
for the subarrays as well until all the elements are sorted. There are different ways of selecting pivot
value. It can be the leftmost, rightmost, middle or a random element.

If the starting value is less than the pivot value, it traverses to the right and compares the next element
until it is greater than or equal to the pivot value and starting value stops at that element. If the ending
value is greater than the pivot value, it traverses to the left side and compares the next element until it is
less than or equal to the pivot value and the ending value stops at that element. If those starting and
ending values are crossed each other, the pivot value and the ending value are swapped. Otherwise,
Starting and ending values are swapped.
int partition(int arr[], int start, int end) {
int pivot = arr[start]; // pivot element
int i = start;

for (int j = start + 1; j <= end; j++) {


if (arr[j] < pivot) {
i++; // increment index of smaller element
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i];
arr[i] = arr[start];
arr[start] = temp;
return i;
}

void quickSort(int a[], int start, int end) /* a[] = array to be sorted,
start = Starting index, end = Ending index */
{
if (start < end) {
int p = partition(a, start, end); // p is partitioning index
quickSort(a, start, p - 1);
quickSort(a, p + 1, end);
}
}
P – Pivot value
S – Start
E – End
0 1 2 3 4 5
12 4 15 1 10 7

S E

12 4 15 1 10 7

S E

12 4 7 1 10 15
E S

10 4 7 1 12 15

4 5
10 4 7 1 12 15
S E

10 4 7 1
SE

1 4 7 10

3
1 4 7 10
S E

1 4 7
E S
0
1 4 7
S E

4 7
E S
1 2
4 7

0 1 2 3 4 5
1 4 7 10 12 15
Time complexities
Sorting Best case Average case Worst case
Algorithm
Bubble sort 𝑂(𝑛) 𝑂(𝑛2 ) 𝑂(𝑛2 )
Insertion sort 𝑂(𝑛) 𝑂(𝑛2 ) 𝑂(𝑛2 )
Selection sort 𝑂(𝑛2 ) 𝑂(𝑛2 ) 𝑂(𝑛2 )
Merge sort 𝑂(𝑛 𝑙𝑜𝑔 𝑛) 𝑂(𝑛 𝑙𝑜𝑔 𝑛) 𝑂(𝑛 𝑙𝑜𝑔 𝑛)
Quick sort 𝑂(𝑛 𝑙𝑜𝑔 𝑛) 𝑂(𝑛 𝑙𝑜𝑔 𝑛) 𝑂(𝑛2 )

There are five sorting algorithms have been considered. All these sorting algorithms are used to
rearrange a given array or list of elements. In conclusion, quick sort is considered the fastest sorting
algorithm because of the performance in the average case for more inputs.

You might also like