Selection Sort
void selectionSort(vector<int>& arr, int n){ // Outer loop to iterate through each element except the last one for(int i = 0; i < n-1; i++ ) { int minIndex = i; // Assume the first unsorted element is the smallest
// Inner loop to find the minimum element in the unsorted portion for(int j = i+1; j < n; j++) { if(arr[j] < arr[minIndex]) // If a smaller element is found, update minIndex minIndex = j; } // Swap the smallest found element with the first unsorted element swap(arr[minIndex], arr[i]); }}Example:
vector<int> arr = {64, 25, 12, 22, 11};int n = 5;Step-by-Step Execution:
-
Initial Array:
arr[] = {64, 25, 12, 22, 11}. -
Outer Loop (i = 0):
- Assume
minIndex = 0(value is 64). - Inner Loop:
- Compare
arr[1] (25)witharr[0] (64)→minIndex = 1. - Compare
arr[2] (12)witharr[1] (25)→minIndex = 2. - Compare
arr[3] (22)witharr[2] (12)→minIndex = 2. - Compare
arr[4] (11)witharr[2] (12)→minIndex = 4.
- Compare
- Swap
arr[0]witharr[4]:arr[] = {11, 25, 12, 22, 64}.
- Assume
-
Outer Loop (i = 1):
- Assume
minIndex = 1(value is 25). - Inner Loop:
- Compare
arr[2] (12)witharr[1] (25)→minIndex = 2. - Compare
arr[3] (22)witharr[2] (12)→minIndex = 2. - Compare
arr[4] (64)witharr[2] (12)→minIndex = 2.
- Compare
- Swap
arr[1]witharr[2]:arr[] = {11, 12, 25, 22, 64}.
- Assume
-
Outer Loop (i = 2):
- Assume
minIndex = 2(value is 25). - Inner Loop:
- Compare
arr[3] (22)witharr[2] (25)→minIndex = 3. - Compare
arr[4] (64)witharr[3] (22)→minIndex = 3.
- Compare
- Swap
arr[2]witharr[3]:arr[] = {11, 12, 22, 25, 64}.
- Assume
-
Outer Loop (i = 3):
- Assume
minIndex = 3(value is 25). - Inner Loop:
- Compare
arr[4] (64)witharr[3] (25)→minIndex = 3.
- Compare
- No swap needed as
minIndex = 3. - Final array:
arr[] = {11, 12, 22, 25, 64}.
- Assume
Final Sorted Array:
- After running the selection sort, the array is sorted:
arr[] = {11, 12, 22, 25, 64}