-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathHeapSort1.java
More file actions
194 lines (181 loc) · 5.84 KB
/
HeapSort1.java
File metadata and controls
194 lines (181 loc) · 5.84 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import java.util.*;
import java.lang.*;
class HeapSort1{
ArrayList<Integer> contents;
int currentIndex = 0;
public HeapSort1(){
contents=new ArrayList<Integer>();
}
public void insert(int value){
if(currentIndex>0){
contents.add(currentIndex,
new Integer(value));
int tempIndex=currentIndex;
while(contents.get(tempIndex)>contents.get(tempIndex/2)){
int a = contents.get(tempIndex);
int b = contents.get((int)(tempIndex)/2);
contents.set(tempIndex, b);
contents.set((int)tempIndex/2, a);
tempIndex=(int)tempIndex/2;
}
currentIndex++;
}else{
contents.add(currentIndex++,
new Integer(value));
}
}
public void traverseByLevel(){
for(int i=0;i<contents.size();i++){
int level=(int)(Math.log(i+1)/Math.log(2));
for(int j=0;j<level;j++){
System.out.print("--");
}
System.out.println(contents.get(i));
}
}
// private void traverseTree(int i, int before, int levels){
// if(2*i-1<contents.size()) {
// for (int i2 = 0; i2 < before; i2++) {
// System.out.print("-");
// }
// System.out.println(contents.get(i - 1));
// if (levels - before > 1) {
// if (i == 1) {
// traverseTree(2, ++before, levels);
// traverseTree(3, ++before, levels);
// } else {
// traverseTree(2 * i, ++before, levels);
// traverseTree(2 * i + 1, ++before, levels);
// }
// }
// }
// }
// public void traverseTree(){
// double t=Math.log(contents.size())/Math.log(2);
// if(t>=(int)t && t<((int)t+1)){
// t=(int)t;
// }else{
// t=(int)t+1;
// }
//
//
// t++; //t is just the number of levels or height, counting from 1
//
// int height=(int)t;
// traverseTree(1, 0, height);
// }
public void swap(int i, int j){
int a=contents.get(i);
int b=contents.get(j);
contents.set(j, a);
contents.set(i, b);
}
public int[] getChildren(int i){
i=i+1;
int[] children;
if(i==1){
if(contents.size()==1){
children=new int[1];
children[0]=contents.get(1);
}else{
children=new int[2];
children[0]=contents.get(1);
children[1]=contents.get(2);
}
}else{
if(contents.size()==2*i){
children=new int[1];
children[0]=contents.get(2*i-1);
}else{
children=new int[2];
children[0]=contents.get(2*i-1);
children[1]=contents.get(2*i);
}
}
return children;
}
public int[] getChildrenIndex(int i){
i=i+1;
int[] children;
if(i==1){
if(contents.size()==1){
children=new int[1];
children[0]=1;
}else{
children=new int[2];
children[0]=1;
children[1]=2;
}
}else{
if(contents.size()==2*i){
children=new int[1];
children[0]=2*i-1;
}else{
children=new int[2];
children[0]=2*i-1;
children[1]=2*i;
}
}
return children;
}
public void arrange(int index){
int[] children=getChildrenIndex(index);
boolean arranged=true;
for(int i=0;i<children.length && children[i]<contents.size();i++){
if(contents.get(children[i]) > contents.get(index)){
arranged=false;
}
}
if(!arranged){
int max_child=0;
for(int i=0;i<children.length;i++){
if(contents.get(children[i]) > contents.get(index)){
// swap(children[i], index);
// arrange(children[i]);
// return;
if(contents.get(children[i])>contents.get(children[max_child])){
max_child=i;
}
}
}
swap(children[max_child], index);
arrange(children[max_child]);
return;
}
}
public ArrayList<Integer> sort(ArrayList<Integer> A){
if(contents.size()==0){
return A;
}
swap(0, currentIndex-1);
System.out.print(contents.get(currentIndex-1)+"\t");
A.add(new Integer(contents.get(currentIndex-1)));
// int a=contents.get(0);
// int b=contents.get(currentIndex-1);
// contents.set(currentIndex-1, a);
// contents.set(0, b);
contents.remove(currentIndex-1);
arrange(0);
currentIndex--;
sort(A);
return A;
}
public static void main(String[] args){
HeapSort1 heapWithArray=new HeapSort1();
heapWithArray.insert(8);
heapWithArray.insert(4);
heapWithArray.insert(7);
heapWithArray.insert(1);
heapWithArray.insert(3);
heapWithArray.insert(5);
heapWithArray.insert(17);
heapWithArray.insert(12);
heapWithArray.traverseByLevel();
ArrayList<Integer> A=new ArrayList<>();
A=heapWithArray.sort(A);
System.out.println();
for(int i=0;i<A.size();i++){
System.out.print(A.get(i)+"\t");
}
}
}