Skip to content

Commit 816bf48

Browse files
authored
Merge pull request #1 from kapilk535/kapilk535-patch-1
Create Selection_Sort.cpp
2 parents 60f95da + 02c986d commit 816bf48

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
void
7+
swapi (int *a, int *b)
8+
{
9+
int t = *a;
10+
*a = *b;
11+
*b = t;
12+
// cout << *a << " " << *b << endl;
13+
}
14+
15+
16+
vector < int >
17+
selectionSort (vector < int >arr, int n)
18+
{
19+
int i, j, min_idx;
20+
21+
// One by one move boundary of unsorted subarray
22+
for (i = 0; i < n - 1; i++)
23+
{
24+
// Find the minimum element in unsorted array
25+
min_idx = i;
26+
for (j = i + 1; j < n; j++)
27+
if (arr[j] < arr[min_idx])
28+
min_idx = j;
29+
30+
// Swap the found minimum element with the first element
31+
swapi (&arr[min_idx], &arr[i]);
32+
}
33+
return arr;
34+
}
35+
36+
37+
38+
int
39+
main ()
40+
{
41+
// cout << "Hello World\n";
42+
std::vector < int >vec;
43+
vec = {202, 4, -5, 66, -2, 0, 96, 5, 78, 12, 48, 88};
44+
45+
vec = selectionSort (vec, vec.size());
46+
for (int x:vec)
47+
{
48+
cout << x << " ";
49+
}
50+
return 0;
51+
}

0 commit comments

Comments
 (0)