Last active
December 19, 2015 00:59
-
-
Save antonio081014/5872988 to your computer and use it in GitHub Desktop.
This is the code implementing Merge Sort.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class Main { | |
public static void main(String[] args) { | |
if (args.length == 0) { | |
System.out | |
.println("Proper Usage is: java main number1 number2 number3 ..."); | |
System.exit(0); | |
} | |
// <T extends Comparable<T>> | |
List<String> list = new ArrayList<String>(); | |
for (int i = 0; i < args.length; i++) | |
list.add(args[i]); | |
Main main = new Main(); | |
main.print(list); | |
main.merge_sort(list, 0, args.length - 1); | |
main.print(list); | |
} | |
public <T> void print(List<T> array) { | |
print(array, 0, array.size() - 1); | |
} | |
public <T> void print(List<T> array, int start, int end) { | |
if (start > end || start < 0 || end > array.size() - 1) | |
return; | |
for (int i = start; i <= end; i++) { | |
System.out.print(array.get(i) + " "); | |
} | |
System.out.println(); | |
} | |
public <T extends Comparable<T>> void merge_sort(List<T> array, int start, | |
int end) { | |
// print(array, start, end); | |
if (end == start) | |
return; | |
int mid = (start + end) / 2; | |
merge_sort(array, start, mid); | |
merge_sort(array, mid + 1, end); | |
if (array.get(mid).compareTo(array.get(mid + 1)) <= 0) | |
return; | |
List<T> arraycopy = new ArrayList<T>(); | |
for (int i = start; i <= end; i++) | |
arraycopy.add(array.get(i)); | |
int i = start; | |
int j = mid + 1; | |
int index = start; | |
while (i <= mid && j <= end) { | |
if (arraycopy.get(i - start).compareTo(arraycopy.get(j - start)) > 0) { | |
array.set(index++, arraycopy.get(j++ - start)); | |
} else { | |
array.set(index++, arraycopy.get(i++ - start)); | |
} | |
} | |
while (i <= mid) | |
array.set(index++, arraycopy.get(i++ - start)); | |
while (j <= end) | |
array.set(index++, arraycopy.get(j++ - start)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the implementation of mergesort in Java. | |
public class Main { | |
public static void main(String[] args) { | |
if (args.length == 0) { | |
System.out | |
.println("Proper Usage is: java main number1 number2 number3 ..."); | |
System.exit(0); | |
} | |
Main main = new Main(); | |
main.print(args); | |
main.merge_sort(args, 0, args.length - 1); | |
main.print(args); | |
} | |
public void print(String[] array) { | |
print(array, 0, array.length - 1); | |
} | |
public void print(String[] array, int start, int end) { | |
if (start > end || start < 0 || end > array.length - 1) | |
return; | |
for (int i = start; i <= end; i++) { | |
System.out.print(array[i] + " "); | |
} | |
System.out.println(); | |
} | |
public void merge_sort(String[] array, int start, int end) { | |
print(array, start, end); | |
if (end == start) | |
return; | |
int mid = (start + end) / 2; | |
merge_sort(array, start, mid); | |
merge_sort(array, mid + 1, end); | |
if (array[mid].compareTo(array[mid + 1]) <= 0) | |
return; | |
int i = start; | |
int j = mid + 1; | |
String[] arraycopy = new String[end - start + 1]; | |
System.arraycopy(array, start, arraycopy, 0, end - start + 1); | |
int index = start; | |
while (i <= mid && j <= end) { | |
if (arraycopy[i - start].compareTo(arraycopy[j - start]) > 0) { | |
array[index++] = arraycopy[j++ - start]; | |
} else { | |
array[index++] = arraycopy[i++ - start]; | |
} | |
} | |
while (i <= mid) | |
array[index++] = arraycopy[i++ - start]; | |
while (j <= end) | |
array[index++] = arraycopy[j++ - start]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment