-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.cpp
More file actions
72 lines (55 loc) · 1.37 KB
/
SelectionSort.cpp
File metadata and controls
72 lines (55 loc) · 1.37 KB
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
71
72
#include "Header.h"
double Sorting::selectionSortTime(int arr[], int n)
{
int * hold = arr;
clock_t start, end;
start = clock();
int i, j, min_idx, temp;
// One by one move boundary of unsorted subarray
for (i = 0; i < n - 1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i + 1; j < n; j++)
if (hold[j] < hold[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
temp = hold[min_idx];
hold[min_idx] = hold[i];
hold[i] = temp;
}
end = clock();
double msecs = ((double)(end - start)) * 1000 / CLOCKS_PER_SEC;
return msecs;
}
int Sorting::selectionSortSteps(int arr[], int n)
{
int * hold = arr;
int i, j, min_idx, temp,numofsteps=0;
numofsteps++;
// One by one move boundary of unsorted subarray
for (i = 0; i < n - 1; i++)
{
numofsteps += 3; //i < n-1, i++, min_idx = i
// Find the minimum element in unsorted array
min_idx = i;
for (j = i + 1; j < n; j++)
{
numofsteps += 2; //j < n, j++
if (hold[j] < hold[min_idx])
{
min_idx = j;
numofsteps += 2;//min_idx = j, hold[j] < hold[min_idx]
}
numofsteps++;//hold[j] > hold[min_idx]
}
numofsteps++;
// Swap the found minimum element with the first element
temp = hold[min_idx];
hold[min_idx] = hold[i];
hold[i] = temp;
numofsteps += 3; // line 62-64
}
numofsteps++; // i > n-1
return numofsteps;
}