Fixed som mistake with bubble sort and insertion sort
This commit is contained in:
parent
49bb25a4d7
commit
0f85e1b4dd
9 changed files with 115 additions and 87 deletions
|
|
@ -114,7 +114,6 @@ public class ExternalSort {
|
|||
for(int i=1; i<rows.length ;i++){
|
||||
row = rows[i];
|
||||
if (min!=null) {
|
||||
|
||||
if(row!=null && row.compareTo(min) < 0){
|
||||
minIndex = i;
|
||||
min = row;
|
||||
|
|
|
|||
|
|
@ -11,41 +11,37 @@ import zutil.algo.sort.sortable.SortableDataList;
|
|||
public class QuickSort{
|
||||
public static final int RANDOM_PIVOT = 0;
|
||||
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){
|
||||
sort(list, 0, list.size()-1, 2, true);
|
||||
public static void sort(SortableDataList<?> 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){
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import zutil.algo.sort.sortable.SortableDataList;
|
|||
*/
|
||||
public class Randomizer {
|
||||
|
||||
|
||||
/**
|
||||
* Randomizes the index of all the elements
|
||||
* @param list The list
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
* Sort the elements in ascending order using bubble sort
|
||||
* witch is the slowest of the algorithms.
|
||||
* Complexity: O(n^2).
|
||||
*
|
||||
* @param list A list to sort.
|
||||
* @return The same array sorted in ascending order.
|
||||
* @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<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;
|
||||
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; i<stop ;i++){
|
||||
for(int j=i; j>start ;j--){
|
||||
if(list.compare(j, j-1) < 0){
|
||||
list.swap(j, j-1);
|
||||
public static void insertionSort(SortableDataList<?> list, int start, int stop){
|
||||
for(int i=start; i<stop ;++i){
|
||||
for(int j=i; start<j && list.compare(j-1, j)>0 ;--j){
|
||||
list.swap(j-1, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@ public class SortableArrayList<T> implements SortableDataList<T>{
|
|||
this.list = list;
|
||||
}
|
||||
|
||||
public T getIndex(int i) {
|
||||
public T get(int i) {
|
||||
return list.get(i);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ public class SortableComparableArray implements SortableDataList<Comparable>{
|
|||
this.list = list;
|
||||
}
|
||||
|
||||
public Comparable getIndex(int i) {
|
||||
public Comparable get(int i) {
|
||||
return list[i];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,23 +3,24 @@ package zutil.algo.sort.sortable;
|
|||
public interface SortableDataList<T>{
|
||||
|
||||
/**
|
||||
* 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<T>{
|
|||
* >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<T>{
|
|||
* >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);
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ public class SortableIntArray implements SortableDataList<Integer>{
|
|||
this.list = list;
|
||||
}
|
||||
|
||||
public Integer getIndex(int i) {
|
||||
public Integer get(int i) {
|
||||
return list[i];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ public class SortableLinkedList<T> implements SortableDataList<T>{
|
|||
this.list = list;
|
||||
}
|
||||
|
||||
public T getIndex(int i) {
|
||||
public T get(int i) {
|
||||
return list.get(i);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue