-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeletionSort.cpp
More file actions
55 lines (44 loc) · 892 Bytes
/
SeletionSort.cpp
File metadata and controls
55 lines (44 loc) · 892 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
50
51
52
53
54
55
//Seletion Sort
//in iteration i, find(select) index min of smallest remaining entry.
//time: O(n^2) compares, O(n) exchanges
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define SORT_NUM 50
void swap(int *a,int m,int n)
{
int tmp = a[m];
a[m] = a[n];
a[n] = tmp;
}
void SeletionSort(int *a,int num)
{
int min = 0;
for(int i=0;i<num;i++)
{
min = i;
for(int j=i+1;j<num;j++)
{
if(a[j]<a[min])
min = j;
}
swap(a,i,min);
}
}
void display(int *a,int num)
{
for(int i=0;i<num;i++)
cout << "sorted array [" << i << "] = " << a[i] << endl;
}
int main()
{
int num = SORT_NUM;
int a[num]= {0};
srand((int)time(0));
for(int i=0;i<num;i++)
a[i] = rand()%100;
SeletionSort(a,num);
display(a,num);
return 0;
}