sorting
sorting
Bubble Sort:
BubbleSort(A)
for i = 0 to length(A)-1
for j = 0 to length(A)-i-2
Explanation:
Bubble Sort repeatedly compares adjacent elements and swaps them if they are in
the wrong order. After each pass, the largest unsorted element moves to its correct
position. This process is repeated until no more swaps are needed. It has a time
complexity of O(n²), making it inefficient for large datasets.
2. Selection Sort:
SelectionSort(A)
for i = 0 to length(A)-1
minIndex = i
minIndex = j
Explanation:
Selection Sort repeatedly selects the smallest (or largest, depending on sorting
order) element from the unsorted part of the array and swaps it with the first
unsorted element. It doesn’t require a lot of swaps, but it still has a time
complexity of O(n²), making it inefficient for large datasets.
3. Insertion Sort:
InsertionSort(A)
for i = 1 to length(A)-1
key = A[i]
j = i-1
A[j+1] = A[j]
j = j-1
A[j+1] = key
Explanation:
Insertion Sort builds a sorted portion of the array one element at a time. For each
element, it compares it with elements in the sorted part and inserts it in its correct
position. It’s efficient for small datasets or lists that are already mostly sorted.
Best-case time complexity is O(n) when the array is already sorted, and worst-case
time complexity is O(n²).
4. Merge Sort:
MergeSort(A)
if length(A) > 1
mid = length(A) / 2
L = A[0..mid-1], R = A[mid..end]
MergeSort(L), MergeSort(R)
merge(L, R, A)
Explanation:
Merge Sort is a divide-and-conquer algorithm that divides the array into two halves,
recursively sorts each half, and then merges the two sorted halves. It guarantees a
time complexity of O(n log n) and is efficient for large datasets, but it requires extra
memory for temporary arrays.
5. Quick Sort:
pivot = A[high]
i = low-1
i += 1
return i+1
Explanation:
6. Heap Sort:
HeapSort(A)
BuildMaxHeap(A)
for i = length(A)-1 to 1
MaxHeapify(A, 0)
BuildMaxHeap(A)
for i = length(A)//2 - 1 to 0
MaxHeapify(A, i)
MaxHeapify(A, i)
largest = i
largest = left
largest = right
if largest != i
MaxHeapify(A, largest)
Explanation:
Heap Sort builds a max heap from the input array and repeatedly extracts the
largest element (the root of the heap) by swapping it with the last element. After
each extraction, the heap is restructured. The time complexity of Heap Sort is O(n
log n), and it doesn’t require extra memory like Merge Sort.
7. Counting Sort:
CountingSort(A, k)
C = array[k] initialized to 0
for i = 0 to length(A)-1
C[A[i]] += 1
for i = 1 to k
C[i] += C[i-1]
for i = length(A)-1 to 0
B[C[A[i]]-1] = A[i]
C[A[i]] -= 1
Explanation:
Counting Sort assumes that the input consists of integers in a known range. It
counts the number of occurrences of each element and uses these counts to place
the elements in the correct position in the output array. It has a time complexity of
O(n+k), where k is the range of the input, making it efficient for large datasets with a
small range of values.
8. Radix Sort:
RadixSort(A, d)
for i = 1 to d
Explanation:
Radix Sort processes each digit of the numbers in the array one at a time, starting
from the least significant digit, using a stable sorting algorithm (like Counting Sort)
to sort based on each digit. It’s efficient for sorting numbers with a fixed number of
digits and has a time complexity of O(d(n+k)), where d is the number of digits and k
is the range of digits.
9. Bucket Sort:
BucketSort(A)
for i = 0 to length(A)-1
Explanation:
Bucket Sort distributes elements into a number of buckets based on their value
range. Each bucket is then sorted individually, usually using another sorting
algorithm, and the buckets are concatenated to get the final sorted list. The time
complexity is O(n) when the input is uniformly distributed, but it depends on how
the data is divided into buckets.
Detailed Explanation:
• Selection Sort: Reduces the number of swaps compared to Bubble Sort but
still O(n²).
• Insertion Sort: Efficient for small or nearly sorted data, O(n) in the best case.
• Quick Sort: Very efficient in practice, O(n log n) on average, but can degrade
to O(n²) in the worst case.
• Heap Sort: Always O(n log n), efficient for large datasets, and doesn’t require
extra space.
• Radix Sort: Good for sorting integers with a fixed number of digits, O(d(n+k)).
• Bucket Sort: Ideal for uniformly distributed data, can achieve O(n) under
optimal conditions.