Skip to content

Commit 119d499

Browse files
committed
add merge sort
1 parent 2161dc2 commit 119d499

File tree

5 files changed

+92
-20
lines changed

5 files changed

+92
-20
lines changed

build.gradle

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,14 @@ apply plugin: 'application'
44
sourceCompatibility = '1.8'
55
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
66

7-
// NetBeans will automatically add "run" and "debug" tasks relying on the
8-
// "mainClass" property. You may however define the property prior executing
9-
// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
10-
//
11-
// Note however, that you may define your own "run" and "debug" task if you
12-
// prefer. In this case NetBeans will not add these tasks but you may rely on
13-
// your own implementation.
147
if (!hasProperty('mainClass')) {
158
ext.mainClass = 'sortVisualiser.SortVisualiser'
169
}
1710

1811
repositories {
1912
mavenCentral()
20-
// You may define additional repositories, or even remove "mavenCentral()".
21-
// Read more about repositories here:
22-
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
2313
}
2414

2515
dependencies {
26-
// TODO: Add dependencies here ...
27-
// You can read more about how to add dependency here:
28-
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
2916
testCompile group: 'junit', name: 'junit', version: '4.10'
3017
}

src/main/java/sortVisualiser/SortVisualiser.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sortVisualiser.algorithms.BubbleSort;
66
import sortVisualiser.algorithms.ISortAlgorithm;
77
import sortVisualiser.algorithms.InsertionSort;
8+
import sortVisualiser.algorithms.MergeSort;
89
import sortVisualiser.algorithms.QuickSort;
910
import sortVisualiser.algorithms.SelectionSort;
1011
import static util.Sleep.secondsToNano;
@@ -32,10 +33,11 @@ public SortVisualiser() {
3233
window.setVisible(true);
3334

3435
sortQueue = new ArrayList<>();
35-
sortQueue.add(new QuickSort());
36-
sortQueue.add(new SelectionSort());
37-
sortQueue.add(new InsertionSort());
38-
sortQueue.add(new BubbleSort());
36+
sortQueue.add(new MergeSort());
37+
//sortQueue.add(new QuickSort());
38+
//sortQueue.add(new SelectionSort());
39+
//sortQueue.add(new InsertionSort());
40+
//sortQueue.add(new BubbleSort());
3941
}
4042

4143
private void shuffleAndWait() {

src/main/java/sortVisualiser/algorithms/InsertionSort.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ public String getName() {
3030
public long getDelay() {
3131
return 2;
3232
}
33-
3433
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package sortVisualiser.algorithms;
2+
3+
import sortVisualiser.SortArray;
4+
5+
/**
6+
* Merge sort implementation
7+
* @author Matthew Hopson
8+
*/
9+
public class MergeSort implements ISortAlgorithm
10+
{
11+
private int[] getSubArray(SortArray array, int begin, int size) {
12+
int arr[] = new int[size];
13+
for (int i = 0; i < size; i++) {
14+
arr[i] = array.getValue(begin + i);
15+
}
16+
return arr;
17+
}
18+
19+
private void merge(SortArray array, int left, int middle, int right) {
20+
//Size of the left and right "sub arrays"
21+
int leftSize = middle - left + 1;
22+
int rightSize = right - middle;
23+
24+
int leftArray[] = getSubArray(array, left, leftSize);
25+
int rightArray[] = getSubArray(array, middle + 1, rightSize);
26+
27+
int i = 0, j = 0, k = left;
28+
while (i < leftSize && j < rightSize) {
29+
if (leftArray[i] <= rightArray[j]) {
30+
array.updateSingle(k, leftArray[i], getDelay());
31+
i++;
32+
}
33+
else {
34+
array.updateSingle(k, rightArray[j], getDelay());
35+
j++;
36+
}
37+
k++;
38+
}
39+
//remaining stufffff
40+
while (i < leftSize) {
41+
array.updateSingle(k, leftArray[i], getDelay());
42+
i++;
43+
k++;
44+
}
45+
while (j < rightSize) {
46+
array.updateSingle(k, rightArray[j], getDelay());
47+
j++;
48+
k++;
49+
}
50+
}
51+
52+
private void mergeSort(SortArray array, int left, int right) {
53+
if (left < right) {
54+
int middleIndex = (left + right) / 2;
55+
56+
//Merge sort is a "divide and conquer" algorithm
57+
//It works by splitting the array into tiny sections
58+
//and sorting them indivually,
59+
//and then finally merges it back together
60+
mergeSort(array, left, middleIndex);
61+
mergeSort(array, middleIndex + 1, right);
62+
63+
merge(array, left, middleIndex, right);
64+
}
65+
}
66+
67+
@Override
68+
public void runSort(SortArray array) {
69+
int left = 0;
70+
int right = array.arraySize() - 1;
71+
mergeSort(array, left, right);
72+
}
73+
74+
@Override
75+
public String getName() {
76+
return "Merge Sort";
77+
}
78+
79+
@Override
80+
public long getDelay() {
81+
return 10;
82+
}
83+
}

src/main/java/sortVisualiser/algorithms/SelectionSort.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ public class SelectionSort implements ISortAlgorithm
1010
{
1111
@Override
1212
public void runSort(SortArray array) {
13-
for (int i = 0; i < array.arraySize() - 1; i++) {
13+
int len = array.arraySize();
14+
for (int i = 0; i < len - 1; i++) {
1415
int minIndex = i;
15-
for (int j = i + 1; j < array.arraySize(); j++) {
16+
for (int j = i + 1; j < len; j++) {
1617
if (array.getValue(j) < array.getValue(minIndex)) {
1718
minIndex = j;
1819
}

0 commit comments

Comments
 (0)