-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathSelectionsort.cpp
More file actions
44 lines (34 loc) · 779 Bytes
/
Selectionsort.cpp
File metadata and controls
44 lines (34 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
void disp(int arr[], int len)
{
for (int i = 0; i < len; i++)
cout << arr[i] << " ";
}
void sort(int arr[], int len) // function for selection sort
{
int temp,count=0;
for (int i = 0; i < len - 1; i++)
{
for (int j = i + 1; j < len; j++)
{
if (arr[j] < arr[i])
{
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
}
int main()
{
cout<<"Initally the elements are: ";
int arr[] = {3,7,1,5,2};
int len = 5;
disp(arr, len);
cout << endl
<< "Array after sorting: ";
sort(arr, len);
disp(arr, len);
}