Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
haidmoham committed Jul 20, 2016
0 parents commit ab1485b
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .idea/compiler.xml

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

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

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

41 changes: 41 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

11 changes: 11 additions & 0 deletions Implementations.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
49 changes: 49 additions & 0 deletions src/MergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Created by Mohammad Haider (haidmoham) on 7/19/16.
*/
import java.util.*;

public class MergeSort {
public static void main(String[] args) {
Integer[] a = {2, 6, 3, 5, 1};

mergeSort(a);

System.out.println(Arrays.toString(a));
}
public static Comparable[] mergeSort(Comparable[] list){
if (list.length <= 1){
return list;
}

Comparable[] first = new Comparable[list.length / 2];
Comparable[] second = new Comparable[list.length - first.length];
System.arraycopy(list, 0, first, 0, first.length);
System.arraycopy(list, first.length, second, 0, second.length);

mergeSort(first);
mergeSort(second);

merge(first, second, list);
return list;
}
private static void merge(Comparable[] first, Comparable[] second, Comparable[] result){
int iFirst = 0;
int iSecond = 0;
int iMerged = 0;

while (iFirst < first.length && iSecond < second.length){
if(first[iFirst].compareTo(second[iSecond]) < 0) {
result[iMerged] = first[iFirst];
iFirst++;
}
else {
result[iMerged] = second[iSecond];
iSecond++;
}
iMerged++;
}
System.arraycopy(first, iFirst, result, iMerged, first.length - iFirst);
System.arraycopy(second, iSecond, result, iMerged, second.length - iSecond);
}
}

0 comments on commit ab1485b

Please sign in to comment.