lol
This commit is contained in:
commit
613bef2496
108 changed files with 8397 additions and 0 deletions
68
src/zutil/algo/QuickSelect.java
Normal file
68
src/zutil/algo/QuickSelect.java
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
package zutil.algo;
|
||||
|
||||
import zutil.algo.sort.sortable.SortableDataList;
|
||||
|
||||
/**
|
||||
* This algorithm is a modified QuickSort
|
||||
* to find the k smallest or biggest value
|
||||
* http://en.wikipedia.org/wiki/Selection_algorithm
|
||||
*
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public class QuickSelect {
|
||||
|
||||
public static Object find(SortableDataList list, int k){
|
||||
return find(list, k, 0, list.size()-1);
|
||||
}
|
||||
|
||||
/*
|
||||
function select(list, k, left, right)
|
||||
select a pivot value list[pivotIndex]
|
||||
pivotNewIndex := partition(list, left, right, pivotIndex)
|
||||
if k = pivotNewIndex
|
||||
return list[k]
|
||||
else if k < pivotNewIndex
|
||||
return select(list, k, left, pivotNewIndex-1)
|
||||
else
|
||||
return select(list, k, pivotNewIndex+1, right)
|
||||
*/
|
||||
public static Object find(SortableDataList list, int k, int left, int right){
|
||||
// select a pivot
|
||||
int pivot = right/2;
|
||||
int newPivot = partition(list, left, right, pivot);
|
||||
if(k == newPivot)
|
||||
return list.getIndex(k);
|
||||
else if(k < newPivot)
|
||||
return find(list, k, left, newPivot-1);
|
||||
else
|
||||
return find(list, k, newPivot+1, right);
|
||||
}
|
||||
|
||||
/*
|
||||
function partition(list, left, right, pivotIndex)
|
||||
pivotValue := list[pivotIndex]
|
||||
swap list[pivotIndex] and list[right] // Move pivot to end
|
||||
storeIndex := left
|
||||
for i from left to right-1
|
||||
if list[i] < pivotValue
|
||||
swap list[storeIndex] and list[i]
|
||||
storeIndex := storeIndex + 1
|
||||
swap list[right] and list[storeIndex] // Move pivot to its final place
|
||||
return storeIndex
|
||||
*/
|
||||
private static int partition(SortableDataList list, int left, int right, int pivot){
|
||||
Object pivotValue = list.getIndex(pivot);
|
||||
list.swap(pivot, right);
|
||||
int storeIndex = left;
|
||||
for(int i=left; i<right ;i++){
|
||||
if(list.compare(i, pivotValue) < 0){
|
||||
list.swap(storeIndex, i);
|
||||
storeIndex = storeIndex+1;
|
||||
}
|
||||
}
|
||||
list.swap(right, storeIndex);
|
||||
return storeIndex;
|
||||
}
|
||||
}
|
||||
39
src/zutil/algo/path/DijkstraPathFinder.java
Normal file
39
src/zutil/algo/path/DijkstraPathFinder.java
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package zutil.algo.path;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class DijkstraPathFinder {
|
||||
|
||||
public static LinkedList<PathNode> find(PathNode start, PathNode stop){
|
||||
// TODO
|
||||
/*
|
||||
1
|
||||
|
||||
5 dist[source] := 0 // Distance from source to source
|
||||
6 Q := copy(Graph) // All nodes in the graph are unoptimized - thus are in Q
|
||||
7 while Q is not empty: // The main loop
|
||||
8 u := extract_min(Q) // Remove and return best vertex from nodes in two given nodes
|
||||
// we would use a path finding algorithm on the new graph, such as depth-first search.
|
||||
9 for each neighbor v of u: // where v has not yet been considered
|
||||
10 alt = dist[u] + length(u, v)
|
||||
11 if alt < dist[v] // Relax (u,v)
|
||||
12 dist[v] := alt
|
||||
13 previous[v] := u
|
||||
14 return previous[]
|
||||
*/
|
||||
|
||||
|
||||
|
||||
LinkedList<PathNode> path = new LinkedList<PathNode>();
|
||||
PathNode current = stop;
|
||||
while(true){
|
||||
path.addFirst(current);
|
||||
current = current.getSourceNeighbor();
|
||||
if(current.equals(start)){
|
||||
path.addFirst(start);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
}
|
||||
117
src/zutil/algo/path/DynamicProgramming.java
Normal file
117
src/zutil/algo/path/DynamicProgramming.java
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
package zutil.algo.path;
|
||||
|
||||
public class DynamicProgramming {
|
||||
public static char[][] words = new char[][]{
|
||||
"bibba".toCharArray(),
|
||||
"bitas".toCharArray(),
|
||||
"brott".toCharArray(),
|
||||
"blöja".toCharArray(),
|
||||
"boson".toCharArray()
|
||||
};
|
||||
|
||||
public static void main(String[] args){
|
||||
new DynamicProgramming().search();
|
||||
}
|
||||
/*
|
||||
|
||||
int search(words[][][])
|
||||
matrix[][][] = 0
|
||||
shortest = -1
|
||||
|
||||
for w=0->length(words)
|
||||
for y=0->length(words)
|
||||
for x=0->length(words)
|
||||
// första raden i matrisen
|
||||
if y == 0
|
||||
// finns första bokstaven i rätt position i första ordet?
|
||||
if words[0][x] != words[w][0]
|
||||
matrix[w][y][x] = -1
|
||||
else
|
||||
matrix[w][y][x] = 0
|
||||
else
|
||||
// om föregående är negativ sätt nuvarande till negativ
|
||||
if matrix[w][y-1][x] < 0
|
||||
matrix[w][y-1][x] = -1
|
||||
// här så händer det riktiga i algoritmen
|
||||
else
|
||||
tmp = minstaForskjutning(words[y], words[w][y], x)
|
||||
if tmp >= 0
|
||||
matrix[w][y][x] = matrix[w][y-1][x] + tmp
|
||||
else
|
||||
matrix[w][y][x] = -1
|
||||
// kolla om det är sista raden i matrisen
|
||||
if y == length(matrix)
|
||||
if (tmp < shortest || shortest < 0) && tmp >= 0
|
||||
shortest = tmp;
|
||||
|
||||
return shortest
|
||||
|
||||
int minstaForskjutning(word[], find, index){
|
||||
minsta = -1
|
||||
for i=0->length(word)
|
||||
if word[i] == cfind && (abs(index-i) < minsta || minsta < 0)
|
||||
minsta = abs(index-i)
|
||||
|
||||
return minsta
|
||||
|
||||
*/
|
||||
|
||||
public int search(){
|
||||
int[][][] matrix = new int[words.length][words.length][words.length];
|
||||
int shortest = -1;
|
||||
|
||||
for(int w=0; w<words.length ;w++){ //lodräta ordet
|
||||
System.out.print("\n\n"+new String(words[w])+"\n ");
|
||||
for(int y=0; y<words.length ;y++){ // vågräta ordet
|
||||
System.out.print("\n"+ new String(words[y])+": ");
|
||||
for(int x=0; x<words.length ;x++){ // psition i y
|
||||
// första vågräta ordet
|
||||
if(y == 0){
|
||||
if(words[0][x] != words[w][0]){
|
||||
matrix[w][y][x] = -1;
|
||||
}
|
||||
else{
|
||||
matrix[w][y][x] = 0;
|
||||
}
|
||||
}
|
||||
//resten av de vågräta orden
|
||||
else{
|
||||
if(matrix[w][y-1][x] < 0){
|
||||
matrix[w][y][x] = -1;
|
||||
}
|
||||
else{
|
||||
int tmp = minstaForskjutning(words[y], words[w][y], x);
|
||||
if(tmp >= 0){
|
||||
matrix[w][y][x] = matrix[w][y-1][x] + tmp;
|
||||
}
|
||||
else{
|
||||
matrix[w][y][x] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(y == words.length-1){
|
||||
int tmp = matrix[w][y][x];
|
||||
if((tmp<shortest || shortest<0)
|
||||
&& tmp>= 0){
|
||||
shortest = tmp;
|
||||
}
|
||||
}
|
||||
System.out.print(" "+matrix[w][y][x]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("\n\nKortaste förflyttningen: "+shortest);
|
||||
return shortest;
|
||||
}
|
||||
|
||||
private int minstaForskjutning(char[] word, char cfind, int index){
|
||||
int minsta = -1;
|
||||
for(int i=0; i<word.length ;i++){
|
||||
if(word[i] == cfind && (Math.abs(index-i)<minsta || minsta<0)){
|
||||
minsta = Math.abs(index-i);
|
||||
}
|
||||
}
|
||||
return minsta;
|
||||
}
|
||||
}
|
||||
17
src/zutil/algo/path/PathNode.java
Normal file
17
src/zutil/algo/path/PathNode.java
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package zutil.algo.path;
|
||||
|
||||
|
||||
public interface PathNode {
|
||||
|
||||
public void setVisited(boolean b);
|
||||
|
||||
public boolean visited();
|
||||
|
||||
public Iterable<PathNode> getNeighbors();
|
||||
|
||||
public int getNeighborCost(PathNode neighbor);
|
||||
|
||||
public void setSourceNeighbor(PathNode neighbor);
|
||||
|
||||
public PathNode getSourceNeighbor();
|
||||
}
|
||||
38
src/zutil/algo/path/PathNodeDefault.java
Normal file
38
src/zutil/algo/path/PathNodeDefault.java
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package zutil.algo.path;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class PathNodeDefault implements PathNode{
|
||||
private HashMap<PathNode,Integer> neighbors;
|
||||
private PathNode neighbor;
|
||||
private boolean visited;
|
||||
|
||||
public PathNodeDefault(){
|
||||
neighbors = new HashMap<PathNode,Integer>();
|
||||
visited = false;
|
||||
}
|
||||
|
||||
public void setVisited(boolean b){
|
||||
visited = b;
|
||||
}
|
||||
|
||||
public int getNeighborCost(PathNode neighbor) {
|
||||
return neighbors.get(neighbor);
|
||||
}
|
||||
|
||||
public Iterable<PathNode> getNeighbors() {
|
||||
return neighbors.keySet();
|
||||
}
|
||||
|
||||
public boolean visited() {
|
||||
return visited;
|
||||
}
|
||||
|
||||
public void setSourceNeighbor(PathNode n) {
|
||||
neighbor = n;
|
||||
}
|
||||
|
||||
public PathNode getSourceNeighbor() {
|
||||
return neighbor;
|
||||
}
|
||||
}
|
||||
95
src/zutil/algo/path/SimplePathFinder.java
Normal file
95
src/zutil/algo/path/SimplePathFinder.java
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
package zutil.algo.path;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* A class for simple path finding algorithms
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class SimplePathFinder {
|
||||
|
||||
/**
|
||||
* Returns the first path to the destination
|
||||
*
|
||||
* @param start Start Node
|
||||
* @param stop Stop Node
|
||||
* @return A list with the path
|
||||
*/
|
||||
public LinkedList<PathNode> BreadthFirstSearch(PathNode start, PathNode stop){
|
||||
Queue<PathNode> queue = new LinkedList<PathNode>();
|
||||
|
||||
queue.add(start);
|
||||
start.setVisited(true);
|
||||
|
||||
PathNode tmp;
|
||||
while(!queue.isEmpty()){
|
||||
tmp = queue.poll();
|
||||
|
||||
for(PathNode next : tmp.getNeighbors()){
|
||||
if(!next.visited() && tmp.getNeighborCost(next) > 0){
|
||||
queue.add(next);
|
||||
next.setVisited(true);
|
||||
next.setSourceNeighbor(tmp);
|
||||
|
||||
if(next.equals(stop)){
|
||||
LinkedList<PathNode> path = new LinkedList<PathNode>();
|
||||
for(PathNode current=stop; !current.equals(start) ;current = current.getSourceNeighbor()){
|
||||
path.addFirst(current);
|
||||
}
|
||||
path.addFirst(start);
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new LinkedList<PathNode>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first path to the destination
|
||||
*
|
||||
* @param start Start Node
|
||||
* @param stop Stop Node
|
||||
* @return A list with the path
|
||||
*/
|
||||
public LinkedList<PathNode> DepthFirstSearch(PathNode start, PathNode stop){
|
||||
LinkedList<PathNode> path = new LinkedList<PathNode>();
|
||||
PathNode current = DepthFirstSearchInternal(start, stop);
|
||||
while(current != null){
|
||||
path.addFirst(current);
|
||||
current = current.getSourceNeighbor();
|
||||
if(current.equals(start)){
|
||||
path.addFirst(start);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* The DepthFirstSearch algorithm
|
||||
* @param node The node to search from
|
||||
* @return The stop PathNode if a path was found else null
|
||||
*/
|
||||
private PathNode DepthFirstSearchInternal(PathNode node, PathNode stop){
|
||||
node.setVisited(true);
|
||||
if(node.equals(stop)){
|
||||
return node;
|
||||
}
|
||||
|
||||
for(PathNode next : node.getNeighbors()){
|
||||
if(!next.visited() && node.getNeighborCost(next) > 0){
|
||||
next.setSourceNeighbor(node);
|
||||
PathNode tmp = DepthFirstSearchInternal(next, stop);
|
||||
if(tmp != null){
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
252
src/zutil/algo/sort/ExternalSort.java
Normal file
252
src/zutil/algo/sort/ExternalSort.java
Normal file
|
|
@ -0,0 +1,252 @@
|
|||
package zutil.algo.sort;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* Sort very big files that doesn't fit in ram
|
||||
* Inspiration:
|
||||
* http://www.codeodor.com/index.cfm/2007/5/14/Re-Sorting-really-BIG-files---the-Java-source-code/1208
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class ExternalSort {
|
||||
public static int CHUNK_SIZE = 100000;
|
||||
|
||||
private BufferedReader in;
|
||||
private File sortedFile;
|
||||
|
||||
/**
|
||||
* Creates a ExternalSort object that sort a big file
|
||||
* with minimal use of ram
|
||||
*
|
||||
* @param orgFile File to sort
|
||||
* @param sortedFile The sorted file
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public ExternalSort(File orgFile, File sortedFile) throws FileNotFoundException{
|
||||
in = new BufferedReader(new FileReader(orgFile));
|
||||
this.sortedFile = sortedFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a ExternalSort object that sort a big file
|
||||
* with minimal use of ram
|
||||
*
|
||||
* @param orgFile File to sort
|
||||
* @param sortedFile The sorted file
|
||||
* @param chunk The chunk size
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public ExternalSort(File orgFile, File sortedFile, int chunk) throws FileNotFoundException{
|
||||
in = new BufferedReader(new FileReader(orgFile));
|
||||
this.sortedFile = sortedFile;
|
||||
CHUNK_SIZE = chunk;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the given file
|
||||
*
|
||||
* @throws IOException Some kind of error
|
||||
*/
|
||||
public void sort() throws IOException{
|
||||
// sorting the chunks
|
||||
LinkedList<File> chunkFiles = sortChunks();
|
||||
|
||||
//merging the chunks
|
||||
mergeFiles(chunkFiles);
|
||||
|
||||
//removing the chunks
|
||||
removeFiles(chunkFiles);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges all the files to one
|
||||
* @param files
|
||||
*/
|
||||
private void mergeFiles(LinkedList<File> files){
|
||||
try {
|
||||
BufferedReader[] chunkReader = new BufferedReader[files.size()];
|
||||
String[] rows = new String[files.size()];
|
||||
BufferedWriter out = new BufferedWriter(new FileWriter(sortedFile));
|
||||
|
||||
boolean someFileStillHasRows = false;
|
||||
|
||||
for (int i=0; i<files.size(); i++){
|
||||
chunkReader[i] = new BufferedReader(new FileReader(files.get(i)));
|
||||
|
||||
// get the first row
|
||||
String line = chunkReader[i].readLine();
|
||||
if (line != null){
|
||||
rows[i] = line;
|
||||
someFileStillHasRows = true;
|
||||
}
|
||||
else{
|
||||
rows[i] = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
String row;
|
||||
while (someFileStillHasRows){
|
||||
String min;
|
||||
int minIndex = 0;
|
||||
|
||||
row = rows[0];
|
||||
if (row!=null) {
|
||||
min = row;
|
||||
minIndex = 0;
|
||||
}
|
||||
else {
|
||||
min = null;
|
||||
minIndex = -1;
|
||||
}
|
||||
|
||||
// check which one is minimum
|
||||
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;
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(row!=null){
|
||||
min = row;
|
||||
minIndex = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (minIndex < 0) {
|
||||
someFileStillHasRows = false;
|
||||
}
|
||||
else{
|
||||
// write to the sorted file
|
||||
out.append(rows[minIndex]);
|
||||
out.newLine();
|
||||
|
||||
// get another row from the file that had the min
|
||||
String line = chunkReader[minIndex].readLine();
|
||||
if (line != null){
|
||||
rows[minIndex] = line;
|
||||
}
|
||||
else{
|
||||
rows[minIndex] = null;
|
||||
}
|
||||
}
|
||||
|
||||
// check if one still has rows
|
||||
someFileStillHasRows = false;
|
||||
for(int i=0; i<rows.length ; i++){
|
||||
if(rows[i] != null){
|
||||
if (minIndex < 0){
|
||||
throw(new IOException("Error sorting!!!"));
|
||||
}
|
||||
someFileStillHasRows = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// check the actual files one more time
|
||||
if (!someFileStillHasRows){
|
||||
//write the last one not covered above
|
||||
for(int i=0; i<rows.length ; i++){
|
||||
if (rows[i] == null){
|
||||
String line = chunkReader[i].readLine();
|
||||
if (line != null){
|
||||
someFileStillHasRows=true;
|
||||
rows[i] = line;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// close all the files
|
||||
out.close();
|
||||
for(int i=0; i<chunkReader.length ; i++){
|
||||
chunkReader[i].close();
|
||||
}
|
||||
}
|
||||
catch (Exception ex){
|
||||
ex.printStackTrace();
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the chunk files and returns a LinkedList
|
||||
* with all the files
|
||||
* @return A linkedList with the files
|
||||
* @throws IOException Some kind of error
|
||||
*/
|
||||
private LinkedList<File> sortChunks() throws IOException{
|
||||
LinkedList<File> chunkFiles = new LinkedList<File>();
|
||||
LinkedList<String> chunk = new LinkedList<String>();
|
||||
do{
|
||||
chunk = readChunk(in);
|
||||
|
||||
//QuickSort.sort(new SortableLinkedList(chunk));
|
||||
Collections.sort(chunk);
|
||||
|
||||
File file = new File("extsort"+chunkFiles.size()+".txt");
|
||||
chunkFiles.add(file);
|
||||
writeChunk(chunk,file);
|
||||
}while(!chunk.isEmpty());
|
||||
|
||||
return chunkFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads in a chunk of rows into a LinkedList
|
||||
*
|
||||
* @param list The list to populate
|
||||
* @param in The BufferedReader to read from
|
||||
* @return The LinkeList with the chunk
|
||||
* @throws IOException Some kind of error
|
||||
*/
|
||||
private LinkedList<String> readChunk(BufferedReader in) throws IOException{
|
||||
LinkedList<String> list = new LinkedList<String>();
|
||||
String tmp;
|
||||
for(int i=0; i<CHUNK_SIZE ;i++){
|
||||
tmp = in.readLine();
|
||||
if(tmp == null) break;
|
||||
list.add(tmp);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writs a chunk of strings to a file
|
||||
*
|
||||
* @param list The list to write down
|
||||
* @param file The file to write to
|
||||
* @throws IOException Some kind of error
|
||||
*/
|
||||
private void writeChunk(LinkedList<String> list, File file) throws IOException{
|
||||
BufferedWriter out = new BufferedWriter(new FileWriter(file));
|
||||
Iterator<String> it = list.iterator();
|
||||
while(it.hasNext()){
|
||||
out.write(it.next());
|
||||
out.newLine();
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
|
||||
private void removeFiles(LinkedList<File> list){
|
||||
Iterator<File> it = list.iterator();
|
||||
while(it.hasNext()){
|
||||
it.next().delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
106
src/zutil/algo/sort/QuickSort.java
Normal file
106
src/zutil/algo/sort/QuickSort.java
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
package zutil.algo.sort;
|
||||
|
||||
import zutil.algo.sort.sortable.SortableComparableArray;
|
||||
import zutil.algo.sort.sortable.SortableDataList;
|
||||
|
||||
/**
|
||||
* This class implements QuickSort to sort a array
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class QuickSort{
|
||||
public static final int RANDOM_PIVOT = 0;
|
||||
public static final int MEDIAN_PIVOT = 1;
|
||||
public static final int HALF_PIVOT = 2;
|
||||
|
||||
/**
|
||||
* Sort the elements in ascending order using quicksort.
|
||||
*
|
||||
* @param A A list to sort.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void sort(SortableDataList list){
|
||||
sort(list, 0, list.size()-1, 2, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
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
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void sort(SortableDataList list, int start, int stop, int type, boolean insertionSort){
|
||||
if(stop-start <= 15 && insertionSort){
|
||||
SimpleSort.insertionSort( list, start, stop);
|
||||
}
|
||||
int pivotIndex = pivot(list,start,stop,type);
|
||||
Object pivot = list.getIndex(pivotIndex);
|
||||
int left=start, right=stop;
|
||||
|
||||
do{
|
||||
while(list.compare(left, pivot) < 0){
|
||||
left++;
|
||||
}
|
||||
while(list.compare(right, pivot) > 0){
|
||||
right--;
|
||||
}
|
||||
|
||||
if(left <= right){
|
||||
list.swap(left, right);
|
||||
left++;
|
||||
right--;
|
||||
}
|
||||
}while(left <= right);
|
||||
|
||||
if(start < right){
|
||||
sort(list, start, right, type, insertionSort);
|
||||
}
|
||||
if(left < stop){
|
||||
sort(list, left, stop, type, insertionSort);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static int pivot(SortableDataList list, int start, int stop,int type){
|
||||
switch(type){
|
||||
case 0:
|
||||
return start+(int)(Math.random()*(stop-start));
|
||||
case 1:
|
||||
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)
|
||||
return start;
|
||||
else if(i[i.length/2].compareTo(list.getIndex(stop)) == 0)
|
||||
return stop;
|
||||
else
|
||||
return start+(stop-start)/2;
|
||||
case 2:
|
||||
return (start+stop)/2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
25
src/zutil/algo/sort/Randomizer.java
Normal file
25
src/zutil/algo/sort/Randomizer.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package zutil.algo.sort;
|
||||
|
||||
import zutil.algo.sort.sortable.SortableDataList;
|
||||
|
||||
/**
|
||||
* This class randomizes the index of all the elements in
|
||||
* the Sortable object
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class Randomizer {
|
||||
|
||||
|
||||
/**
|
||||
* Randomizes the index of all the elements
|
||||
* @param list The list
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void sort(SortableDataList list){
|
||||
int size = list.size();
|
||||
for(int i=0; i<size ;i++){
|
||||
list.swap(i, (int)(Math.random()*size));
|
||||
}
|
||||
}
|
||||
}
|
||||
67
src/zutil/algo/sort/SimpleSort.java
Normal file
67
src/zutil/algo/sort/SimpleSort.java
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package zutil.algo.sort;
|
||||
|
||||
import zutil.algo.sort.sortable.SortableDataList;
|
||||
|
||||
/**
|
||||
* A collection of sorting algorithms for arrays of integers.
|
||||
*
|
||||
* @author Ziver Koc
|
||||
* @version 2006-10-31
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
@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]
|
||||
int m = i;
|
||||
for (int j = i + 1; j < n; 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.
|
||||
*
|
||||
* @param A A list to sort.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static SortableDataList insertionSort(SortableDataList list){
|
||||
return insertionSort(list, 0, list.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the elements in ascending order using insertionsort.
|
||||
*
|
||||
* @param A An array of integers.
|
||||
* @param start The index to start from
|
||||
* @param stop 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
42
src/zutil/algo/sort/sortable/SortableArrayList.java
Normal file
42
src/zutil/algo/sort/sortable/SortableArrayList.java
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package zutil.algo.sort.sortable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class SortableArrayList<T> implements SortableDataList<T>{
|
||||
private ArrayList<T> list;
|
||||
|
||||
public SortableArrayList(ArrayList<T> list){
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public T getIndex(int i) {
|
||||
return list.get(i);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return list.size();
|
||||
}
|
||||
|
||||
public void swap(int a, int b) {
|
||||
T temp = list.get(a);
|
||||
list.set(a, list.get(b));
|
||||
list.set(b, temp);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public int compare(int a, int b) {
|
||||
Comparable aa = (Comparable)list.get(a);
|
||||
Comparable bb = (Comparable)list.get(b);
|
||||
return aa.compareTo(bb);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public int compare(int a, T b) {
|
||||
Comparable aa = (Comparable)list.get(a);
|
||||
Comparable bb = (Comparable)b;
|
||||
return aa.compareTo(bb);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
47
src/zutil/algo/sort/sortable/SortableComparableArray.java
Normal file
47
src/zutil/algo/sort/sortable/SortableComparableArray.java
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package zutil.algo.sort.sortable;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class SortableComparableArray implements SortableDataList<Comparable>{
|
||||
private Comparable[] list;
|
||||
|
||||
public SortableComparableArray(Comparable[] list){
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public Comparable getIndex(int i) {
|
||||
return list[i];
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return list.length;
|
||||
}
|
||||
|
||||
public void swap(int a, int b) {
|
||||
Comparable temp = list[a];
|
||||
list[a] = list[b];
|
||||
list[b] = temp;
|
||||
}
|
||||
|
||||
public int compare(int a, int b) {
|
||||
if(list[a].compareTo(list[b]) < 0){
|
||||
return -1;
|
||||
}
|
||||
else if(list[a].compareTo(list[b]) > 0){
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int compare(int a, Comparable b) {
|
||||
if(list[a].compareTo(b) < 0){
|
||||
return -1;
|
||||
}
|
||||
else if(list[a].compareTo(b) > 0){
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
50
src/zutil/algo/sort/sortable/SortableDataList.java
Normal file
50
src/zutil/algo/sort/sortable/SortableDataList.java
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package zutil.algo.sort.sortable;
|
||||
|
||||
public interface SortableDataList<T>{
|
||||
|
||||
/**
|
||||
* Returns a specific index i the list
|
||||
* @param i The index
|
||||
* @return
|
||||
*/
|
||||
public T getIndex(int i);
|
||||
|
||||
/**
|
||||
* Returns 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
|
||||
*/
|
||||
public void swap(int a, int b);
|
||||
|
||||
/**
|
||||
* Compares to indexes and returns:
|
||||
* <0 if a<b ,
|
||||
* >0 if a>b ,
|
||||
* =0 if a=b
|
||||
*
|
||||
* @param a Firs index to compare
|
||||
* @param b Second index to compare
|
||||
* @return Look at the info
|
||||
*/
|
||||
public int compare(int a, int b);
|
||||
|
||||
/**
|
||||
* Compares to indexes and returns:
|
||||
* <0 if a<b ,
|
||||
* >0 if a>b ,
|
||||
* =0 if a=b
|
||||
*
|
||||
* @param a Firs index to compare
|
||||
* @param b Second Object to compare
|
||||
* @return Look at the info
|
||||
*/
|
||||
public int compare(int a, T b);
|
||||
|
||||
}
|
||||
46
src/zutil/algo/sort/sortable/SortableIntArray.java
Normal file
46
src/zutil/algo/sort/sortable/SortableIntArray.java
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package zutil.algo.sort.sortable;
|
||||
|
||||
public class SortableIntArray implements SortableDataList<Integer>{
|
||||
private int[] list;
|
||||
|
||||
public SortableIntArray(int[] list){
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public Integer getIndex(int i) {
|
||||
return list[i];
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return list.length;
|
||||
}
|
||||
|
||||
public void swap(int a, int b) {
|
||||
int temp = list[a];
|
||||
list[a] = list[b];
|
||||
list[b] = temp;
|
||||
}
|
||||
|
||||
public int compare(int a, int b) {
|
||||
if(list[a] < list[b]){
|
||||
return -1;
|
||||
}
|
||||
else if(list[a] > list[b]){
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int compare(int a, Integer b) {
|
||||
if(list[a] < b){
|
||||
return -1;
|
||||
}
|
||||
else if(list[a] > b){
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
42
src/zutil/algo/sort/sortable/SortableLinkedList.java
Normal file
42
src/zutil/algo/sort/sortable/SortableLinkedList.java
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package zutil.algo.sort.sortable;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class SortableLinkedList<T> implements SortableDataList<T>{
|
||||
private LinkedList<T> list;
|
||||
|
||||
public SortableLinkedList(LinkedList<T> list){
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public T getIndex(int i) {
|
||||
return list.get(i);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return list.size();
|
||||
}
|
||||
|
||||
public void swap(int a, int b) {
|
||||
T temp = list.get(a);
|
||||
list.set(a, list.get(b));
|
||||
list.set(b, temp);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public int compare(int a, int b) {
|
||||
Comparable aa = (Comparable)list.get(a);
|
||||
Comparable bb = (Comparable)list.get(b);
|
||||
return aa.compareTo(bb);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public int compare(int a, T b) {
|
||||
Comparable aa = (Comparable)list.get(a);
|
||||
Comparable bb = (Comparable)b;
|
||||
return aa.compareTo(bb);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue