๐ 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;
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) {
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();
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;
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;
}
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]);
}
}
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};
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);
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;
}
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)