Skip to content

Commit c03b668

Browse files
shubhendra-20MadhavBahl
authored andcommitted
Selection Sort (CodeToExpress#232)
1 parent 6e71f8c commit c03b668

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

day32/CPP/day32_solved.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Author: Shubhendra Singh
3+
Github: shubhendra-20
4+
*/
5+
6+
#include <iostream>
7+
using namespace std;
8+
void swap(int *xp, int *yp)
9+
{
10+
int temp = *xp;
11+
*xp = *yp;
12+
*yp = temp;
13+
}
14+
void selectionSort(int arr[], int n)
15+
{
16+
int i, j, min_idx;
17+
18+
for (i = 0; i < n-1; i++)
19+
{
20+
min_idx = i;
21+
for (j = i+1; j < n; j++)
22+
if (arr[j] < arr[min_idx])
23+
min_idx = j;
24+
25+
swap(&arr[min_idx], &arr[i]);
26+
}
27+
}
28+
void printArray(int arr[], int size)
29+
{
30+
int i;
31+
for (i=0; i < size; i++)
32+
cout<<arr[i]<<" ";
33+
}
34+
int main()
35+
{
36+
int n;
37+
cin>>n;
38+
int a[n];
39+
int i;
40+
for(i=0;i<n;i++)
41+
{
42+
cin>>a[i];
43+
}
44+
selectionSort(a, n);
45+
printArray(a, n);
46+
return 0;
47+
}

0 commit comments

Comments
 (0)