-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
62 lines (53 loc) · 1.04 KB
/
Copy pathSelectionSort.java
File metadata and controls
62 lines (53 loc) · 1.04 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
package sortingAnalysis;
public class SelectionSort
{
private static long comparisons = 0;
private static long movements= 0;
// Constructors
public SelectionSort()
{
}
// Getters
public static long getComparisons()
{
return comparisons;
}
public static long getMovements()
{
return movements;
}
// Method for sorting the numbers
public static void selectionSort(int[] list)
{
comparisons = 0;
movements = 0;
for (int i = 0; i < list.length - 1; i++)
{
// Find minimum in list[i..linst.length-1]
int currentMin = list[i];
int currentMinIndex = i;
movements++;
movements++;
for (int j = i + 1; j < list.length; j++)
{
if (currentMin > list[j])
{
currentMin = list[j];
currentMinIndex = j;
movements++;
movements++;
}
comparisons++;
}
// Swap list[i] with list[currentMinIndex] if necessary
if (currentMinIndex != i)
{
list[currentMinIndex] = list[i];
list[i] = currentMin;
movements++;
movements++;
}
comparisons++;
}
}
}