-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathSelection Sort.txt
70 lines (51 loc) · 1.16 KB
/
Selection Sort.txt
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include<algorithm>
using namespace std;
void selectionSort(int arr[], int n)
{
int minIdx;
//0 1 2 3 4 5
for (int i = 0; i < n - 1; i++)//60 40 50 30 10 20
{
minIdx = i;//4
for (int j = i + 1; j < n; j++)
if (arr[j] > arr[minIdx])
minIdx = j;
swap(arr[minIdx], arr[i]);
}
}
/*
//\\//\\//\\//\\//\\Recursive//\\//\\//\\//\\//\\//\\
int minIndex(int a[], int i, int j)
{
if (i == j)
return i;
int k = minIndex(a, i + 1, j);
return (a[i] < a[k]) ? i : k;
}
void recurSelectionSort(int a[], int n, int index = 0)
{
if (index == n)
return;
int k = minIndex(a, index, n - 1);
if (k != index)
swap(a[k], a[index]);
recurSelectionSort(a, n, index + 1);
}
*/
void print(int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = { -60, 0, 50, 30, 10,20 };
int n = sizeof(arr) / sizeof(arr[0]);//6*4=24 / 4
selectionSort(arr, n);
//recurSelectionSort(arr, n);
cout<<"Array After Selection Sort : \n";
print(arr, n);
return 0;
}