Fixed som mistake with bubble sort and insertion sort

This commit is contained in:
Ziver Koc 2010-02-25 22:18:01 +00:00
parent 49bb25a4d7
commit 0f85e1b4dd
9 changed files with 115 additions and 87 deletions

View file

@ -114,7 +114,6 @@ public class ExternalSort {
for(int i=1; i<rows.length ;i++){ for(int i=1; i<rows.length ;i++){
row = rows[i]; row = rows[i];
if (min!=null) { if (min!=null) {
if(row!=null && row.compareTo(min) < 0){ if(row!=null && row.compareTo(min) < 0){
minIndex = i; minIndex = i;
min = row; min = row;

View file

@ -11,41 +11,37 @@ import zutil.algo.sort.sortable.SortableDataList;
public class QuickSort{ public class QuickSort{
public static final int RANDOM_PIVOT = 0; public static final int RANDOM_PIVOT = 0;
public static final int MEDIAN_PIVOT = 1; public static final int MEDIAN_PIVOT = 1;
public static final int HALF_PIVOT = 2; public static final int MIDDLE_PIVOT = 2;
/** /**
* Sort the elements in ascending order using quicksort. * Sort the elements in ascending order using Quicksort.
* *
* @param A A list to sort. * @param A is the list to sort.
*/ */
@SuppressWarnings("unchecked") public static void sort(SortableDataList<?> list){
public static void sort(SortableDataList list){ sort(list, 0, list.size()-1, MIDDLE_PIVOT, true);
sort(list, 0, list.size()-1, 2, true);
} }
/** /**
* Sort the elements in ascending order using quicksort. * Sort the elements in ascending order using Quicksort.
* *
* @param A A list to sort. * @param A is the list to sort.
* @param type type of pivot * @param type is the type of pivot
* @param insert to use insertion sort when needed * @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(list, 0, list.size()-1, type, insert);
} }
/** /**
* Sort the elements in ascending order using qicksort. * Sort the elements in ascending order using Quicksort.
* after the 10 th re write and a bad mood i found this * Reference: http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/quick/quicken.htm
* site that gave me much help: * Complexity: O(n*log n) normally, but O(n^2) if the pivot is bad
* http://www.inf.fh-flensburg.de/lang/algorithmen/
* sortieren/quick/quicken.htm
* *
* @param A A list to sort. * @param A is the list to sort.
* @param start The index to start from * @param start is the index to start from
* @param stop The index to stop * @param stop is the index to stop
* @param type The type of pivot to use * @param type is the type of pivot to use
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void sort(SortableDataList list, int start, int stop, int type, boolean insertionSort){ 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); SimpleSort.insertionSort( list, start, stop);
} }
int pivotIndex = pivot(list,start,stop,type); int pivotIndex = pivot(list,start,stop,type);
Object pivot = list.getIndex(pivotIndex); Object pivot = list.get(pivotIndex);
int left=start, right=stop; int left=start, right=stop;
do{ do{
@ -80,25 +76,24 @@ public class QuickSort{
} }
@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") private static int pivot(SortableDataList<?> list, int start, int stop,int type){
private static int pivot(SortableDataList list, int start, int stop,int type){
switch(type){ switch(type){
case 0: case RANDOM_PIVOT:
return start+(int)(Math.random()*(stop-start)); return start+(int)(Math.random()*(stop-start));
case 1: case MEDIAN_PIVOT:
Comparable[] i = new Comparable[]{ Comparable[] i = new Comparable[]{
(Comparable)list.getIndex(0), (Comparable)list.get(0),
(Comparable)list.getIndex(list.size()/2), (Comparable)list.get(list.size()/2),
(Comparable)list.getIndex(list.size()-1)}; (Comparable)list.get(list.size()-1)};
SimpleSort.insertionSort(new SortableComparableArray(i),0,i.length); SimpleSort.insertionSort(new SortableComparableArray(i));
if(i[i.length/2].compareTo(list.getIndex(start)) == 0) if(i[i.length/2].compareTo(list.get(start)) == 0)
return start; 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; return stop;
else else
return start+(stop-start)/2; return start+(stop-start)/2;
case 2: case MIDDLE_PIVOT:
return (start+stop)/2; return (start+stop)/2;
} }
return 0; return 0;

View file

@ -9,7 +9,6 @@ import zutil.algo.sort.sortable.SortableDataList;
* @author Ziver * @author Ziver
*/ */
public class Randomizer { public class Randomizer {
/** /**
* Randomizes the index of all the elements * Randomizes the index of all the elements

View file

@ -3,7 +3,7 @@ package zutil.algo.sort;
import zutil.algo.sort.sortable.SortableDataList; 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 * @author Ziver Koc
* @version 2006-10-31 * @version 2006-10-31
@ -11,57 +11,91 @@ import zutil.algo.sort.sortable.SortableDataList;
public class SimpleSort{ public class SimpleSort{
/** /**
* Sort the elements in ascending order using selection sort. * Sort the elements in ascending order using bubble sort
* This algorithm has time complexity Theta(n*n), where n is * witch is the slowest of the algorithms.
* the length of the array. * Complexity: O(n^2).
* *
* @param list A list to sort. * @param A is the list to sort.
* @return The same array sorted in ascending order.
*/ */
@SuppressWarnings("unchecked") public static void bubbleSort(SortableDataList<?> list){
public static SortableDataList selectionSort(SortableDataList list){ bubbleSort(list, 0, list.size());
int n = list.size(); }
for (int i = 0; i < n - 1; i++) { /**
// find index m of min element in v[i..n-1] * 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<stop ;++i){
for(int j=stop-2; i<=j ;--j){
if(list.compare(j, j+1) > 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; 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) if (list.compare(j, m) < 0)
m = j; m = j;
} }
// swap v[i] and v[m] // swap v[i] and v[m]
list.swap(i, 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 void insertionSort(SortableDataList<?> list){
public static SortableDataList insertionSort(SortableDataList list){ insertionSort(list, 0, list.size());
return 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 A is an array of integers.
* @param start The index to start from * @param start is the index to start from
* @param stop The index to stop * @param stop is the index to stop
*/ */
@SuppressWarnings("unchecked") public static void insertionSort(SortableDataList<?> list, int start, int stop){
public static SortableDataList insertionSort(SortableDataList list, int start, int stop){ for(int i=start; i<stop ;++i){
for(int i=start; i<stop ;i++){ for(int j=i; start<j && list.compare(j-1, j)>0 ;--j){
for(int j=i; j>start ;j--){ list.swap(j-1, j);
if(list.compare(j, j-1) < 0){ }
list.swap(j, j-1); }
}
}
}
return list;
} }
} }

View file

@ -9,7 +9,7 @@ public class SortableArrayList<T> implements SortableDataList<T>{
this.list = list; this.list = list;
} }
public T getIndex(int i) { public T get(int i) {
return list.get(i); return list.get(i);
} }

View file

@ -8,7 +8,7 @@ public class SortableComparableArray implements SortableDataList<Comparable>{
this.list = list; this.list = list;
} }
public Comparable getIndex(int i) { public Comparable get(int i) {
return list[i]; return list[i];
} }

View file

@ -3,23 +3,24 @@ package zutil.algo.sort.sortable;
public interface SortableDataList<T>{ public interface SortableDataList<T>{
/** /**
* Returns a specific index i the list * Returns a is a specific index i the list
* @param i The index * @param i is the index
* @return * @return
*/ */
public T getIndex(int i); public T get(int i);
/** /**
* Returns the size of the list * Returns the size of the list
* *
* @return The size of the list * @return the size of the list
*/ */
public int size(); public int size();
/** /**
* Swaps the given indexes * 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); public void swap(int a, int b);
@ -29,8 +30,8 @@ public interface SortableDataList<T>{
* >0 if a>b , * >0 if a>b ,
* =0 if a=b * =0 if a=b
* *
* @param a Firs index to compare * @param a is the first index to compare
* @param b Second index to compare * @param b is the second index to compare
* @return Look at the info * @return Look at the info
*/ */
public int compare(int a, int b); public int compare(int a, int b);
@ -41,8 +42,8 @@ public interface SortableDataList<T>{
* >0 if a>b , * >0 if a>b ,
* =0 if a=b * =0 if a=b
* *
* @param a Firs index to compare * @param a is the first index to compare
* @param b Second Object to compare * @param b is the second Object to compare
* @return Look at the info * @return Look at the info
*/ */
public int compare(int a, T b); public int compare(int a, T b);

View file

@ -7,7 +7,7 @@ public class SortableIntArray implements SortableDataList<Integer>{
this.list = list; this.list = list;
} }
public Integer getIndex(int i) { public Integer get(int i) {
return list[i]; return list[i];
} }

View file

@ -9,7 +9,7 @@ public class SortableLinkedList<T> implements SortableDataList<T>{
this.list = list; this.list = list;
} }
public T getIndex(int i) { public T get(int i) {
return list.get(i); return list.get(i);
} }