-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgo.java
More file actions
120 lines (111 loc) · 4.02 KB
/
Algo.java
File metadata and controls
120 lines (111 loc) · 4.02 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
package sortingalgojava;
public abstract class Algo extends drawUI{
static int smallIndex, temp, minIndex, index;
public static void insertion(int [] list, int listLength){
long start = System.currentTimeMillis();
for (minIndex=1; minIndex<listLength; minIndex++)
{
temp = list[minIndex];
index = minIndex;
while ((index>0) && (list[index-1]>temp))
{
list[index] = list[index-1];
index--;
}
list[index] = temp;
for(loop = 0;loop<listLength;loop++){
txtarea.append(list[loop]+" ");
mainpanel.updateUI();
}
txtarea.append("\n");
pass++;
if(handler.isSorted(list) == true)
break;
}
passNum.setText(""+pass);
long end = System.currentTimeMillis();
long time = end-start;
execTime.setText(time+" milliseconds");
}
public static void bubble(int list[], int listLength)
{
long start = System.currentTimeMillis();
for(index=0; index<listLength;index++){
for(minIndex=1; minIndex<listLength-index; minIndex++){
if(list[minIndex-1]>list[minIndex])
{
temp = list[minIndex-1];
list[minIndex-1] = list[minIndex];
list[minIndex] = temp;
}
for(loop = 0;loop<listLength;loop++){
txtarea.append(list[loop]+" ");
mainpanel.updateUI();
}
txtarea.append("\n");
}
txtarea.append("\n");
pass++;
if(handler.isSorted(list) == true)
break;
}
passNum.setText(""+pass);
long end = System.currentTimeMillis();
long time = end-start;
execTime.setText(time+" milliseconds");
}
public static int[] Merge (int list[], int listLength){
long start = System.currentTimeMillis();
if (listLength > 1){
int array1Length = listLength /2;
int array2Length = listLength - array1Length;
int array1[] = new int[array1Length];
int array2 [] = new int[array2Length];
for(index = 0; index< array1Length; index++)
array1[index] = list[index];
for(index = array1Length; index< array1Length+array2Length; index++)
array2[index-array1Length] = list[index];
array1 = Merge(array1 , array1.length);
array2 = Merge(array2 , array2.length);
int i=0, j=0, k=0;
while(array1.length != j && array2.length != k){
if(array1[j] <= array2[k]){
list[i] = array1[j];
i++; j++;
} else {
list[i] = array2[k];
i++; k++;
}
}
while(array1.length != j){
list[i] = array1[j];
i++; j++;
}
for(loop = 0;loop<array1Length;loop++){
txtarea.append(array1[loop]+" ");
mainpanel.updateUI();
}
txtarea.append("|");
while(array2.length != k){
list[i] = array2[k];
i++; k++;
}
for(loop = 0;loop<array2Length;loop++){
txtarea.append(array2[loop]+" ");
mainpanel.updateUI();
}
txtarea.append("\n");
for(loop = 0;loop<listLength;loop++){
txtarea.append("->"+list[loop]+" ");
mainpanel.updateUI();
}
txtarea.append("\n \n");
pass++;
}
passNum.setText(""+pass);
long end = System.currentTimeMillis();
long time = end-start;
execTime.setText(time+" milliseconds");
return list;
}
}