0% found this document useful (0 votes)
9 views5 pages

Selection Sort

The document provides an explanation of the Selection Sort algorithm, which sorts an array by repeatedly selecting the minimum element from the unsorted portion and swapping it with the first unsorted element. It includes a C++ implementation of the algorithm, detailing the code structure and the step-by-step process of sorting an example array. The final output of the sorting process is a sorted array.

Uploaded by

remonhasan.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Selection Sort

The document provides an explanation of the Selection Sort algorithm, which sorts an array by repeatedly selecting the minimum element from the unsorted portion and swapping it with the first unsorted element. It includes a C++ implementation of the algorithm, detailing the code structure and the step-by-step process of sorting an example array. The final output of the sorting process is a sorted array.

Uploaded by

remonhasan.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

🔖 Sorting

Selection Sort
array sorting welldev interview

C++

/*
author: remonhasan
Topic: Selection Sort
*/
#include <bits/stdc++.h>
using namespace std;

void selectionSort(int a[], int N) {


for (int i = 0; i < N - 1; i++) {
int midIndex = i;

for (int j = i + 1; j < N; j++) {


// a[j] is smaller than midIndex than it is the minimum, update j as
midIndex
if (a[j] < a[midIndex]) {
midIndex = j;
}
}

// If midIndex is not i then swap


if (midIndex != i) {
swap(a[i], a[midIndex]);
}
}

int main() {
int N;
cin >> N;
int a[N];

for (int i = 0; i < N; i++) cin >> a[i]; // a[i] = 5, 4, 10, 1, 2

selectionSort(a, N);

for (int i = 0; i < N; i++) cout << a[i] << " ";

return 0;

}
Concepts of Selection Sort:
Selection Sort is a simple and intuitive comparison-based sorting algorithm. The concept behind it
is to repeatedly select the minimum (or maximum) element from an unsorted portion of the array
and swap it with the element at the beginning (or end) of the unsorted part.
Here’s the basic idea:
1. Start with the first element:
• Assume that the first element is the minimum in the array.

2. Search for the smallest (or largest) element in the remaining unsorted portion of the array.
3. Swap the smallest element found with the first element.
4. Move to the next element: Now, consider the first element sorted, and repeat the process for
the remaining unsorted portion of the array.
5. Repeat the above steps for each element until the entire array is sorted.

How Selection Sort Works:


Given an array: 3, 1, 4, 1, 5
• First pass: The smallest element is 1 , which is swapped with the first element 3 . Now, the
array looks like: [1, 3, 4, 1, 5] .
• Second pass: In the subarray [3, 4, 1, 5] , the smallest element is 1 , which is swapped with
3 . Now, the array looks like: [1, 1, 4, 3, 5] .

• Third pass: In the subarray [4, 3, 5] , the smallest element is 3 , which is swapped with 4 .
Now, the array looks like: [1, 1, 3, 4, 5] .
• Fourth pass: In the subarray [4, 5] , the smallest element is 4 , which is already in place. The
array remains: [1, 1, 3, 4, 5] .
After these steps, the array is sorted: [1, 1, 3, 4, 5] .

Explanation
Let's break down this portion of code step by step, explaining each part with reference to your
array 3, 1, 4, 1, 5 :

Code Breakdown:
C++

for (int i = 0; i < n - 1; i++) {


// Assume the current element is the minimum
int minIndex = i;

// Find the smallest element in the remaining unsorted array


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

// Swap the found minimum element with the first element


if (minIndex != i) {
swap(arr[i], arr[minIndex]);
}
}

Let's explain each part of this code, step by step, using your array 3,
1, 4, 1, 5 .

1. Outer Loop: for (int i = 0; i < n - 1; i++)


This outer loop iterates through the array. The variable i represents the current position where
we're looking for the smallest element to swap into place.
• First pass ( i = 0 ): We're looking at the first element (3).
• Second pass ( i = 1 ): We're looking at the second element (1).
• Third pass ( i = 2 ): We're looking at the third element (4).
• Fourth pass ( i = 3 ): We're looking at the fourth element (1).
• Fifth pass ( i = 4 ): We're looking at the last element (5), but no further comparisons are
needed since it's the last element.

2. Initializing minIndex : int minIndex = i


At the start of each outer loop iteration, we assume the current element ( arr[i] ) is the smallest.
So we set minIndex = i , marking the position of the smallest element (for now, it's just the
element at i ).
For example:
• First pass ( i = 0 ): We assume arr[0] = 3 is the smallest element.
• Second pass ( i = 1 ): We assume arr[1] = 1 is the smallest element, and so on.

3. Inner Loop: for (int j = i + 1; j < n; j++)


This inner loop compares the current element ( arr[i] ) with the remaining elements in the array
(those that come after arr[i] ).
• j = i + 1 : We start from the next element after i because we're only interested in comparing
the remaining unsorted elements.
• Loop runs until j < n : This ensures the loop continues through the entire remaining unsorted
portion of the array.

4. Comparing and Updating minIndex : if (arr[j] < arr[minIndex])


Inside the inner loop, we compare each element ( arr[j] ) with the current smallest element
( arr[minIndex] ). If we find a smaller element, we update minIndex to point to the new smallest
element.
For example, in the first pass:
• Initial minIndex = 0 , meaning arr[0] = 3 is the smallest.
• Comparing arr[1] = 1 with arr[0] = 3 , since 1 is smaller, we update minIndex to 1 .
• Comparing arr[2] = 4 with arr[1] = 1 , 4 is not smaller, so minIndex remains 1 .
• Comparing arr[3] = 1 with arr[1] = 1 , 1 is not smaller, so minIndex remains 1 .
• Comparing arr[4] = 5 with arr[1] = 1 , 5 is not smaller, so minIndex remains 1 .
At the end of the first pass, minIndex = 1 , meaning the smallest element in the unsorted part is 1 .

5. Swap: swap(arr[i], arr[minIndex])


Once the inner loop finishes, we have found the smallest element in the unsorted part of the array.
If the smallest element isn't already at index i , we swap the elements at i and minIndex to place
the smallest element in its correct position.
For example, after the first pass:
• Before swap: arr[0] = 3 and arr[minIndex] = 1 .
• Swap them: arr[0] becomes 1 , and arr[1] becomes 3 .
• Array after swap: 1, 3, 4, 1, 5

Full Step-by-Step Execution with Your Array:


Let’s go through this step by step using the array 3, 1, 4, 1, 5 .

First Pass ( i = 0 ):
• minIndex = 0 , assuming arr[0] = 3 is the smallest.
• Compare arr[1] = 1 with arr[0] = 3 : Update minIndex = 1 .
• Compare arr[2] = 4 with arr[1] = 1 : No change.
• Compare arr[3] = 1 with arr[1] = 1 : No change.
• Compare arr[4] = 5 with arr[1] = 1 : No change.
• After inner loop: minIndex = 1 .
• Swap arr[0] and arr[1] : The array becomes [1, 3, 4, 1, 5] .

Second Pass ( i = 1 ):
• minIndex = 1 , assuming arr[1] = 3 is the smallest.
• Compare arr[2] = 4 with arr[1] = 3 : No change.
• Compare arr[3] = 1 with arr[1] = 3 : Update minIndex = 3 .
• Compare arr[4] = 5 with arr[3] = 1 : No change.
• After inner loop: minIndex = 3 .
• Swap arr[1] and arr[3] : The array becomes [1, 1, 4, 3, 5] .

Third Pass ( i = 2 ):
• minIndex = 2 , assuming arr[2] = 4 is the smallest.
• Compare arr[3] = 3 with arr[2] = 4 : Update minIndex = 3 .
• Compare arr[4] = 5 with arr[3] = 3 : No change.
• After inner loop: minIndex = 3 .
• Swap arr[2] and arr[3] : The array becomes [1, 1, 3, 4, 5] .

Fourth Pass ( i = 3 ):
• minIndex = 3 , assuming arr[3] = 4 is the smallest.
• Compare arr[4] = 5 with arr[3] = 4 : No change.
• After inner loop: minIndex = 3 .
• No swap needed as minIndex = i .

Fifth Pass ( i = 4 ):
• The last element is already in place, so no comparisons or swaps are needed.

Final Sorted Array:


Text
1, 1, 3, 4, 5

You might also like