-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathSelectionSort.cpp
53 lines (43 loc) · 1.09 KB
/
SelectionSort.cpp
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
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;
void Selectionsort(int arr[], int n){
cout<< "Running selection sort "<<endl;
int IndexOfMin , temp ;
for (int i = 0; i < n-1; i++)
{
IndexOfMin=i;
for (int j = i+1; j < n; j++)
{
if (arr[j]<arr[IndexOfMin])
{
IndexOfMin = j;
}
}
// swap the element
temp = arr[i];
arr[i]= arr[IndexOfMin];
arr[IndexOfMin]=temp;
}
}
int main()
{
int n;
cout << "Enter the no of array you want:- ";
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cout << "Enter the element of array :- ";
cin >> arr[i];
}
for (int i = 0; i < n; i++)
{
cout << "your array is :- " << arr[i]<<endl; // printing the array before sorting
}
Selectionsort(arr,n);
for (int i = 0; i < n; i++)
{
cout << "your selection sort array is :- " << arr[i]<< endl; // printing the array before sorting
}
return 0;
}