Created
January 12, 2012 13:03
-
-
Save 19h/1600387 to your computer and use it in GitHub Desktop.
BubbleShit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
//import java.util.Integer; | |
public class Sortierverfahren { | |
/** | |
* @param args | |
*/ | |
public static int[] genIntArray(int length) { | |
int[] a = new int[length]; | |
//Random generator = new Random(); | |
int i = 0; | |
for (i = 0; i!= length; i++) { | |
a[i] = (i); | |
} | |
return a; | |
} | |
public static void PrintIntArray(String name, int length) { | |
Random generator = new Random(); | |
int i = 0; | |
System.out.print("int[] " + name + " = {"); | |
for (i = 1; i!= length+1; i++) { | |
System.out.print(generator.nextInt(length)); | |
if (i < length) | |
System.out.print(", "); | |
if (i % 15 == 0) | |
System.out.print("\n"); | |
} | |
System.out.print("};\n\n"); | |
} | |
public static int[] bubble(int[] x) { | |
int i = 0; | |
boolean fertig = false; | |
boolean sorted = false; | |
while (!fertig) { | |
sorted = false; | |
for (i = 1; i<x.length; ++i) { | |
if (x[i] < x[i-1]) { | |
int temp = x[i-1]; x[i-1] = x[i]; x[i] = temp; sorted = true; | |
} | |
} | |
if (!sorted) { | |
fertig = true; | |
} | |
} | |
return x; | |
} | |
public static void printArray(int[] x) { | |
int i = 0; | |
for (i=0; i< x.length; i++) { | |
System.out.print(x[i]); | |
if (i+1< x.length) { | |
System.out.print(", "); | |
} | |
} | |
System.out.println(); | |
} | |
public static void main(String[] args) { | |
System.out.print("Array generated: \n"); | |
//PrintIntArray("a", 15); | |
int count = Integer.parseInt(args[0]); | |
System.out.println("Anzahl: " + count); | |
int[] a = genIntArray(count); | |
//PrintIntArray("test", count); | |
long tick1, tick2; | |
//Zeitmessung beginnen | |
System.out.println("old unsorted array: "); | |
//printArray(a); | |
//start bubble sort | |
int[] x = bubble(a); | |
tick1 = System.currentTimeMillis(); | |
x = bubble(x); | |
//Zeitmessung beenden | |
tick2 = System.currentTimeMillis(); | |
System.out.println("new sorted array: "); | |
//printArray(x); | |
System.out.println("\nZeit benötigt: " + ((tick2 - tick1)) + " ms"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment