diff --git a/src/zutil/algo/sort/ExternalSort.java b/src/zutil/algo/sort/ExternalSort.java index fba0196..bdcf250 100644 --- a/src/zutil/algo/sort/ExternalSort.java +++ b/src/zutil/algo/sort/ExternalSort.java @@ -114,7 +114,6 @@ public class ExternalSort { for(int i=1; i list){ + sort(list, 0, list.size()-1, MIDDLE_PIVOT, true); } /** - * Sort the elements in ascending order using quicksort. + * Sort the elements in ascending order using Quicksort. * - * @param A A list to sort. - * @param type type of pivot - * @param insert to use insertion sort when needed + * @param A is the list to sort. + * @param type is the type of pivot + * @param insert is if insertion sort will be used */ - @SuppressWarnings("unchecked") - public static void sort(SortableDataList list, int type, boolean insert){ + public static void sort(SortableDataList list, int type, boolean insert){ sort(list, 0, list.size()-1, type, insert); } /** - * Sort the elements in ascending order using qicksort. - * after the 10 th re write and a bad mood i found this - * site that gave me much help: - * http://www.inf.fh-flensburg.de/lang/algorithmen/ - * sortieren/quick/quicken.htm + * Sort the elements in ascending order using Quicksort. + * Reference: http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/quick/quicken.htm + * Complexity: O(n*log n) normally, but O(n^2) if the pivot is bad * - * @param A A list to sort. - * @param start The index to start from - * @param stop The index to stop - * @param type The type of pivot to use + * @param A is the list to sort. + * @param start is the index to start from + * @param stop is the index to stop + * @param type is the type of pivot to use */ @SuppressWarnings("unchecked") public static void sort(SortableDataList list, int start, int stop, int type, boolean insertionSort){ @@ -53,7 +49,7 @@ public class QuickSort{ SimpleSort.insertionSort( list, start, stop); } int pivotIndex = pivot(list,start,stop,type); - Object pivot = list.getIndex(pivotIndex); + Object pivot = list.get(pivotIndex); int left=start, right=stop; do{ @@ -80,25 +76,24 @@ public class QuickSort{ } - - @SuppressWarnings("unchecked") - private static int pivot(SortableDataList list, int start, int stop,int type){ + @SuppressWarnings("unchecked") + private static int pivot(SortableDataList list, int start, int stop,int type){ switch(type){ - case 0: + case RANDOM_PIVOT: return start+(int)(Math.random()*(stop-start)); - case 1: + case MEDIAN_PIVOT: Comparable[] i = new Comparable[]{ - (Comparable)list.getIndex(0), - (Comparable)list.getIndex(list.size()/2), - (Comparable)list.getIndex(list.size()-1)}; - SimpleSort.insertionSort(new SortableComparableArray(i),0,i.length); - if(i[i.length/2].compareTo(list.getIndex(start)) == 0) + (Comparable)list.get(0), + (Comparable)list.get(list.size()/2), + (Comparable)list.get(list.size()-1)}; + SimpleSort.insertionSort(new SortableComparableArray(i)); + if(i[i.length/2].compareTo(list.get(start)) == 0) return start; - else if(i[i.length/2].compareTo(list.getIndex(stop)) == 0) + else if(i[i.length/2].compareTo(list.get(stop)) == 0) return stop; else return start+(stop-start)/2; - case 2: + case MIDDLE_PIVOT: return (start+stop)/2; } return 0; diff --git a/src/zutil/algo/sort/Randomizer.java b/src/zutil/algo/sort/Randomizer.java index 6ffb25c..0e29caa 100644 --- a/src/zutil/algo/sort/Randomizer.java +++ b/src/zutil/algo/sort/Randomizer.java @@ -9,7 +9,6 @@ import zutil.algo.sort.sortable.SortableDataList; * @author Ziver */ public class Randomizer { - /** * Randomizes the index of all the elements diff --git a/src/zutil/algo/sort/SimpleSort.java b/src/zutil/algo/sort/SimpleSort.java index 12547aa..463253b 100644 --- a/src/zutil/algo/sort/SimpleSort.java +++ b/src/zutil/algo/sort/SimpleSort.java @@ -3,7 +3,7 @@ package zutil.algo.sort; import zutil.algo.sort.sortable.SortableDataList; /** - * A collection of sorting algorithms for arrays of integers. + * A collection of simple and slow sorting algorithms. * * @author Ziver Koc * @version 2006-10-31 @@ -11,57 +11,91 @@ import zutil.algo.sort.sortable.SortableDataList; public class SimpleSort{ /** - * Sort the elements in ascending order using selection sort. - * This algorithm has time complexity Theta(n*n), where n is - * the length of the array. - * - * @param list A list to sort. - * @return The same array sorted in ascending order. + * Sort the elements in ascending order using bubble sort + * witch is the slowest of the algorithms. + * Complexity: O(n^2). + * + * @param A is the list to sort. */ - @SuppressWarnings("unchecked") - public static SortableDataList selectionSort(SortableDataList list){ - int n = list.size(); - for (int i = 0; i < n - 1; i++) { - // find index m of min element in v[i..n-1] + public static void bubbleSort(SortableDataList list){ + bubbleSort(list, 0, list.size()); + } + /** + * Sort the elements in ascending order using bubble sort + * witch is the slowest of the algorithms. + * Complexity: O(n^2). + * + * @param A is an array of integers. + * @param start is the index to start from + * @param stop is the index to stop + */ + public static void bubbleSort(SortableDataList list, int start, int stop){ + for(int i=start; i 0){ + list.swap(j, j+1); + } + } + } + } + + /** + * Sort the elements in ascending order using selection sort + * witch in practice is 40% faster than bubble sort. + * Complexity: O(n^2). + * + * @param list is the list to sort. + */ + public static void selectionSort(SortableDataList list){ + selectionSort(list, 0, list.size()); + } + /** + * Sort the elements in ascending order using selection sort + * witch in practice is 40% faster than bubble sort. + * Complexity: O(n^2). + * + * @param list is the list to sort. + * @param start is the index to start from + * @param stop is the index to stop + */ + public static void selectionSort(SortableDataList list, int start, int stop){ + for (int i = start; i < stop - 1; i++) { + // find index m of the minimum element in v[i..n-1] int m = i; - for (int j = i + 1; j < n; j++) { + for (int j = i + 1; j < stop; j++) { if (list.compare(j, m) < 0) m = j; } // swap v[i] and v[m] list.swap(i, m); } - - return list; } - + /** - * Sort the elements in ascending order using insertionsort. + * Sort the elements in ascending order using insertion sort + * witch in practice is 5 times faster than bubble sort. + * Complexity: O(n^2). * - * @param A A list to sort. + * @param A is a list to sort. */ - @SuppressWarnings("unchecked") - public static SortableDataList insertionSort(SortableDataList list){ - return insertionSort(list, 0, list.size()); - } - + public static void insertionSort(SortableDataList list){ + insertionSort(list, 0, list.size()); + } /** - * Sort the elements in ascending order using insertionsort. + * Sort the elements in ascending order using insertion sort + * witch in practice is 5 times faster than bubble sort. + * Complexity: O(n^2). * - * @param A An array of integers. - * @param start The index to start from - * @param stop The index to stop + * @param A is an array of integers. + * @param start is the index to start from + * @param stop is the index to stop */ - @SuppressWarnings("unchecked") - public static SortableDataList insertionSort(SortableDataList list, int start, int stop){ - for(int i=start; istart ;j--){ - if(list.compare(j, j-1) < 0){ - list.swap(j, j-1); - } - } - } - return list; + public static void insertionSort(SortableDataList list, int start, int stop){ + for(int i=start; i0 ;--j){ + list.swap(j-1, j); + } + } } } \ No newline at end of file diff --git a/src/zutil/algo/sort/sortable/SortableArrayList.java b/src/zutil/algo/sort/sortable/SortableArrayList.java index 6b56207..1bbbb7c 100644 --- a/src/zutil/algo/sort/sortable/SortableArrayList.java +++ b/src/zutil/algo/sort/sortable/SortableArrayList.java @@ -9,7 +9,7 @@ public class SortableArrayList implements SortableDataList{ this.list = list; } - public T getIndex(int i) { + public T get(int i) { return list.get(i); } diff --git a/src/zutil/algo/sort/sortable/SortableComparableArray.java b/src/zutil/algo/sort/sortable/SortableComparableArray.java index d974f96..c6866ea 100644 --- a/src/zutil/algo/sort/sortable/SortableComparableArray.java +++ b/src/zutil/algo/sort/sortable/SortableComparableArray.java @@ -8,7 +8,7 @@ public class SortableComparableArray implements SortableDataList{ this.list = list; } - public Comparable getIndex(int i) { + public Comparable get(int i) { return list[i]; } diff --git a/src/zutil/algo/sort/sortable/SortableDataList.java b/src/zutil/algo/sort/sortable/SortableDataList.java index 90ecc1a..be83c4d 100644 --- a/src/zutil/algo/sort/sortable/SortableDataList.java +++ b/src/zutil/algo/sort/sortable/SortableDataList.java @@ -3,23 +3,24 @@ package zutil.algo.sort.sortable; public interface SortableDataList{ /** - * Returns a specific index i the list - * @param i The index + * Returns a is a specific index i the list + * @param i is the index * @return */ - public T getIndex(int i); + public T get(int i); /** * Returns the size of the list * - * @return The size of the list + * @return the size of the list */ public int size(); /** * Swaps the given indexes - * @param a First index - * @param b Second index + * + * @param a is the first index + * @param b is the second index */ public void swap(int a, int b); @@ -29,8 +30,8 @@ public interface SortableDataList{ * >0 if a>b , * =0 if a=b * - * @param a Firs index to compare - * @param b Second index to compare + * @param a is the first index to compare + * @param b is the second index to compare * @return Look at the info */ public int compare(int a, int b); @@ -41,8 +42,8 @@ public interface SortableDataList{ * >0 if a>b , * =0 if a=b * - * @param a Firs index to compare - * @param b Second Object to compare + * @param a is the first index to compare + * @param b is the second Object to compare * @return Look at the info */ public int compare(int a, T b); diff --git a/src/zutil/algo/sort/sortable/SortableIntArray.java b/src/zutil/algo/sort/sortable/SortableIntArray.java index b610d20..8b26fdd 100644 --- a/src/zutil/algo/sort/sortable/SortableIntArray.java +++ b/src/zutil/algo/sort/sortable/SortableIntArray.java @@ -7,7 +7,7 @@ public class SortableIntArray implements SortableDataList{ this.list = list; } - public Integer getIndex(int i) { + public Integer get(int i) { return list[i]; } diff --git a/src/zutil/algo/sort/sortable/SortableLinkedList.java b/src/zutil/algo/sort/sortable/SortableLinkedList.java index 30b4ab0..c65f7bf 100644 --- a/src/zutil/algo/sort/sortable/SortableLinkedList.java +++ b/src/zutil/algo/sort/sortable/SortableLinkedList.java @@ -9,7 +9,7 @@ public class SortableLinkedList implements SortableDataList{ this.list = list; } - public T getIndex(int i) { + public T get(int i) { return list.get(i); }