Thursday, May 4, 2023

Sorting in Data Structures

 

Sorting in Data Structures

1️⃣ What is Sorting?

Sorting is the process of arranging elements in a particular order (ascending or descending). It helps in efficient searching, data retrieval, and processing.

2️⃣ Types of Sorting Algorithms

Sorting algorithms are mainly categorized into:

  • Comparison-based Sorting (Bubble Sort, Selection Sort, Quick Sort, Merge Sort, etc.)
  • Non-comparison Sorting (Counting Sort, Radix Sort, Bucket Sort, etc.)

3️⃣ Common Sorting Algorithms

AlgorithmBest CaseWorst CaseAverage CaseSpace ComplexityStable?
Bubble SortO(n)O(n²)O(n²)O(1)✅ Yes
Selection SortO(n²)O(n²)O(n²)O(1)❌ No
Insertion SortO(n)O(n²)O(n²)O(1)✅ Yes
Merge SortO(n log n)O(n log n)O(n log n)O(n)✅ Yes
Quick SortO(n log n)O(n²)O(n log n)O(log n)❌ No
Heap SortO(n log n)O(n log n)O(n log n)O(1)❌ No
Counting SortO(n + k)O(n + k)O(n + k)O(n + k)✅ Yes
Radix SortO(nk)O(nk)O(nk)O(n + k)✅ Yes

4️⃣ Sorting Algorithm Implementations in C

✅ 1. Bubble Sort (Simple but Inefficient)

Bubble Sort repeatedly swaps adjacent elements if they are in the wrong order.


#include <stdio.h> void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { // Swap int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } void printArray(int arr[], int n) { for (int i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); } int main() { int arr[] = {5, 3, 8, 4, 2}; int n = sizeof(arr) / sizeof(arr[0]); bubbleSort(arr, n); printf("Sorted array: "); printArray(arr, n); return 0; }

Simple but slow (O(n²))
Not suitable for large datasets


✅ 2. Insertion Sort (Efficient for Small Datasets)

Insertion Sort builds a sorted array one item at a time.


#include <stdio.h> void insertionSort(int arr[], int n) { for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } void printArray(int arr[], int n) { for (int i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); } int main() { int arr[] = {9, 7, 5, 3, 1}; int n = sizeof(arr) / sizeof(arr[0]); insertionSort(arr, n); printf("Sorted array: "); printArray(arr, n); return 0; }

Good for small datasets (O(n²))
Performs well when data is nearly sorted


✅ 3. Merge Sort (Efficient, Uses Divide & Conquer)

Merge Sort divides the array into two halves, sorts them, and merges them.


#include <stdio.h> void merge(int arr[], int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; int L[n1], R[n2]; for (int i = 0; i < n1; i++) L[i] = arr[left + i]; for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j]; int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (L[i] <= R[j]) arr[k++] = L[i++]; else arr[k++] = R[j++]; } while (i < n1) arr[k++] = L[i++]; while (j < n2) arr[k++] = R[j++]; } void mergeSort(int arr[], int left, int right) { if (left < right) { int mid = left + (right - left) / 2; mergeSort(arr, left, mid); mergeSort(arr, mid + 1, right); merge(arr, left, mid, right); } } void printArray(int arr[], int n) { for (int i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); } int main() { int arr[] = {12, 11, 13, 5, 6, 7}; int n = sizeof(arr) / sizeof(arr[0]); mergeSort(arr, 0, n - 1); printf("Sorted array: "); printArray(arr, n); return 0; }

Efficient (O(n log n))
Uses extra memory (O(n))


✅ 4. Quick Sort (Best for Large Datasets)

Quick Sort picks a pivot, partitions the array, and recursively sorts both halves.


#include <stdio.h> void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = (low - 1); for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; swap(&arr[i], &arr[j]); } } swap(&arr[i + 1], &arr[high]); return (i + 1); } void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } void printArray(int arr[], int n) { for (int i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); } int main() { int arr[] = {10, 7, 8, 9, 1, 5}; int n = sizeof(arr) / sizeof(arr[0]); quickSort(arr, 0, n - 1); printf("Sorted array: "); printArray(arr, n); return 0; }

Fastest in practice (O(n log n) average case)
Worst-case O(n²) if poorly implemented

No comments:

Post a Comment

Complete Binary Tree in Data Structures

  Complete Binary Tree in Data Structures 🌳 A Complete Binary Tree (CBT) is a type of Binary Tree where: ✔ All levels except possibly t...