-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathselectionsort.cpp
More file actions
46 lines (45 loc) · 800 Bytes
/
selectionsort.cpp
File metadata and controls
46 lines (45 loc) · 800 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
45
46
#include <iostream>
using namespace std;
void SelectionSort(int*,int);
int main()
{
int n,i;
cout << "Enter the number of data element to be sorted: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array" << endl;
for(i = 0; i < n; i++)
{
cin>>arr[i];
}
/*
//selection sort
*/
SelectionSort(arr,n);
// Display the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}
void SelectionSort(int array[],int s)
{
int max; //maximum index
for(int i=s-1;i>0;i--)
{
max=0;
//finding max element index
for(int j=0;j<=i;j++)
{
if(array[max]<array[j])
{
max = j;
}
}
//swapp
int temp = array[i];
array[i] = array[max];
array[max] = temp;
}
}