Sorting Algorithms Explained With Examples πŸšƒ

Sorting Algorithms Explained With Examples πŸšƒ

Β·

4 min read

Table of contents

No heading

No headings in the article.

Introduction: Sorting is an essential operation in computer science, allowing us to arrange elements in a particular order. There are various sorting algorithms available, each with its own advantages and disadvantages. In this blog post, we will explore four popular sorting algorithms in C++: Selection Sort, Bubble Sort, Merge Sort, and Quick Sort. Along the way, we'll provide real-life examples to illustrate their practical applications. Let's dive in! πŸ’»πŸ“Š

  1. Selection Sort: Selection Sort works by repeatedly finding the minimum (or maximum) element from the unsorted portion of the array and placing it at the beginning. This process continues until the entire array is sorted.

Real-life example: Imagine you're organizing a stack of books on a shelf in ascending order based on their height. You start by finding the shortest book and placing it at the leftmost position. Then, you move on to the remaining books, finding the next shortest one and placing it next to the first book. This process continues until all the books are arranged in order of increasing height.

C++ code example:

void selectionSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex])
                minIndex = j;
        }
        swap(arr[i], arr[minIndex]);
    }
}
  1. Bubble Sort: Bubble Sort works by repeatedly swapping adjacent elements if they are in the wrong order. In each pass, the largest (or smallest) element "bubbles" up to its correct position.

Real-life example: Imagine you're blowing bubbles in a bubble bath, and you want to arrange the bubbles in ascending order based on their sizes. You start by comparing adjacent bubbles and swapping them if they're out of order. The larger bubbles gradually rise to the top, resulting in a sorted arrangement.

C++ code example:

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(arr[j], arr[j + 1]);
        }
    }
}
  1. Merge Sort: Merge Sort follows the divide-and-conquer approach. It recursively divides the array into two halves, sorts them separately, and then merges the sorted halves to produce the final sorted array.

Real-life example: Suppose you have two sorted lists of students: one sorted by their first names and the other sorted by their last names. To generate a single sorted list, you would compare the first elements from each list and choose the smaller one, gradually merging the two lists together.

C++ code example:

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);
    }
}
  1. Quick Sort: Quick Sort also employs the divide-and-conquer strategy. It selects a pivot element and partitions the array into two subarrays, one with elements smaller than the pivot and the other with elements greater than the pivot. The process is repeated recursively for each subarray.

Real-life example: Consider sorting a deck of cards. You can randomly select a card as the pivot, divide the remaining cards into two groups (smaller and larger), and recursively repeat the process for each group. Eventually, all the cards will be in their correct positions.

C++ code example:

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 pivot = partition(arr, low, high);
        quickSort(arr, low, pivot - 1);
        quickSort(arr, pivot + 1, high);
    }
}

Conclusion: Sorting algorithms play a crucial role in various real-life scenarios and computer science applications. In this blog post, we covered Selection Sort, Bubble Sort, Merge Sort, and Quick Sort with real-life examples to help you understand their concepts better. Feel free to explore and implement these algorithms in your own projects to optimize the sorting process. Happy coding! πŸ˜„πŸ‘¨β€πŸ’»