Added merge sort
This commit is contained in:
parent
c161e4bdd8
commit
7bad0e2c18
8 changed files with 171 additions and 4 deletions
135
src/zutil/algo/sort/MergeSort.java
Normal file
135
src/zutil/algo/sort/MergeSort.java
Normal file
|
|
@ -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<tmp.length ;++i){
|
||||||
|
tmp[i] = list[start+i];
|
||||||
|
}
|
||||||
|
|
||||||
|
int index1 = 0;
|
||||||
|
int index2 = length;
|
||||||
|
for(int i=start; i<stop ;++i){
|
||||||
|
if( index2 < stop-start && (index1 >= 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 <T>
|
||||||
|
*
|
||||||
|
* @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 <T> void merge(SortableDataList<T> list, int start, int stop, int pivot){
|
||||||
|
int length = pivot-start;
|
||||||
|
Object[] tmp = new Object[stop-start];
|
||||||
|
|
||||||
|
for(int i=0; i<tmp.length ;++i){
|
||||||
|
tmp[i] = list.get( start+i );
|
||||||
|
}
|
||||||
|
|
||||||
|
int index1 = 0;
|
||||||
|
int index2 = length;
|
||||||
|
for(int i=start; i<stop ;++i){
|
||||||
|
if( index2 < stop-start && (index1 >= length || ((Comparable)tmp[index1]).compareTo(tmp[index2]) > 0 )){
|
||||||
|
list.set(i, (T)tmp[index2]);
|
||||||
|
++index2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
list.set(i, (T)tmp[index1]);
|
||||||
|
++index1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -13,6 +13,10 @@ public class SortableArrayList<T> implements SortableDataList<T>{
|
||||||
return list.get(i);
|
return list.get(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void set(int i, T o){
|
||||||
|
list.set(i, o);
|
||||||
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return list.size();
|
return list.size();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,10 @@ public class SortableComparableArray implements SortableDataList<Comparable>{
|
||||||
return list[i];
|
return list[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void set(int i, Comparable o){
|
||||||
|
list[i] = o;
|
||||||
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return list.length;
|
return list.length;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,20 @@ public interface SortableDataList<T>{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a is a specific index i the list
|
* Returns a is a specific index i the list
|
||||||
|
*
|
||||||
* @param i is the index
|
* @param i is the index
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public T get(int i);
|
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
|
* Returns the size of the list
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,10 @@ public class SortableIntArray implements SortableDataList<Integer>{
|
||||||
return list[i];
|
return list[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void set(int i, Integer o){
|
||||||
|
list[i] = o;
|
||||||
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return list.length;
|
return list.length;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,10 @@ public class SortableLinkedList<T> implements SortableDataList<T>{
|
||||||
return list.get(i);
|
return list.get(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void set(int i, T o){
|
||||||
|
list.set(i, o);
|
||||||
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return list.size();
|
return list.size();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -142,6 +142,10 @@ public class MedianFilter extends ImageFilterProcessor{
|
||||||
return data[ getY(i) ][ getX(i) ][ channel ];
|
return data[ getY(i) ][ getX(i) ][ channel ];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void set(int i, Integer o){
|
||||||
|
data[ getY(i) ][ getX(i) ][ channel ] = o;
|
||||||
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return cols * rows;
|
return cols * rows;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
package zutil.test;
|
package zutil.test;
|
||||||
|
import zutil.algo.sort.MergeSort;
|
||||||
import zutil.algo.sort.QuickSort;
|
import zutil.algo.sort.QuickSort;
|
||||||
import zutil.algo.sort.SimpleSort;
|
import zutil.algo.sort.SimpleSort;
|
||||||
import zutil.algo.sort.sortable.SortableIntArray;
|
import zutil.algo.sort.sortable.SortableIntArray;
|
||||||
|
|
@ -20,10 +21,12 @@ public class SortTestSimple extends TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
long time = System.currentTimeMillis();
|
long time = System.currentTimeMillis();
|
||||||
//SimpleSort.bubbleSort(new SortableIntArray(array));
|
//SimpleSort.bubbleSort( new SortableIntArray(array) );
|
||||||
//SimpleSort.selectionSort(new SortableIntArray(array));
|
//SimpleSort.selectionSort( new SortableIntArray(array) );
|
||||||
SimpleSort.insertionSort(new SortableIntArray(array));
|
//SimpleSort.insertionSort( new SortableIntArray(array) );
|
||||||
//QuickSort.sort(new SortableIntArray(array));
|
//QuickSort.sort( new SortableIntArray(array) );
|
||||||
|
//MergeSort.sort( array );
|
||||||
|
MergeSort.sort( new SortableIntArray(array) );
|
||||||
time = System.currentTimeMillis() - time;
|
time = System.currentTimeMillis() - time;
|
||||||
|
|
||||||
System.out.println("\n--------------------------------------------");
|
System.out.println("\n--------------------------------------------");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue