-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathSorts.java
More file actions
129 lines (116 loc) · 3.08 KB
/
Sorts.java
File metadata and controls
129 lines (116 loc) · 3.08 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
public class Sorts{
private long steps;
/**
* Description of Constructor
*
* @param list Description of Parameter
*/
public Sorts(){
steps = 0;
}
/**
* Description of the Method
*
* @param list reference to an array of integers to be sorted
*/
public void bubbleSort(int [] list){
//replace these lines with your code
System.out.println();
System.out.println("Bubble Sort");
System.out.println();
steps = 0;
for (int outer = 0; outer < list.length - 1; outer++){
for (int inner = 0; inner < list.length-outer-1; inner++){
steps += 3;//count one compare and 2 gets
if (list[inner]>(list[inner + 1])){
steps += 4;//count 2 gets and 2 sets
int temp = list[inner];
list[inner] = list[inner + 1];
list[inner + 1] = temp;
}
}
}
}
/**
* Description of the Method
*
* @param list reference to an array of integers to be sorted
*/
public void selectionSort(int [] list){
//replace these lines with your code
System.out.println();
System.out.println("Selection Sort");
System.out.println();
}
/**
* Description of the Method
*
* @param list reference to an array of integers to be sorted
*/
public void insertionSort(int [] list){
//replace these lines with your code
System.out.println();
System.out.println("Insertion Sort");
System.out.println();
}
/**
* Description of the Method
*
* @param list reference to an array of integers to be sorted
*/
public void mergeSort(int [] list, int n){
//replace these lines with your code
if (n < 2) {
return;
}
int mid = n / 2;
int[] l = new int[mid];
int[] r = new int[n - mid];
for (int i = 0; i < mid; i++) {
l[i] = list[i];
}
for (int i = mid; i < n; i++) {
r[i - mid] = list[i];
}
mergeSort(l, mid);
mergeSort(r, n - mid);
merge(list, l, r, mid, n - mid);
}
public void merge(int[] a, int[] l, int[] r, int left, int right) {
int i = 0, j = 0, k = 0;
while (i < left && j < right) {
if (l[i] <= r[j])
{
a[k++] = l[i++];
}
else
{
a[k++] = r[j++];
}
}
while (i < left)
{
a[k++] = l[i++];
}
while (j < right)
{
a[k++] = r[j++];
}
}
/**
* Accessor method to return the current value of steps
*
*/
public long getStepCount(){
return steps;
}
/**
* Modifier method to set or reset the step count. Usually called
* prior to invocation of a sort method.
*
* @param stepCount value assigned to steps
*/
public void setStepCount(long stepCount){
steps = stepCount;
}
}