From 7bad0e2c180ff899d016ffc756a01ba3cc94d15e Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Fri, 26 Feb 2010 18:39:18 +0000 Subject: [PATCH] Added merge sort --- src/zutil/algo/sort/MergeSort.java | 135 ++++++++++++++++++ .../algo/sort/sortable/SortableArrayList.java | 4 + .../sortable/SortableComparableArray.java | 4 + .../algo/sort/sortable/SortableDataList.java | 9 ++ .../algo/sort/sortable/SortableIntArray.java | 4 + .../sort/sortable/SortableLinkedList.java | 4 + src/zutil/image/filters/MedianFilter.java | 4 + src/zutil/test/SortTestSimple.java | 11 +- 8 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 src/zutil/algo/sort/MergeSort.java diff --git a/src/zutil/algo/sort/MergeSort.java b/src/zutil/algo/sort/MergeSort.java new file mode 100644 index 0000000..2b85d15 --- /dev/null +++ b/src/zutil/algo/sort/MergeSort.java @@ -0,0 +1,135 @@ +package zutil.algo.sort; + +import zutil.algo.sort.sortable.SortableDataList; + + +public class MergeSort{ + + /** + * Sorts the given list with Merge sort + * + * @param list is the list to sort + */ + public static void sort(int[] list){ + if(list == null) + return; + + sort(list, 0, list.length); + } + + /** + * This method is the array splitting method + * that recursively splits the array in two. + * + * @param list is the list to sort + * @param start is the starting index of the sub list + * @param stop is the end index of the sub list + */ + protected static void sort(int[] list, int start, int stop){ + if(stop-start <= 1) return; + + int pivot = start+(stop-start)/2; + sort(list, start, pivot); + sort(list, pivot, stop); + + merge(list, start, stop, pivot); + } + + /** + * This method is the merger, after the array + * has been split this method will merge the + * two parts of the array and sort it. + * + * @param list is the list to merge + * @param start is the start of the first sublist + * @param stop is the end of the second sublist + * @param pivot is the end index for the first list and the beginning of the second. + */ + protected static void merge(int[] list, int start, int stop, int pivot){ + int length = pivot-start; + int[] tmp = new int[stop-start]; + + for(int i=0; i= length || tmp[index1] > tmp[index2]) ){ + list[i] = tmp[index2]; + ++index2; + } + else { + list[i] = tmp[index1]; + ++index1; + } + } + } + + + /** + * Sorts the given list with Merge sort, + * this is slower than the one with int[] array + * + * @param list is the list to sort + */ + public static void sort(SortableDataList list){ + if(list == null) + return; + + sort(list, 0, list.size()); + } + + /** + * This method is the array splitting method + * that recursively splits the array in two. + * + * @param list is the list to sort + * @param start is the starting index of the sub list + * @param stop is the end index of the sub list + */ + protected static void sort(SortableDataList list, int start, int stop){ + if(stop-start <= 1) return; + + int pivot = start+(stop-start)/2; + sort(list, start, pivot); + sort(list, pivot, stop); + + merge(list, start, stop, pivot); + } + + /** + * This method is the merger, after the array + * has been split this method will merge the + * two parts of the array and sort it. + * @param + * + * @param list is the list to merge + * @param start is the start of the first sublist + * @param stop is the end of the second sublist + * @param pivot is the end index for the first list and the beginning of the second. + */ + @SuppressWarnings("unchecked") + protected static void merge(SortableDataList list, int start, int stop, int pivot){ + int length = pivot-start; + Object[] tmp = new Object[stop-start]; + + for(int i=0; i= length || ((Comparable)tmp[index1]).compareTo(tmp[index2]) > 0 )){ + list.set(i, (T)tmp[index2]); + ++index2; + } + else { + list.set(i, (T)tmp[index1]); + ++index1; + } + } + } +} diff --git a/src/zutil/algo/sort/sortable/SortableArrayList.java b/src/zutil/algo/sort/sortable/SortableArrayList.java index 1bbbb7c..3446cf0 100644 --- a/src/zutil/algo/sort/sortable/SortableArrayList.java +++ b/src/zutil/algo/sort/sortable/SortableArrayList.java @@ -12,6 +12,10 @@ public class SortableArrayList implements SortableDataList{ public T get(int i) { return list.get(i); } + + public void set(int i, T o){ + list.set(i, o); + } public int size() { return list.size(); diff --git a/src/zutil/algo/sort/sortable/SortableComparableArray.java b/src/zutil/algo/sort/sortable/SortableComparableArray.java index c6866ea..ec531b1 100644 --- a/src/zutil/algo/sort/sortable/SortableComparableArray.java +++ b/src/zutil/algo/sort/sortable/SortableComparableArray.java @@ -11,6 +11,10 @@ public class SortableComparableArray implements SortableDataList{ public Comparable get(int i) { return list[i]; } + + public void set(int i, Comparable o){ + list[i] = o; + } public int size() { return list.length; diff --git a/src/zutil/algo/sort/sortable/SortableDataList.java b/src/zutil/algo/sort/sortable/SortableDataList.java index be83c4d..86cd798 100644 --- a/src/zutil/algo/sort/sortable/SortableDataList.java +++ b/src/zutil/algo/sort/sortable/SortableDataList.java @@ -4,11 +4,20 @@ public interface SortableDataList{ /** * Returns a is a specific index i the list + * * @param i is the index * @return */ public T get(int i); + /** + * Sets an Object in the specified index + * + * @param i is the index + * @param o is the Object + */ + public void set(int i, T o); + /** * Returns the size of the list * diff --git a/src/zutil/algo/sort/sortable/SortableIntArray.java b/src/zutil/algo/sort/sortable/SortableIntArray.java index 8b26fdd..f96848b 100644 --- a/src/zutil/algo/sort/sortable/SortableIntArray.java +++ b/src/zutil/algo/sort/sortable/SortableIntArray.java @@ -11,6 +11,10 @@ public class SortableIntArray implements SortableDataList{ return list[i]; } + public void set(int i, Integer o){ + list[i] = o; + } + public int size() { return list.length; } diff --git a/src/zutil/algo/sort/sortable/SortableLinkedList.java b/src/zutil/algo/sort/sortable/SortableLinkedList.java index c65f7bf..b164bc6 100644 --- a/src/zutil/algo/sort/sortable/SortableLinkedList.java +++ b/src/zutil/algo/sort/sortable/SortableLinkedList.java @@ -12,6 +12,10 @@ public class SortableLinkedList implements SortableDataList{ public T get(int i) { return list.get(i); } + + public void set(int i, T o){ + list.set(i, o); + } public int size() { return list.size(); diff --git a/src/zutil/image/filters/MedianFilter.java b/src/zutil/image/filters/MedianFilter.java index 39bdce3..7c656bc 100644 --- a/src/zutil/image/filters/MedianFilter.java +++ b/src/zutil/image/filters/MedianFilter.java @@ -141,6 +141,10 @@ public class MedianFilter extends ImageFilterProcessor{ public Integer get(int i) { return data[ getY(i) ][ getX(i) ][ channel ]; } + + public void set(int i, Integer o){ + data[ getY(i) ][ getX(i) ][ channel ] = o; + } public int size() { return cols * rows; diff --git a/src/zutil/test/SortTestSimple.java b/src/zutil/test/SortTestSimple.java index 39a7fa6..b73bf96 100644 --- a/src/zutil/test/SortTestSimple.java +++ b/src/zutil/test/SortTestSimple.java @@ -1,4 +1,5 @@ package zutil.test; +import zutil.algo.sort.MergeSort; import zutil.algo.sort.QuickSort; import zutil.algo.sort.SimpleSort; import zutil.algo.sort.sortable.SortableIntArray; @@ -20,10 +21,12 @@ public class SortTestSimple extends TestCase { } long time = System.currentTimeMillis(); - //SimpleSort.bubbleSort(new SortableIntArray(array)); - //SimpleSort.selectionSort(new SortableIntArray(array)); - SimpleSort.insertionSort(new SortableIntArray(array)); - //QuickSort.sort(new SortableIntArray(array)); + //SimpleSort.bubbleSort( new SortableIntArray(array) ); + //SimpleSort.selectionSort( new SortableIntArray(array) ); + //SimpleSort.insertionSort( new SortableIntArray(array) ); + //QuickSort.sort( new SortableIntArray(array) ); + //MergeSort.sort( array ); + MergeSort.sort( new SortableIntArray(array) ); time = System.currentTimeMillis() - time; System.out.println("\n--------------------------------------------");