Skip to content

Commit fbd13c1

Browse files
committed
Added selection sort algorithm
1 parent 9961df7 commit fbd13c1

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed
File renamed without changes.
File renamed without changes.

Sorting/selection_sort.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# include <iostream>
2+
using namespace std;
3+
4+
int main(int argc, char const *argv[])
5+
{ // length of array
6+
cout<<"Enter the length of array"<<endl;
7+
int n,temp;
8+
cin>>n;
9+
int arr[n];
10+
// Getting elements of array
11+
cout<<"Enter the elements of array"<<endl;
12+
for (int i = 0; i < n; i++)
13+
{
14+
cin>>arr[i];
15+
}
16+
for (int i = 0; i < n; i++)
17+
{
18+
for(int j = i; j<n; j++)
19+
{ // comparing elements of array
20+
if(arr[j]<arr[i])
21+
{
22+
temp = arr[i];
23+
arr[i]=arr[j];
24+
arr[j]=temp;
25+
}
26+
}
27+
}
28+
for(int i=0; i<n; i++)
29+
{
30+
cout<<arr[i]<<" ";
31+
}
32+
return 0;
33+
}

0 commit comments

Comments
 (0)