Skip to content

Commit

Permalink
add merge sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopson97 committed Sep 4, 2018
1 parent 2161dc2 commit 119d499
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 20 deletions.
13 changes: 0 additions & 13 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,14 @@ apply plugin: 'application'
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

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

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

dependencies {
// TODO: Add dependencies here ...
// You can read more about how to add dependency here:
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
testCompile group: 'junit', name: 'junit', version: '4.10'
}
10 changes: 6 additions & 4 deletions src/main/java/sortVisualiser/SortVisualiser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sortVisualiser.algorithms.BubbleSort;
import sortVisualiser.algorithms.ISortAlgorithm;
import sortVisualiser.algorithms.InsertionSort;
import sortVisualiser.algorithms.MergeSort;
import sortVisualiser.algorithms.QuickSort;
import sortVisualiser.algorithms.SelectionSort;
import static util.Sleep.secondsToNano;
Expand Down Expand Up @@ -32,10 +33,11 @@ public SortVisualiser() {
window.setVisible(true);

sortQueue = new ArrayList<>();
sortQueue.add(new QuickSort());
sortQueue.add(new SelectionSort());
sortQueue.add(new InsertionSort());
sortQueue.add(new BubbleSort());
sortQueue.add(new MergeSort());
//sortQueue.add(new QuickSort());
//sortQueue.add(new SelectionSort());
//sortQueue.add(new InsertionSort());
//sortQueue.add(new BubbleSort());
}

private void shuffleAndWait() {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/sortVisualiser/algorithms/InsertionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ public String getName() {
public long getDelay() {
return 2;
}

}
83 changes: 83 additions & 0 deletions src/main/java/sortVisualiser/algorithms/MergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package sortVisualiser.algorithms;

import sortVisualiser.SortArray;

/**
* Merge sort implementation
* @author Matthew Hopson
*/
public class MergeSort implements ISortAlgorithm
{
private int[] getSubArray(SortArray array, int begin, int size) {
int arr[] = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = array.getValue(begin + i);
}
return arr;
}

private void merge(SortArray array, int left, int middle, int right) {
//Size of the left and right "sub arrays"
int leftSize = middle - left + 1;
int rightSize = right - middle;

int leftArray[] = getSubArray(array, left, leftSize);
int rightArray[] = getSubArray(array, middle + 1, rightSize);

int i = 0, j = 0, k = left;
while (i < leftSize && j < rightSize) {
if (leftArray[i] <= rightArray[j]) {
array.updateSingle(k, leftArray[i], getDelay());
i++;
}
else {
array.updateSingle(k, rightArray[j], getDelay());
j++;
}
k++;
}
//remaining stufffff
while (i < leftSize) {
array.updateSingle(k, leftArray[i], getDelay());
i++;
k++;
}
while (j < rightSize) {
array.updateSingle(k, rightArray[j], getDelay());
j++;
k++;
}
}

private void mergeSort(SortArray array, int left, int right) {
if (left < right) {
int middleIndex = (left + right) / 2;

//Merge sort is a "divide and conquer" algorithm
//It works by splitting the array into tiny sections
//and sorting them indivually,
//and then finally merges it back together
mergeSort(array, left, middleIndex);
mergeSort(array, middleIndex + 1, right);

merge(array, left, middleIndex, right);
}
}

@Override
public void runSort(SortArray array) {
int left = 0;
int right = array.arraySize() - 1;
mergeSort(array, left, right);
}

@Override
public String getName() {
return "Merge Sort";
}

@Override
public long getDelay() {
return 10;
}
}
5 changes: 3 additions & 2 deletions src/main/java/sortVisualiser/algorithms/SelectionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public class SelectionSort implements ISortAlgorithm
{
@Override
public void runSort(SortArray array) {
for (int i = 0; i < array.arraySize() - 1; i++) {
int len = array.arraySize();
for (int i = 0; i < len - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < array.arraySize(); j++) {
for (int j = i + 1; j < len; j++) {
if (array.getValue(j) < array.getValue(minIndex)) {
minIndex = j;
}
Expand Down

0 comments on commit 119d499

Please sign in to comment.