Sorting Algorithms
Updated Feb 05, 20253 min read
Sorting
Sorting is the process of arranging a collection of data into either ascending or descending order. Generally the output is arranged in sorted order so that it can be easily interpreted. Sometimes sorting at the initial stages increases the performances of an algorithm while solving a problem.
Sorting techniques are broadly classified into two categories:
- Internal Sort: - Internal sorts are the sorting algorithms in which the complete data set to be sorted is available in the computer’s main memory. Below are the list of some internal Sorting algorithms:
i. Bubble Sort
ii. Selection Sort
iii. Insertion Sort
- External Sort: - External sorting techniques are used when the collection of complete data cannot reside in the main memory but must reside in secondary storage for example on a disk.
Bubble Sort:
It is the simplest sorting algorithm in which each pair of adjacent elements is compared and exchanged if they are not in order. This algorithm is not recommended for use for a bigger size array because its average and worst case complexity are of Ο(n2) where n is the number of elements in an array.This algorithm is known as bubble sort, because the largest element in the given unsorted array, bubbles up towards the last place in every cycle/pass .
I am going to demonstrate through tabluar view
First Pass
23 | 18 | 15 | 37 | 8 | 11 |
18 | 23 | 15 | 37 | 8 | 11 |
18 | 15 | 23 | 37 | 8 | 11 |
18 | 15 | 23 | 37 | 8 | 11 |
18 | 15 | 23 | 8 | 37 | 11 |
18 | 15 | 23 | 8 | 11 | 37 |
Second Pass
18 | 15 | 23 | 8 | 11 | 37 |
15 | 18 | 23 | 8 | 11 | 37 |
15 | 18 | 23 | 8 | 11 | 37 |
15 | 18 | 8 | 23 | 11 | 37 |
15 | 18 | 8 | 11 | 23 | 37 |
Third Pass
15 | 18 | 8 | 11 | 23 | 37 |
15 | 18 | 8 | 11 | 23 | 37 |
15 | 8 | 18 | 11 | 23 | 37 |
15 | 8 | 11 | 18 | 23 | 37 |
Fourth Pass
15 | 8 | 11 | 18 | 23 | 37 |
8 | 15 | 11 | 18 | 23 | 37 |
8 | 11 | 15 | 18 | 23 | 37 |
Fifth Pass
8 | 11 | 15 | 18 | 23 | 37 |
8 | 11 | 15 | 18 | 23 | 37 |
8 | 11 | 15 | 18 | 23 | 37 |
Pseudo Code:
public void bubbleSort(int[] arr) {
int n = arr.length;
boolean swapped;
// Repeat the process until no swaps are made in a pass
do {
swapped = false;
// Loop through the array elements
for (int i = 0; i < n - 1; i++) {
// If the current element is greater than the next element, swap them
if (arr[i] > arr[i + 1]) {
// Swap arr[i] and arr[i+1]
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
// Mark that a swap occurred during this pass
swapped = true;
}
}
// Optionally, you can reduce 'n' here since the largest element is in place after each pass.
} while (swapped);
}
Complexity Analysis
Time Complexity:
- Worst-case: O(n2)(occurs when the list is sorted in reverse order).
- Best-case:O(n)O(n) O(n) (occurs when the list is already sorted; with the optimization that stops if no swaps occur).
- Average-case:O(n2)O(n^2) O(n2).
Space Complexity:
O(1)O(1)O(1) – Bubble sort is an in-place sorting algorithm, meaning it requires only a constant amount of additional memory.
