-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionsort.cpp
More file actions
49 lines (46 loc) · 802 Bytes
/
selectionsort.cpp
File metadata and controls
49 lines (46 loc) · 802 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
47
48
49
#include <iostream>
#include <algorithm>
using namespace std;
int findMaxPos(int arr[],int n)
{
int i;
int pos = 0;
int max = arr[0];
for(i = 1;i<n;i++)
{
if(arr[i]>max)
{
max = arr[i];
pos = i;
}
}
return pos;
}
void selectionSort(int arr[],int n)
{
int temp;
for(int i = n;i>1;i--)
{
int pos = findMaxPos(arr,i);
temp = arr[pos];
arr[pos] = arr[i-1];
arr[i-1] = temp;
}
}
//选择排序
void test2()
{
int arr[] = {9,6,2,3,5,1,3};
algorithm_me al;
al.selectionSort(arr,7);
for(int i = 0;i<7;i++)
{
cout <<"选择排序"<< arr[i] << endl;
}
}
int main(int argc, char *argv[])
{
test2();
cout << "Hello World!" << endl;
return 0;
}