forked from Adi8080/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectionsort.cpp
43 lines (40 loc) · 845 Bytes
/
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
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;
void selectionsort(vector<int> v)
{
vector<int>::iterator i, j, pos;
int small;
for (i = v.begin(); i != v.end() - 1; i++)
{
small = *i;
for (j = i + 1; j != v.end(); j++)
{
if (*j < small)
{
small = *j;
pos = j;
}
}
swap(*i, *pos);
}
cout << "\nSorted vector using selection sort:\n";
for (i = v.begin(); i != v.end(); i++)
cout << *i << " ";
}
int main()
{
int n = 10;
vector<int> v;
srand(time(0));
cout << "\nUnsorted vector:\n";
for (int i = 0; i < n; i++)
{
v.push_back(1 + rand() % 100);
cout << v[i] << " ";
}
selectionsort(v);
return 0;
}