๐น The Chronicles of the Order-Bearer: The Insertion Sort Saga
"A line is not born perfect.
It becomes perfect when each finds his rightful place among those before him."
โ Song of the Order-Bearer
In the Kingdom of Lines, chaos walked proudly. Warriors stood not by skill, but by chance. A weakling might precede a champion, and a champion might be buried among squires.
The High Sage decreed:
โEach warrior shall step into the line one by one.
He shall not stand where he first lands, but where he belongs among those before him.
Thus, the line shall grow in order, piece by piece, until all stand as one.โ
This ritual became known as the Insertion Sort.
๐ The Scroll of Lineage
#include <iostream>
#include <vector>
using namespace std;
The scribes laid open the scrolls (iostream
) and spread the line of warriors (vector
). Upon these pages the ritual of order would be etched, so the kingdom would remember.
โ๏ธ The Call of the Sage
void insertionSort(vector<int>& arr) {
The Sage raised his staff and called the warriors into the ritual. The spell of insertionSort was named, and the line stood trembling, waiting to be set aright.
int n = arr.size();
The first step was to count them โ all who would stand in the line. n
was not just a number; it was the length of destiny, the measure of the order yet to come.
๐น The Warrior Steps Forward
for (int i = 1; i < n; i++) {
The first warrior at position 0 was left alone โ for a single soul needs no order.
Then, beginning from the second (i = 1
), each warrior stepped forward into the growing line. He could not stand merely where he entered. He had to find his rightful place among those who came before.
โ๏ธ The Choice of the Key
int key = arr[i];
int j = i - 1;
The chosen warrior was lifted from the line โ called the key. He looked back to those who already stood ordered behind him. The one before him (j
) was his rival, his test.
๐ก๏ธ The Trials of Comparison
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
The key warrior faced the champions before him. If a warrior (arr[j]
) was stronger (greater) than him, that warrior stepped one place forward to make room.
The key pressed on, moving backward, testing again and again โ until he found a rival no stronger than himself, or the beginning of the line.
Thus, each trial shifted others forward, like a ripple of respect, until the keyโs rightful place was uncovered.
๐ The Final Placement
arr[j + 1] = key;
}
}
At last, the key settled into the place destiny had prepared for him. The line closed around him, stronger than before. Then the next warrior stepped forward, and the ritual repeated.
By the end, every soul had found his rightful place, and the line stood perfect โ ordered from weakest to strongest.
๐บ The Trial of Eight
int main() {
vector<int> arr = {12, 11, 13, 5, 6, 7, 3, 15};
On one dawn, eight warriors entered the line: 12, 11, 13, 5, 6, 7, 3, 15. They stood in chaos, their strength unmatched to their order. The Sage prepared the ritual, and the people watched.
insertionSort(arr);
The ritual began. One by one, the warriors were lifted, tested, and placed. With each step, the line grew more ordered, until the disorder was driven out entirely.
๐ The Heraldโs Call
for (int x : arr) {
cout << x << " ";
}
cout << endl;
}
At last, the Herald marched down the line, calling each warrior in turn. Their names rang out in ascending order. The crowd cheered, for the line now mirrored the wisdom of the Sage.
๐ง Memory of the Ritual
- The outer loop โ each warrior stepping forward into the line.
- The key โ the chosen warrior, lifted to find his place.
- The while loop โ the backward trials, shifting stronger warriors forward.
- The placement โ the key settling where destiny decreed.
Thus, Insertion Sort is remembered as the Ritual of the Order-Bearer, where chaos gave way to harmony, one warrior at a time. ๐น๐
Top comments (0)