2 Dimensionaler Bubblesort

Big_Reddy

Mitglied
Hi,
ich habe die Aufgabe ein mit Randomzahlen gefülltes Array gefülltes 2-D Array zu sortieren, ohne array.sort zu nutzen.
Ich habe Bubblesort gewählt, das es relativ simpel ist. Jedoch arbeitet es nicht so, wie es soll:
Es hängt sich an der do-Schleife auf und durchläuft diese unendlich, da das IF immer angesprochen wird.
Die Funktionsweise sollte so aussehen, dass 'zahlen [y] [x]' immer eine Stelle hinter 'zahlen [j] ' ist.
Wenn 'zahlen [y] [x]' größer ist als 'zahlen [j] ' dann sollten die Variablen getauscht werden und 'test' auf true gesetzt werden. Sollange das IF angesprochen wird läuft die do-Schleife.

Danke in Vorraus für jede Hilfe.


Sort:

Java:
 do { 
      test = false;
      for (i = 0; i < 5; i++) {
        for (j = 0; j < 3; j++) {
          if (zahlen [j] [i] < zahlen [y] [x] && zahlen [j] [i] != zahlen [y] [x]) {
            temp = zahlen [j] [i];
            zahlen [j] [i] = zahlen [y] [x];
            zahlen [y] [x] = temp;
            test = true;
          } // end of if
          y = j;
        } // end of for
        x = i;
      } // end of for
    } while (test = true);


Ganzer Quellcode:

Java:
import java.util.*;
public class RandomArray
{
  public static void main(String h[])
  {
    Scanner sc = new Scanner(System.in);
    int zahlen [] [] = new int [3] [5];
    int temp;
    int i = 0;
    int j = 0; 
    int x = 0;
    int y = 0;   
    boolean test = false;
    
    for (i = 0; i < 5; i++) {
      for (j = 0; j < 3; j++) {
        zahlen [j] [i] = ((int)(Math.random() * 100));
      } // end of for
    } // end of for
    for (i = 0; i < 5; i++) {
      for (j = 0; j < 3; j++) {
        System.out.print(zahlen [j] [i]+"\t");
      } // end of for 
      System.out.println("");
    } // end of for
    System.out.println("");
    
    do { 
      test = false;
      for (i = 0; i < 5; i++) {
        for (j = 0; j < 3; j++) {
          if (zahlen [j] [i] < zahlen [y] [x] && zahlen [j] [i] != zahlen [y] [x]) {
            temp = zahlen [j] [i];
            zahlen [j] [i] = zahlen [y] [x];
            zahlen [y] [x] = temp;
            test = true;
          } // end of if
          y = j;
        } // end of for
        x = i;
      } // end of for
    } while (test = true);
    
    for (i = 0; i < 5; i++) {
      for (j = 0; j < 3; j++) {
        System.out.print(zahlen [j] [i]+"\t");
      } // end of for 
      System.out.println("");
    } // end of for
    System.out.println("");   
  }
}
 

Big_Reddy

Mitglied
Ich habe es jetzt mit einem Selection Sort versucht

Selection Sort:

Java:
do { 
      min = Integer.MAX_VALUE;
      for (i = 0; i < 5; i++) {
        for (j = 0; j < 3; j++) {
          if (zahlen [j] [i] < min) {
            iSpeicher = i;
            jSpeicher = j;
            min = zahlen [j] [i];
          } // end of if
        } // end of for
      } // end of for
      sort [y] [x] = zahlen [jSpeicher] [iSpeicher];
      zahlen [jSpeicher] [iSpeicher] = Integer.MAX_VALUE;
      if (y < 3) {
        y++;
      } // end of if
      else if (y == 3) {
        y = 0;
        x++;
      } // end of if-else
      else if (x == 5) {
        test = true;
      } // end of if-else
    } while (test = false);

Ganzer Quellcode²:

Java:
import java.util.*;
public class RandomArray
{
  public static void main(String h[])
  {
    Scanner sc = new Scanner(System.in);
    int zahlen [] [] = new int [3] [5];
    int sort [] [] = new int [3] [5];
    int temp;
    int i = 0;
    int j = 0; 
    int x = 0;
    int y = 0;
    int iSpeicher = 0;
    int jSpeicher = 0;
    int min;
    int max;
    boolean test = false;
    
    for (i = 0; i < 5; i++) {
      for (j = 0; j < 3; j++) {
        zahlen [j] [i] = ((int)(Math.random() * 100));
      } // end of for
    } // end of for
    for (i = 0; i < 5; i++) {
      for (j = 0; j < 3; j++) {
        System.out.print(zahlen [j] [i]+"\t");
      } // end of for 
      System.out.println("");
    } // end of for
    System.out.println("");
    
    
    do { 
      min = Integer.MAX_VALUE;
      for (i = 0; i < 5; i++) {
        for (j = 0; j < 3; j++) {
          if (zahlen [j] [i] < min) {
            iSpeicher = i;
            jSpeicher = j;
            min = zahlen [j] [i];
          } // end of if
        } // end of for
      } // end of for
      sort [y] [x] = zahlen [jSpeicher] [iSpeicher];
      zahlen [jSpeicher] [iSpeicher] = Integer.MAX_VALUE;
      if (y < 3) {
        y++;
      } // end of if
      else if (y == 3) {
        y = 0;
        x++;
      } // end of if-else
      else if (x == 5) {
        test = true;
      } // end of if-else
    } while (test = false);
    
    for (i = 0; i < 5; i++) {
      for (j = 0; j < 3; j++) {
        System.out.print(sort [j] [i]+"\t");
      } // end of for 
      System.out.println("");
    } // end of for
    System.out.println("");
    for (i = 0; i < 5; i++) {
      for (j = 0; j < 3; j++) {
        System.out.print(zahlen [j] [i]+"\t");
      } // end of for 
      System.out.println("");
    } // end of for
  }
}
 

Ähnliche Java Themen

Neue Themen


Oben