forked from codesession/Data-structure-Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectsort.cpp
More file actions
32 lines (31 loc) · 519 Bytes
/
selectsort.cpp
File metadata and controls
32 lines (31 loc) · 519 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
#include<algorithm>
#include<iostream>
template<class T>
void selectsort(T* arr, int length)
{
if (length <= 1) return;
int minpos;
for (int i = 0; i < length - 1; i++)
{
minpos = i;
for (int j = length - 1; j > i; j--)
{
if (arr[j] < arr[minpos])
{
minpos = j;
}
}
std::swap(arr[i], arr[minpos]);
}
}
int main(int argc, char const *argv[])
{
//1 2 2 3 3 5 5 7 8
int arr[] = {8, 5, 3, 2, 1, 5, 7, 2, 3};
selectsort(arr, 9);
for (auto i : arr)
{
std::cout << i << " ";
}
return 0;
}