DEV Community

Harsh Mishra
Harsh Mishra

Posted on

Selection Sort C++: Story

๐Ÿ‘‘ The Rite of the True Heir: The Selection Sort Saga

"Among many who claim the crown, only one is worthy.
Choose him, place him upon the throne,
and the line of kings shall be written."

โ€” The Book of Crowns


In the fractured kingdom, many warriors shouted of their worth. Each believed himself the strongest, yet none stood where destiny demanded.

The High Oracle commanded a ritual, not of chaos or battle, but of choice. In each moment, the smallest and humblest would be sought out among the many, lifted, and placed upon the next empty throne.

Thus began the Rite of the True Heir, known to the scribes as Selection Sort.


๐Ÿ“œ The Scroll of Crowns

#include <iostream>
#include <vector>
using namespace std;
Enter fullscreen mode Exit fullscreen mode

The scribes unrolled the scrolls (iostream) and laid out the line of would-be kings (vector). Upon these pages the names of the chosen would be recorded, one after another, in rightful order.


โš”๏ธ The Oracleโ€™s Decree

void selectionSort(vector<int>& arr) {
Enter fullscreen mode Exit fullscreen mode

The Oracle raised her hand. The ritual of selectionSort was spoken aloud, and the line of claimants trembled. Soon, one by one, the crowns would be assigned.


    int n = arr.size();
Enter fullscreen mode Exit fullscreen mode

The Herald counted the warriors. Their number, n, was the length of destiny โ€” for each throne would be filled in turn, from the smallest to the greatest.


๐Ÿ‘‘ The Search for the True Heir

    for (int i = 0; i < n - 1; i++) {
        int minIndex = i;
Enter fullscreen mode Exit fullscreen mode

The Oracleโ€™s gaze fell upon the first empty throne (i). She pointed to the warrior standing before it and declared, โ€œFor now, you are the chosen.โ€

But she knew better than to trust appearances. The true heir must be sought. Thus, the warrior at i was marked as minIndex โ€” the one assumed to be smallest, until proven otherwise.


๐Ÿ” The Trial of Comparison

        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex])
                minIndex = j;
        }
Enter fullscreen mode Exit fullscreen mode

One by one, the Oracle inspected every warrior beyond the throne. Each was measured against the current choice.

If any warrior smaller, humbler, or more rightful appeared, the Oracleโ€™s finger shifted. The crown would not be given until all had been judged.

Thus, the smallest among all was revealed, even if he had stood at the farthest edge of the line.


๐Ÿ”„ The Exchange of Thrones

        swap(arr[i], arr[minIndex]);
    }
}
Enter fullscreen mode Exit fullscreen mode

At last, the true heir was known. The Oracle lifted him from his place and set him upon the throne i.

If the chosen heir had stood there already, no change was needed. But if another had been mistaken for king, their places were exchanged.

The ritual continued, throne after throne, until all warriors had been seated in their rightful order โ€” the weakest upon the first, the strongest upon the last.


๐ŸŽบ The Trial of Eight

int main() {
    vector<int> arr = {29, 10, 14, 37, 13, 2, 50, 41};
Enter fullscreen mode Exit fullscreen mode

One day, eight claimants stood before the Oracle: 29, 10, 14, 37, 13, 2, 50, 41. They shouted loudly of crowns, yet none stood in the order of truth.


    selectionSort(arr);
Enter fullscreen mode Exit fullscreen mode

The ritual began. One by one, the smallest was found, crowned, and seated. Pride was stripped away, and the kingdomโ€™s order was restored through the ritual of selection.


๐Ÿ‘ The Heraldโ€™s Call

    for (int x : arr) {
        cout << x << " ";
    }
    cout << endl;
}
Enter fullscreen mode Exit fullscreen mode

At last, the Herald called their names aloud. Each warrior stepped forth in ascending order, weakest to strongest. The people cheered, for the true heirs had taken their places, and the kingdom knew peace again.


๐Ÿง  Memory of the Crown

  • The outer loop โ€” the thrones waiting, one by one.
  • The minIndex โ€” the assumed heir, tested against others.
  • The inner loop โ€” the trials of comparison, seeking the true smallest.
  • The swap โ€” the crowning of the rightful heir.

Thus, Selection Sort is remembered as the Rite of the True Heir, where order was restored not by battle, but by careful choice. ๐Ÿ‘‘โš”๏ธ

Top comments (0)