Skip to content

Commit 0fc1160

Browse files
committed
Day 9
1 parent b4f081f commit 0fc1160

15 files changed

+707
-0
lines changed

ch-23/.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store

ch-23/.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-23/.idea/inspectionProfiles/Project_Default.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-23/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-23/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-23/.idea/uiDesigner.xml

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-23/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch-23/ch-23.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

ch-23/src/HeapSort.java

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
4+
/**
5+
*
6+
* *23.8 (Sort using a heap)
7+
8+
* Implement the following sort method using a heap.
9+
* public static <E extends Comparable<E>> void sort(E[] list)
10+
*
11+
*/
12+
public class HeapSort {
13+
public static void main(String[] args) {
14+
Integer[] integers = {9,7,4,3,21,5,6,3,2,1,6,88,54,334,5,3};
15+
sort(integers);
16+
System.out.println(Arrays.toString(integers));
17+
}
18+
19+
public static <E extends Comparable<E>> void sort(E[] list) {
20+
Heap<E> heap = new Heap<>();
21+
22+
for(E o : list) {
23+
heap.add(o);
24+
}
25+
26+
for(int i = list.length - 1; i >= 0; i--) {
27+
list[i] = heap.remove();
28+
}
29+
}
30+
}
31+
32+
class Heap<E extends Comparable<E>> {
33+
private ArrayList<E> list = new ArrayList<>();
34+
35+
public Heap() {
36+
}
37+
38+
public Heap(E[] list){
39+
for(E o : list)
40+
add(o);
41+
}
42+
43+
public void add(E o) {
44+
list.add(o);
45+
int currentIndex = list.size() - 1;
46+
47+
while (currentIndex > 0) {
48+
int parentIndex = (currentIndex - 1) / 2;
49+
if(list.get(currentIndex).compareTo(list.get(parentIndex)) > 0) {
50+
E temp = list.get(parentIndex);
51+
list.set(parentIndex, list.get(currentIndex));
52+
list.set(currentIndex, temp);
53+
} else {
54+
break;
55+
}
56+
57+
currentIndex = parentIndex;
58+
}
59+
}
60+
61+
public E remove() {
62+
if (list.size() == 0) return null;
63+
64+
E removedObject = list.get(0);
65+
list.set(0, list.get(list.size() - 1));
66+
list.remove(list.size() - 1);
67+
68+
int currentIndex = 0;
69+
while(currentIndex < list.size()) {
70+
int leftChildIndex = currentIndex * 2 + 1;
71+
int rightChildIndex = currentIndex * 2 + 2;
72+
if(leftChildIndex >= list.size()) break;
73+
int maxIndex = leftChildIndex;
74+
if(rightChildIndex < list.size()) {
75+
if(list.get(maxIndex).compareTo(list.get(rightChildIndex)) < 0) {
76+
maxIndex = rightChildIndex;
77+
}
78+
}
79+
80+
if(list.get(currentIndex).compareTo(list.get(maxIndex)) < 0) {
81+
E temp = list.get(maxIndex);
82+
list.set(maxIndex, list.get(currentIndex));
83+
list.set(currentIndex, temp);
84+
currentIndex = maxIndex;
85+
} else {
86+
break;
87+
}
88+
}
89+
90+
return removedObject;
91+
}
92+
93+
public int getSize() {
94+
return list.size();
95+
}
96+
}

ch-23/src/algorthim/BubbleSort.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package algorthim;
2+
3+
import java.util.Arrays;
4+
5+
public class BubbleSort {
6+
public static void main(String[] args) {
7+
int[] list = {2, 9, 5, 4, 8, 1, 7, 6};
8+
bubbleSort(list);
9+
System.out.println(Arrays.toString(list));
10+
}
11+
12+
public static void bubbleSort(int[] list) {
13+
for(int k = 1; k < list.length; k++) {
14+
for(int i = 0; i < list.length - k; i++) {
15+
if(list[i] > list[i + 1]) {
16+
int temp = list[i + 1];
17+
list[i + 1] = list[i];
18+
list[i] = temp;
19+
}
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)