Array Sorting Algorithms in Python
Array Sorting Algorithms in Python
Sorting algorithms are used to arrange elements in a list or array in a specific order, usually in
ascending or descending order. Below are some common sorting algorithms along with Python
implementations.
1. Bubble Sort
Bubble sort is a simple algorithm that repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order. The process repeats until the list is
sorted.
Time Complexity:
Python Code:
def bubble_sort(arr):
n = len(arr)
for i in range(n):
# Example usage
bubble_sort(arr)
2. Selection Sort
Selection sort divides the list into two parts: the sorted part and the unsorted part. It repeatedly
selects the smallest element from the unsorted part and swaps it with the first unsorted
element.
Time Complexity:
Python Code:
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_index = i
min_index = j
# Example usage
selection_sort(arr)
3. Insertion Sort
Insertion sort works by taking one element at a time and inserting it into its correct position in a
sorted portion of the list. It's similar to how you might sort playing cards in your hands.
Time Complexity:
Python Code:
def insertion_sort(arr):
key = arr[i]
j=i-1
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
# Example usage
insertion_sort(arr)
4. Merge Sort
Merge sort is a divide-and-conquer algorithm. It divides the array into two halves, sorts each
half recursively, and then merges the sorted halves. Merge sort has better performance
compared to the previous algorithms.
Time Complexity:
Python Code:
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
merge_sort(left_half)
merge_sort(right_half)
i=j=k=0
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
arr[k] = left_half[i]
i += 1
k += 1
arr[k] = right_half[j]
j += 1
k += 1
# Example usage
merge_sort(arr)
5. Quick Sort
Quick sort is another divide-and-conquer algorithm. It selects a pivot element from the array,
partitions the other elements into two sub-arrays (elements less than the pivot and elements
greater than the pivot), and recursively sorts the sub-arrays.
Time Complexity:
Python Code:
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
# Example usage
arr = [64, 34, 25, 12, 22, 11, 90]
sorted_arr = quick_sort(arr)
6. Heap Sort
Heap sort is based on the binary heap data structure. It converts the array into a binary heap (a
complete binary tree) and repeatedly extracts the maximum element from the heap and places
it at the end of the array.
Time Complexity:
Python Code:
import heapq
def heap_sort(arr):
# Example usage
sorted_arr = heap_sort(arr)
Time Complexity:
Python Code:
sorted_arr2 = sorted(arr2)
Selection
O(n²) O(n²) O(n²) O(1)
Sort
Insertion
O(n) O(n²) O(n²) O(1)
Sort
Each algorithm has its strengths and weaknesses, and the choice of which one to use depends
on the problem and the dataset. For practical purposes, Timsort (used in Python's sort()
method) is the most commonly used due to its performance and efficiency in handling both
sorted and unsorted data.