Skip to content

Commit 8781ccc

Browse files
authored
Merge pull request #3572 from Ishitamukherjee2004/other
Selection Sort Algorithm from gfg is added
2 parents a972a11 + 1f90422 commit 8781ccc

File tree

1 file changed

+267
-0
lines changed

1 file changed

+267
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
---
2+
id: selection-sort
3+
title: Selection Sort
4+
sidebar_label: Selection-Sort
5+
tags:
6+
- Sorting
7+
- Algorithms
8+
description: "This tutorial covers the solution to the SelectionSort problem from the GeeksforGeeks."
9+
---
10+
## Problem Description
11+
12+
Given an unsorted array of size `N`, use selection sort to sort `arr[]` in increasing order.
13+
14+
## Examples
15+
16+
**Example 1:**
17+
18+
```
19+
Input:
20+
N = 5
21+
arr[] = {4, 1, 3, 9, 7}
22+
Output:
23+
1 3 4 7 9
24+
Explanation:
25+
Maintain sorted (in bold) and unsorted subarrays.
26+
Select 1. Array becomes 1 4 3 9 7.
27+
Select 3. Array becomes 1 3 4 9 7.
28+
Select 4. Array becomes 1 3 4 9 7.
29+
Select 7. Array becomes 1 3 4 7 9.
30+
Select 9. Array becomes 1 3 4 7 9.
31+
```
32+
33+
**Example 2:**
34+
35+
```
36+
Input:
37+
N = 10
38+
arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
39+
Output:
40+
1 2 3 4 5 6 7 8 9 10
41+
```
42+
43+
## Your Task
44+
You dont need to read input or print anything. Complete the functions select() and selectionSort() ,where select() takes arr[] and starting point of unsorted array i as input parameters and returns the selected element for each iteration in selection sort, and selectionSort() takes the array and it's size and sorts the array.
45+
46+
Expected Time Complexity: O(N^2)
47+
48+
Expected Auxiliary Space: O(1)
49+
50+
## Constraints
51+
52+
* `1 ≤ N ≤ 10^3`
53+
54+
## Problem Explanation
55+
56+
The task is to traverse the array and sort the array using selection sort.
57+
58+
## Code Implementation
59+
60+
### C++ Solution
61+
62+
```cpp
63+
64+
// C++ program for implementation of
65+
// selection sort
66+
#include <bits/stdc++.h>
67+
using namespace std;
68+
69+
// Function for Selection sort
70+
void selectionSort(int arr[], int n)
71+
{
72+
int i, j, min_idx;
73+
74+
// One by one move boundary of
75+
// unsorted subarray
76+
for (i = 0; i < n - 1; i++) {
77+
78+
// Find the minimum element in
79+
// unsorted array
80+
min_idx = i;
81+
for (j = i + 1; j < n; j++) {
82+
if (arr[j] < arr[min_idx])
83+
min_idx = j;
84+
}
85+
86+
// Swap the found minimum element
87+
// with the first element
88+
if (min_idx != i)
89+
swap(arr[min_idx], arr[i]);
90+
}
91+
}
92+
93+
// Function to print an array
94+
void printArray(int arr[], int size)
95+
{
96+
int i;
97+
for (i = 0; i < size; i++) {
98+
cout << arr[i] << " ";
99+
cout << endl;
100+
}
101+
}
102+
103+
// Driver program
104+
int main()
105+
{
106+
int arr[] = { 64, 25, 12, 22, 11 };
107+
int n = sizeof(arr) / sizeof(arr[0]);
108+
109+
// Function Call
110+
selectionSort(arr, n);
111+
cout << "Sorted array: \n";
112+
printArray(arr, n);
113+
return 0;
114+
}
115+
116+
```
117+
118+
```java
119+
// Java program for implementation of Selection Sort
120+
import java.io.*;
121+
public class SelectionSort
122+
{
123+
void sort(int arr[])
124+
{
125+
int n = arr.length;
126+
127+
// One by one move boundary of unsorted subarray
128+
for (int i = 0; i < n-1; i++)
129+
{
130+
// Find the minimum element in unsorted array
131+
int min_idx = i;
132+
for (int j = i+1; j < n; j++)
133+
if (arr[j] < arr[min_idx])
134+
min_idx = j;
135+
136+
// Swap the found minimum element with the first
137+
// element
138+
int temp = arr[min_idx];
139+
arr[min_idx] = arr[i];
140+
arr[i] = temp;
141+
}
142+
}
143+
144+
// Prints the array
145+
void printArray(int arr[])
146+
{
147+
int n = arr.length;
148+
for (int i=0; i<n; ++i)
149+
System.out.print(arr[i]+" ");
150+
System.out.println();
151+
}
152+
153+
// Driver code to test above
154+
public static void main(String args[])
155+
{
156+
SelectionSort ob = new SelectionSort();
157+
int arr[] = {64,25,12,22,11};
158+
ob.sort(arr);
159+
System.out.println("Sorted array");
160+
ob.printArray(arr);
161+
}
162+
}
163+
164+
```
165+
166+
```python
167+
# Python program for implementation of Selection
168+
# Sort
169+
A = [64, 25, 12, 22, 11]
170+
171+
# Traverse through all array elements
172+
for i in range(len(A)-1):
173+
174+
# Find the minimum element in remaining
175+
# unsorted array
176+
min_idx = i
177+
for j in range(i+1, len(A)):
178+
if A[min_idx] > A[j]:
179+
min_idx = j
180+
181+
# Swap the found minimum element with
182+
# the first element
183+
A[i], A[min_idx] = A[min_idx], A[i]
184+
185+
# Driver code to test above
186+
print ("Sorted array")
187+
for i in range(len(A)):
188+
print(A[i],end=" ")
189+
190+
```
191+
192+
```javascript
193+
<script>
194+
// Javascript program for implementation of selection sort
195+
function swap(arr,xp, yp)
196+
{
197+
var temp = arr[xp];
198+
arr[xp] = arr[yp];
199+
arr[yp] = temp;
200+
}
201+
202+
function selectionSort(arr, n)
203+
{
204+
var i, j, min_idx;
205+
206+
// One by one move boundary of unsorted subarray
207+
for (i = 0; i < n-1; i++)
208+
{
209+
// Find the minimum element in unsorted array
210+
min_idx = i;
211+
for (j = i + 1; j < n; j++)
212+
if (arr[j] < arr[min_idx])
213+
min_idx = j;
214+
215+
// Swap the found minimum element with the first element
216+
swap(arr,min_idx, i);
217+
}
218+
}
219+
220+
function printArray( arr, size)
221+
{
222+
var i;
223+
for (i = 0; i < size; i++)
224+
document.write(arr[i] + " ");
225+
document.write(" <br>");
226+
}
227+
228+
var arr = [64, 25, 12, 22, 11];
229+
var n = 5;
230+
selectionSort(arr, n);
231+
document.write("Sorted array: <br>");
232+
printArray(arr, n);
233+
234+
// This code is contributed by akshitsaxenaa09.
235+
</script>
236+
237+
```
238+
239+
## Solution Logic:
240+
241+
1. For the first position in the sorted array, the whole array is traversed from index 0 to 4 sequentially. The first position where 64 is stored presently, after traversing whole array it is clear that 11 is the lowest value.
242+
243+
2. Thus, replace 64 with 11. After one iteration 11, which happens to be the least value in the array, tends to appear in the first position of the sorted list.
244+
245+
3. For the second position, where 25 is present, again traverse the rest of the array in a sequential manner.
246+
247+
4. After traversing, we found that 12 is the second lowest value in the array and it should appear at the second place in the array, thus swap these values.
248+
249+
5. Now, for third place, where 25 is present again traverse the rest of the array and find the third least value present in the array.
250+
251+
6. While traversing, 22 came out to be the third least value and it should appear at the third place in the array, thus swap 22 with element present at third position.
252+
253+
7. Similarly, for fourth position traverse the rest of the array and find the fourth least element in the array
254+
255+
8. As 25 is the 4th lowest value hence, it will place at the fourth position.
256+
257+
9. At last the largest value present in the array automatically get placed at the last position in the array
258+
259+
10. The resulted array is the sorted array.
260+
261+
## Time Complexity
262+
263+
* The time complexity is $O(N^2)$ as there are two nested loops
264+
265+
## Space Complexity
266+
267+
* The auxiliary space complexity is $O(1)$ due to the only extra memory used is for temporary variables while swapping two values in Array. The selection sort never makes more than $O(N)$ swaps and can be useful when memory writing is costly.

0 commit comments

Comments
 (0)