rekursi-sortieren

Status
Nicht offen für weitere Antworten.
G

Guest

Gast
Hallo zusammen!

Ich bins mal wieder. Bin gerade daran, wie der Titel dieses Thema schon verlauten lässt, eine Rekursiv-Sortierung vorzunehmen. Das Element a[0] sollte grösser sein als das Element a[1] etc. Ich weiss nicht, ob ich es ganz richtig gemacht habe, ich füge den Code mal hinzu (bei void sort (int i) ), denn beim Output kommt dann etliche Male rot angestrichen, dass es einen Fehler gäbe. Ich hab bei void sort übrigens zweilmal die"if" verwendet, da es bei "else" komischerweise immer einen Fehler feststellt... Kann mir da jemand weiterhelfen?

lg

Exception in thread "main" java.lang.StackOverflowError
at array.SortArray.sort(SortArray.java:77)
at array.SortArray.sort(SortArray.java:77)
at array.SortArray.sort(SortArray.java:77)
at array.SortArray.sort(SortArray.java:77) etc.

lg

Code:
package array;

import java.util.Random;
import java.util.*;
/**
* Informatik II - SS2007 

* Uebungsserie 2, Aufgabe 1 

* Template for class SortArray.java 

*
* @author Silvia Santini
*/

public class SortArray {
  // int-Array (15 elements)
  int[] a = new int[15];
  int myarray;
  int length() {
    return 0;
}
  /**
   * insert comments here
   */
 void initialize() {

      for(int i=0; i<a.length; i++)
         { 
         int myarray = a[i];
          Random f = new Random();
          int z = Math.abs(f.nextInt(1000));
         
          a[i]=z;
          myarray=z;
        // System.out.println(a[i]);
          
     };
      

    System.out.println("Implement here: initialization");
 }

  /**
   * insert comments here
   */
  void print() {
        
     for(int i=0; i<a.length;i++) {
         int arr = a[i];
        // System.out.println(arr);
          
        
          
     }
    
          
    
          
     };
        
     
  
  
  

  /**
   * insert comments here
   */
  void sort(int i) {
      i = 0;
      int j = i+1;
      if( a[i] <= a[j]);
      {
      int temp = j;
       j = i;
       i = temp;
      };
       if(a[i] >= a[j]);
       {sort(i+1);
       }
       
      
     
      
      
    /*  
      while( a[i] > a[i+1])
      {int b=a[i];
       a[i]=a[i+1];
       a[i+1] =b;}
      }
  
    */  
      /*while (int j>0 && curElement < a[j-1]) {
            a[j]=a[j-1];  //move elements forward
            j = j-1;
      System.out.println("Implement here: sorting method");
   */
  
  }
  /**
   * insert comments here
   */
  public static void main(String[] args) {
    
    
      
      // An instance of the class SortArray need to be created
      // to access class' Methods
      SortArray s = new SortArray();

      // Call to method methodName with s.<methodName>
      // Initialize:
      s.initialize();

      System.out.println("Array not sorted:");
      // Output, not sorted:
      s.print();
   
      
      
      // Sorting (Which parameter as input?)
      s.sort(9999);

      System.out.println("Array sorted:");
      // Output, sorted:
     s.print();
   }
 }
 
S

SlaterB

Gast
> int temp = j;
> j = i;
> i = temp;

du vertauschst dort nur die Indexe, willst du nicht eher die Array-Elemente vertauschen?

wieso übergibst du eigentlich sort einen Parameter wenn du diesen immer auf 0 setzt?

und generell die Frage: was ist dein Plan, was soll sort machen,
warum rufst du es rekursiv auf?
erst denken, dann programmieren
 
G

Guest

Gast
Ja, stimmt sollten Elemente sein. Das Programm funtioniert jetzt, danke:D.

lg
 
Status
Nicht offen für weitere Antworten.

Oben