Damit sollte es in meinen Augen nichts zu tun haben.
Durchlauf 0
Durchlauf 1
Durchlauf 2
Durchlauf 3
Durchlauf 4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at bubblechen.main(bubblechen.java:35)
4
4+1=5
-1
import java.util.*;
public class bubblechen {
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
int bubbleman[]= {12, 16, 36, 24, 56};
// int bubbleman[] = new int[5];
int zwischenspeicher;
boolean wechsel;
/*Random zufallsGenerator = new Random();
int zufall;
for (int i = 0; i<bubbleman.length; i++) // Zufallszahlen
{
bubbleman[i] = 2;
}*/
do // Sortieren
{
wechsel = false;
System.out.println("Test");
for (int i = 0; i<bubbleman.length-1; i++)
{
System.out.println("Durchlauf "+i);
if (bubbleman[i+1] < bubbleman[i])
{
zwischenspeicher = bubbleman[i];
bubbleman[i] = bubbleman[i+1];
bubbleman[i+1] = zwischenspeicher;
wechsel = true;
} // if
} // for Ende
}while(wechsel);
System.out.println("Sortiert:");
for (int x = 0; x<bubbleman.length; x++)
{
System.out.println(bubbleman[x]);
}// for
}
}
Das habe ich nun gemacht... gleiche Ausgabe ...
Habe mir den Thread hier nicht komplett durchgelesen, aber gibt es nicht, wie in C eine Funktion, die das macht?
import java.util.*;
public class bubblechen {
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
int bubbleman[]= {12, 16, 36, 24, 56};
// int bubbleman[] = new int[5];
int zwischenspeicher;
boolean wechsel;
/*Random zufallsGenerator = new Random();
int zufall;
for (int i = 0; i<bubbleman.length; i++) // Zufallszahlen
{
bubbleman[i] = 2;
}*/
do // Sortieren
{
wechsel = false;
System.out.println("Test");
for (int i = 0; i<bubbleman.length-1; i++)
{
// System.out.println("Durchlauf "+i);
if (bubbleman[i+1] < bubbleman[i])
{
zwischenspeicher = bubbleman[i];
bubbleman[i] = bubbleman[i+1];
bubbleman[i+1] = zwischenspeicher;
wechsel = true;
} // if
else
{
wechsel = false;
}
} // for Ende
}while(wechsel);
for (int x = 0; x<bubbleman.length; x++)
{
System.out.println(bubbleman[x]);
}// for
System.out.println("Sortiert:");
}
}
import java.util.*;
public class bubblechen {
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
int bubbleman[]= {58, 16, 36, 24, 56};
// int bubbleman[] = new int[5];
int zwischenspeicher;
boolean wechsel;
/*Random zufallsGenerator = new Random();
int zufall;
for (int i = 0; i<bubbleman.length; i++) // Zufallszahlen
{
bubbleman[i] = 2;
}*/
do // Sortieren
{
wechsel = false;
System.out.println("Test");
for (int i = 0; i<bubbleman.length-1; i++)
{
// System.out.println("Durchlauf "+i);
if (bubbleman[i+1] < bubbleman[i])
{
System.out.println("Durchlauf"+i);
zwischenspeicher = bubbleman[i];
bubbleman[i] = bubbleman[i+1];
bubbleman[i+1] = zwischenspeicher;
wechsel = true;
} // if
} // for Ende
System.out.println("EndeWhile");
}while(wechsel);
System.out.println("Nach der While");
for (int x = 0; x<bubbleman.length; x++)
{
System.out.println(bubbleman[x]);
}// for
System.out.println("Sortiert:");
}
}
Durchlauf1
Durchlauf2
Durchlauf3
EndeWhile
import java.util.*;
public class neuebubble {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] bubblegirl = {34, 32, 12, 4, 5};
int temp;
boolean getauscht;
int test=0;
// Durchläufe
do
{
// vor dem Durchlauf wurde noch nicht getauscht
getauscht = false;
for (int i = 0; i<bubblegirl.length-1;i++)
{
if (bubblegirl[i+1]<bubblegirl[i])
{
test++;
// Tauschvorgang
temp = bubblegirl[i];
bubblegirl[i] = bubblegirl[i+1];
bubblegirl[i]=temp;
// setze getauscht auf true
getauscht = true;
}
}
for (int i =0; i<bubblegirl.length;i++)
{
System.out.println(bubblegirl[i]);
}
}while(test<bubblegirl.length);
System.out.println("Fertig");
}
}
Kompilierst du nie nach der Änderung neu? :bahnhof:
Der von dir gepostete Code funktioniert
public class Bubblesort {
private static void sortiereArray(int[] nr){
for (int letzter = nr.length-1; letzter > 1; letzter--){
for(int aktuell = 0; aktuell < letzter; aktuell++){
if(nr[aktuell] > nr[aktuell + 1]){
int temp = nr[aktuell];
nr[aktuell] = nr [aktuell+1];
nr[aktuell+1] = temp;
}
}
}
}
public static void main(String ... args) {
int[] array = new int[] {1,3,5,34,5,76,5,4,56,4,43,3,4,5,56,4,32,22,1};
sortiereArray(array);
System.out.println(java.util.Arrays.toString(array));
}
}
public class BubbleSort {
public void sortiere (int [] x){
boolean unsortiert = true;
int temp;
while (unsortiert)
unsortiert = false;
for (int i = 0; i < x.length-1; i++);
if (x[i+1] < x[i])
{
temp = x[i];
x[i] = x[i+1];
x[i+1] = temp;
unsortiert = true;
}
}
public static void main (String [] args)
{
int [] zahlen ={10, 14, 2, 3};
sortiere (zahlen);
System.out.println(zahlen[i]);
}
}
[code=Java]
Nichts gegen Java...
Aber ich sehne mich nach meinem Visual Studio für C#