-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingAlgorithms.java
More file actions
156 lines (146 loc) · 4.55 KB
/
SortingAlgorithms.java
File metadata and controls
156 lines (146 loc) · 4.55 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.util.ArrayList;
public class SortLevel {
public static boolean BubbleSortStep(int[] array){
boolean swapped = true;
int temp;
for (int i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1]) {
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swapped = false;
}
}
return swapped;
}
public static void ShellSort(int[] array){
ArrayList<Integer> list = KnuthSequence(array.length);
for (int i = 0; i < list.size(); i++){
for (int k = 0; k < list.get(i); k++){
InsertionSortStep(array, list.get(i),k);
}
}
}
public static void SelectionSortStep(int[] array, int i){
if (i+1 < array.length){
int index = i+1;
int min = array[index];
for (int j = i+1; j < array.length; j++){
if (min > array[j]){
min = array[j];
index = j;
}
}
min = array[i];
array[i] = array[index];
array[index] = min;
}
}
public static void InsertionSortStep(int[] array, int step, int i) {
int temp;
int index = i;
for (int j = i; j < array.length; j += step) {
if (array[index] > array[j]) {
if (array[j] < array[i]) {
temp = array[j];
for (int k = j; k >= i; k -= step) {
if (k - step >= i) {
array[k] = array[k - step];
}
}
array[i] = temp;
} else {
for (int k = j; k > i; k -= step){
if (((k - step) > i)&&(array[k] < array[k - step])){
temp = array[k-step];
array[k-step] = array[k];
array[k] = temp;
}
}
}
}
index = j;
}
}
public static void sort(int[] arr){
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
public static ArrayList<Integer> KnuthSequence(int array_size) {
ArrayList<Integer> list = new ArrayList<>();
int i = 0;
while ((i = 3 * i + 1) <= array_size) {
list.add(0, i);
}
return list;
}
public static void print(int[] array){
for (int i : array) {
System.out.print(i + " ");
}
System.out.println();
}
public static int sequence(int iterator){
if (iterator == 1){
return 1;
}else {
return 3*sequence(iterator-1)+1;
}
}
public static int ArrayChunk(int[] array){
int i = 0;
int j = array.length-1;
int index = array.length/2;
int frame = array[index];
int temp;
boolean goTo = false;
while (true){
if (goTo){
i = 0;
j = array.length-1;
index = array.length/2;
frame = array[index];
goTo = false;
}
while (array[i] < frame){
i++;
}
while (array[j] > frame){
j--;
}
if ((i == (j - 1)) && (array[i] > array[j])){
temp = array[i];
array[i] = array[j];
array[j] = temp;
if (array[i] == frame){
index = i;
}else if (array[j] == frame){
index = j;
}
goTo = true;
} else if ((i == j) || ((array[i] < array[j]) && i == j - 1)){
return index;
}else {
temp = array[i];
array[i] = array[j];
array[j] = temp;
if (array[i] == frame){
index = i;
}else if (array[j] == frame){
index = j;
}
}
}
}
}