Sorting is the process of arranging elements in a specific order (ascending or descending). Sorting algorithms like Merge Sort and Quick Sort are essential parts of DSA in Python, especially for optimizing performance. Sorting improves the efficiency of other operations like searching.
Bubble Sort is a simple sorting algorithm that works by repeatedly comparing adjacent elements in a list and swapping them if they are in the wrong order. This “bubbling” process continues until the entire list is sorted. It is easy to implement and understand, but inefficient for large datasets due to its time complexity of O(n²). It works best for small or nearly sorted lists.
In Python, bubble sort can be implemented using nested loops. The main operations performed in bubble sort are:
Bubble sort is widely used for educational purposes, small datasets, and scenarios where simplicity matters more than efficiency.
Example :
Input:
# Bubble Sort
arr = [6,3,2,8]
for i in range(len(arr)):
for j in range(0, len(arr)-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
print("Sorted array:", arr)
Output:
Sorted array: [3,2,6,8]
Merge Sort is a divide-and-conquer sorting algorithm that works by splitting the array into halves, sorting each half recursively, and then merging the sorted halves back together. It guarantees a time complexity of O(n log n) and is a stable sorting algorithm, meaning the relative order of equal elements is preserved.
In Python, merge sort can be implemented using recursion. The main operations performed in merge sort are:
Merge sort is widely used in large datasets, external sorting, database systems, and applications where stability is important.
# Merge Sort
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr)//2
L = arr[:mid]
R = arr[mid:]
merge_sort(L)
merge_sort(R)
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j < len(R):
arr[k] = R[j]
j += 1
k += 1
arr = [38, 27, 43, 10]
merge_sort(arr)
print("Sorted array:", arr)
Sorted array: [10,27,38,43]
Quick Sort is a divide-and-conquer sorting algorithm that works by partitioning the array around a pivot element, placing smaller elements on the left and larger elements on the right. It then recursively sorts the left and right partitions. Quick Sort is fast in practice, with an average time complexity of O(n log n), but can degrade to O(n²) if the pivot is poorly chosen.
In Python, quick sort can be implemented using recursion. The main operations performed in quick sort are:
Quick Sort is widely used in large datasets, standard library sorting functions, and applications where average-case efficiency is critical.
# Quick Sort
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr)//2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
arr = [8,4,3,5,10,5,2]
print("Sorted array:", quick_sort(arr))
Output:
Sorted array: [2,3,4,5,7,8,10]
Top Tutorials
Related Articles