-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubblesort(int[])
84 lines (73 loc) · 2.2 KB
/
Bubblesort(int[])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
public class Bubblesort
{
public static int[] ZufallsListe(int n){
int[] liste = new int[n];
int x;
for(int i=0; i<n;i++){
x = (int) Math.floor(n*Math.random()+1);
liste[i] = x;
}
return liste;
}
public static int[] Copy(int[] liste) {
int[] cliste = new int[liste.length];
for(int n=0;n<liste.length;n++){
cliste[n] = liste[n];
}
return cliste;
}
public static boolean IstSortiert(int[] liste) {
int n=0;
for(int i =0;i<liste.length-1;i++){
if(liste[i]<liste[i+1]){
n++;
}
}
if(n==liste.length-1){return true;}
else {return false;}
}
public static void Tauschen(int[] zahlenreihe, int a , int b)
{
/*int[] neueliste = new int[zahlenreihe.length];
for (int i=0;i<zahlenreihe.length;i=i+1) {
neueliste[i] = zahlenreihe[i];
}
neueliste[a] = zahlenreihe[b];
neueliste[b] = zahlenreihe[a];
*/
int temp = zahlenreihe[a];
zahlenreihe[a] = zahlenreihe[b];
zahlenreihe[b] = temp;
}
public static void SortierenT(int[] liste) {
for(int i=1;i<liste.length;i++){
//if(IstSortiert(liste)==false){
for(int n=0;n<liste.length-i;n++){
if(liste[n]>liste[n+1]){
Tauschen(liste,n,n+1);
}
}
//}
}
}
public static void Ausgabe(int[] liste) {
System.out.print("{");
for (int i =0; i<liste.length-1;i++) {
System.out.print(liste[i]+", ");
}
System.out.println(liste[liste.length-1]+"}");
}
public static void main(String[]args){
System.out.println("Listenlänge: | Zeit(ms):");
for(int n=0;n<50001;n=n+1000){
int[] liste = ZufallsListe(n);
int [] liste2 = Copy(liste);
long start = System.currentTimeMillis();
SortierenT(liste);
long ende = System.currentTimeMillis();
System.out.println(n+" | "+(ende-start));
}
//Ausgabe(liste);
//Ausgabe(sortierteliste);
}
}