Skip to content

Instantly share code, notes, and snippets.

@Feh
Created June 23, 2011 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Feh/1043477 to your computer and use it in GitHub Desktop.
Save Feh/1043477 to your computer and use it in GitHub Desktop.
Radixsort
import java.util.Queue;
import java.util.LinkedList;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
public class R {
public static void main(String[] args) {
int len = 10;
int[] v = new int[len];
// Array von Queues initialisieren
@SuppressWarnings({"unchecked"})
Queue<Integer>[] b = new Queue[10];
for(int i=0; i<10; i++)
b[i] = new LinkedList<Integer>();
// Tmpfile erstellen
File tempFile = null;
BufferedWriter out = null;
try {
tempFile = File.createTempFile("sortiert", ".tmp.txt" );
out = new BufferedWriter(new FileWriter(tempFile));
} catch (IOException ex) { }
System.out.println("Problem:");
for(int i=0; i<v.length; i++) {
v[i] = (int) (Math.random() * Math.pow(10, (int)(5+(Math.random()*10)%6)));
// Fixed length
// v[i] = (int) (Math.random() * Math.pow(10, 9));
System.out.printf("%" + Integer.toString(len) + "d\n", v[i]);
}
for(int i=0; i<len; i++) {
for(int j=0; j<v.length; j++)
b[(v[j] / (int)Math.pow(10,i)) % 10].offer(v[j]);
int ind = 0;
for(int j=0; j<10; j++)
while(! b[j].isEmpty())
v[ind++] = b[j].poll();
}
System.out.println("\nLösung:");
for(int i=0; i<v.length; i++) {
System.out.printf("%" + Integer.toString(len) + "d\n", v[i]);
if(tempFile != null)
try {
out.write(Integer.toString(v[i]) + "\n");
} catch (IOException ex) { }
}
if(tempFile != null) {
System.out.printf("\nErgebnis liegt in %s\n", tempFile.getAbsolutePath());
try {
out.close();
} catch (IOException ex) { }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment