Selection Sort

Fragen zu Algorithmen und Datenstrukturen in dieses Forum.

Selection Sort

Beitragvon keyes » Fr 14. Aug 2009, 16:46

Hey Leute,

ich hab zu Übungszwecken für meine Klausur mal ein kleines Programm in Java zum Algorithmus SelectionSort geschrieben. Falls ihr eine bessere Idee habt nur her damit ;):

Code: Alles auswählen
import java.lang.*;

public class SelectionSort2 {
   public static void main(String[] args) {
      sortieren(new int[] {12, 4, 66, 34, 62, 2, 3, 7, 1, 678});
   }
   
   public static void sortieren(int[] a) {
      System.out.println("SelectionSort");
      //SelectionSort-Algorithmus
      for(int i = 0; i < a.length; i++) {
         for(int j = i; j < a.length; j++) {
            if(a[j] < a[i]) {
               int temp = a[j];
               a[j] = a[i];
               a[i] = temp;
            }
         }
      }
      
      //das hier ist nur die Ausgabe des SelectionSort-Algorithmus
      for(int i = 0; i < a.length; i++) {
         System.out.println(a[i]);
      }
   }
}


Natürlich kann man das auch ein wenig abändern, doch ich hab das jetzt mal so geschrieben ...
Try to live every day as though it were your last. Make the
most of every day and accomplish something. Even if it is
something small, every baby step adds up to a huge success
in the end.
keyes
 
Beiträge: 20
Registriert: Mo 15. Jun 2009, 13:48

Zurück zu Algorithmen und Datenstrukturen

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast

cron