Skip to content

Add merge sort #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/src/main/assets/desc.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@
"<a href='https://en.wikipedia.org/wiki/Quicksort'>Quicksort</a><br>"
]
},
"merge_sort": {
"Merge Sort": "Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves.",
"Complexity": {
"Time": "worst O(nlogn), best O(nlogn), average O(nlogn)",
"Space": "worst O(n)"
},
"linked_list": {
"Singly linked list": "A Linked List is a linear collection of data elements, called nodes, each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of data and a reference (in other words, a link) to the next node in the sequence. ",
"Complexity": {
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/com/naman14/algovisualizer/AlgorithmCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,34 @@ public class AlgorithmCode {
" }\n" +
"}";

public static final String CODE_MERGE_SORT = " void mergesort(int[] array, int[] temp, int leftStart, int rightEnd){\n" +
" if(leftStart >= rightEnd) return;\n" +
" int middle = (leftStart + rightEnd) / 2;\n" +
" mergesort(array, temp, leftStart, middle);\n" +
" mergesort(array, temp, middle + 1, rightEnd);\n" +
" mergeHalves(array, temp, leftStart, rightEnd);\n" +
" }\n" +
"\n" +
" void mergeHalves(int[] array, int[] temp, int leftStart, int rightEnd){\n" +
" int leftEnd = (leftStart + rightEnd) / 2;\n" +
" int rightStart = leftEnd + 1;\n" +
" int size = rightEnd - leftStart + 1;\n" +
"\n" +
" int left = leftStart;\n" +
" int right = rightStart;\n" +
" int index = leftStart;\n" +
"\n" +
" while(left <= leftEnd && right <= rightEnd) {\n" +
" if(array[left] <= array[right]){\n" +
" temp[index] = array[left];\n" +
" left++;\n" +
" } else{\n" +
" temp[index] = array[right];\n" +
" right++;\n" +
" }\n" +
" index++;\n" +
" }";

public static final String CODE_BST_SEARCH = " int id = DataUtils.getRandomKeyFromBST();\n" +
" addLog(\"Searching for \" + String.valueOf(id));\n" +
" BinarySearchTree.Node current = b.getRoot();\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public void setCode(String key) {
case Algorithm.QUICKSORT:
addCodeItem(AlgorithmCode.CODE_QUICKSORT, "Quicksort");
break;
case Algorithm.MERGE_SORT:
addCodeItem(AlgorithmCode.CODE_MERGE_SORT, "Merge sort");
break;
case Algorithm.BST_SEARCH:
addCodeItem(AlgorithmCode.CODE_BST_SEARCH, "BST Search");
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public boolean onChildClick(ExpandableListView expandableListView, View view, in
case 3:
algoFragment.setupFragment(Algorithm.QUICKSORT);
break;
case 4:
algoFragment.setupFragment(Algorithm.MERGE_SORT);
}
break;
case 2:
Expand Down Expand Up @@ -227,6 +229,7 @@ private void prepareListData() {
heading2.add("Insertion Sort");
heading2.add("Selection Sort");
heading2.add("Quicksort");
heading2.add("Merge Sort");
List<String> heading3 = new ArrayList<String>();
heading3.add("BST Search");
heading3.add("BST Insert");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.naman14.algovisualizer.algorithm.search.LinearSearch;
import com.naman14.algovisualizer.algorithm.sorting.BubbleSort;
import com.naman14.algovisualizer.algorithm.sorting.InsertionSort;
import com.naman14.algovisualizer.algorithm.sorting.MergeSort;
import com.naman14.algovisualizer.algorithm.sorting.QuickSort;
import com.naman14.algovisualizer.algorithm.sorting.SelectionSort;
import com.naman14.algovisualizer.algorithm.tree.bst.BSTAlgorithm;
Expand Down Expand Up @@ -205,6 +206,12 @@ public void setupFragment(String algorithmKey) {
algorithm = new BSTAlgorithm((BSTVisualizer) visualizer, getActivity(), logFragment);
((BSTAlgorithm) algorithm).setData(DataUtils.createBinaryTree());
break;
case Algorithm.MERGE_SORT:
visualizer = new SortingVisualizer(getActivity());
appBarLayout.addView(visualizer);
algorithm = new MergeSort((SortingVisualizer) visualizer, getActivity(), logFragment);
((MergeSort) algorithm).setData(DataUtils.createRandomArray(30));
break;
case Algorithm.BST_INSERT:
visualizer = new BSTVisualizer(getActivity(), 280);
ArrayVisualizer arrayVisualizer = new ArrayVisualizer(getActivity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Algorithm extends HandlerThread {
public static final String INSERTION_SORT = "insertion_sort";
public static final String SELECTION_SORT = "selection_sort";
public static final String QUICKSORT = "quicksort";
public static final String MERGE_SORT = "merge_sort";
public static final String BINARY_SEARCH = "binary_search";
public static final String LINEAR_SEARCH = "linear_search";
public static final String BST_INSERT = "bst_insert";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.naman14.algovisualizer.algorithm.sorting;

import android.app.Activity;

import com.naman14.algovisualizer.LogFragment;
import com.naman14.algovisualizer.algorithm.Algorithm;
import com.naman14.algovisualizer.algorithm.DataHandler;
import com.naman14.algovisualizer.visualizer.SortingVisualizer;

public class MergeSort extends SortAlgorithm implements DataHandler {

int[] array;

public MergeSort(SortingVisualizer visualizer, Activity activity, LogFragment logFragment) {
this.visualizer = visualizer;
this.activity = activity;
this.logFragment = logFragment;
}


@Override
public void run() {
super.run();
}

// Sorting algorithm (merge sort)
private void sort() {
logArray("Original array - ", array);
if (array == null || array.length == 0) return;

mergesort(array, new int[array.length], 0, array.length - 1);
addLog("Array has been sorted");
completed();
}

private void mergesort(int[] array, int[] temp, int leftStart, int rightEnd){
if(leftStart >= rightEnd) return;
int middle = (leftStart + rightEnd) / 2;
mergesort(array, temp, leftStart, middle);
mergesort(array, temp, middle + 1, rightEnd);
mergeHalves(array, temp, leftStart, rightEnd);
}

private void mergeHalves(int[] array, int[] temp, int leftStart, int rightEnd) {
int leftEnd = (leftStart + rightEnd) / 2;
int rightStart = leftEnd + 1;
int size = rightEnd - leftStart + 1;

int left = leftStart;
int right = rightStart;
int index = leftStart;

while(left <= leftEnd && right <= rightEnd) {
highlightSwap(left, right);
addLog("Swapping " + array[left] + " and " + array[right]);
if(array[left] <= array[right]){
temp[index] = array[left];
left++;
} else{
temp[index] = array[right];
right++;
}
sleep();
index++;
}
// given two arrays, copies one to the other
System.arraycopy(array, left, temp, index, leftEnd - left + 1);
System.arraycopy(array, right, temp, index, rightEnd - right + 1);
System.arraycopy(temp, leftStart, array, leftStart, size);

}

@Override
public void onDataRecieved(Object data) {
super.onDataRecieved(data);
this.array = (int[]) data;
}

@Override
public void onMessageReceived(String message) {
super.onMessageReceived(message);
if (message.equals(Algorithm.COMMAND_START_ALGORITHM)) {
startExecution();
sort();
}
}

}