4

Informatik (Fach) / Sortieren (Lektion)

Sortieralgorithmen

Diese Lektion wurde von JimboSteam erstellt.

Lektion lernen

  • selectionSort public void selectionSort(Datenpaket[]a){      vergleiche = 0;      vertausche = 0;      Datenpaket tmp;      for(int i=0; i<a.length-1; i++){           for(int k=i+1; k<a.length; k++){                vergleiche++;                if(a[i].getZahl()>a[k].getZahl()){                      tmp=a[i];                      a[i]=a[k];                      a[k]=tmp;                      vertausche++;                }           }      } }
  • insertionSort public void insertionSort(Datenpaket[]a){      vergleiche = 0;     vertausche = 0;     for(int i=1; i<a.length; i++){ // evtl i = 2 ?          Datenpaket tmp=a[i];          int j=i;          vergleiche++;//          while (j>0 && a[j-1].getZahl()>tmp.getZahl()){               vergleiche++;               //kein vertausch sondern shift               a[j]=a[j-1];               j--;           }         a[j]=tmp;         vertausche++;     } }
  • BubbleSort public void bubbleSort(int[] x) {      boolean unsortiert=true;      int temp;       while (unsortiert){           unsortiert = false;           for (int i=0; i < x.length-1; i++)                 if (x[i] > x[i+1]) {                      temp = x[i];                     x[i] = x[i+1];                     x[i+1] = temp;                     unsortiert = true;                }       } }
  • QuickSort public static void sortiere(int x[]) {     qSort(x, 0, x.length-1); } public static void qSort(int x[], int links, int rechts) {    if (links < rechts) {         int i = partition(x,links,rechts);         qSort(x,links,i-1);         qSort(x,i+1,rechts);    } } public static int partition(int x[], int links, int rechts) {      int pivot, i, j, help;      pivot = x[rechts];       i = links;      j = rechts-1;     while(i<=j) {          if (x[i] > pivot) {                // tausche x[i] und x[j]               help = x[i];                x[i] = x[j];                x[j] = help;                j--;          } else i++;       }      // tausche x[i] und x[rechts]      help = x[i];      x[i] = x[rechts];      x[rechts] = help;      return i; }
Anzeige