Advertisement
cr34tiv3

[JAVA] Merge Sort /f Beginner /w Comments

Sep 12th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1.  public int[] mergeSort(int[] pArray)
  2.      {
  3.          int n=pArray.length;
  4.          //****************************************************
  5.          //******************STARTBEDINGUNG********************
  6.          //****************************************************
  7.            if(n<=1)   // Wenn das Array die Laenge 0 oder 1 hat, braucht man nichts
  8.                     // zu sortieren.
  9.          {
  10.            return pArray;
  11.          }
  12.          else      // Jetzt ist die Laenge also mindestens 2.
  13.          {
  14.            int[] ausgabeArray = new int[n];
  15.            //*************************************************
  16.            //******DEFINITION**DER**HILFSARRAYS***************
  17.            //*************************************************
  18.            int laengeLinks = n/2;                           // Die Hälfte wird genommen
  19.            int laengeRechts = n - laengeLinks;              // Der "Rest" wird genommen
  20.            
  21.            int[] links = new int[laengeLinks];
  22.            int[] rechts = new int[laengeRechts];
  23.            //*************************************************
  24.            //*********BELEGUNG**DER**HILFSARRAYS**************
  25.            //*************************************************
  26.            for (int i=0; i<laengeLinks; i++)
  27.            {
  28.              links[i] = pArray[i];                          //der "linke" Teil wird in das Hilfsarray gespeichert
  29.            }
  30.            
  31.            for (int i=0; i<laengeRechts; i++)
  32.            {
  33.              rechts[i] = pArray[i+laengeLinks];             //s.o. *rechte
  34.            }
  35.            //*************************************************
  36.            //*************REKURSION**HILFSARRAYS**************
  37.            //*************************************************
  38.            links = mergeSort(links);
  39.            rechts = mergeSort(rechts);
  40.            //*************************************************
  41.            //****MERGE-VORGANG(d.h. Verschmelz-Vorgang)*******
  42.            //*************************************************
  43.            
  44.            int linksIndex = 0;                              //Indizes die für die "Verwaltung" des Merges verantwortlich sind
  45.            int rechtsIndex = 0;
  46.            int gesamtIndex = 0;
  47.            
  48.            while( (linksIndex < laengeLinks) && (rechtsIndex < laengeRechts) )
  49.            {
  50.              if(links[linksIndex] < rechts[rechtsIndex])    // Welcher Wert ist kleiner?
  51.              {
  52.                ausgabeArray[gesamtIndex] = links[linksIndex];//Die "x"te Stelle wird mit dem "x"ten wert von links genommen
  53.                gesamtIndex++;                               // gesamtIndex + 1, damit es beim nächsten Mal um die 'nächste' Stelle geht
  54.                linksIndex++;                                // links Index + 1, damit nicht schon wieder dieselbe Stelle genommen wird
  55.              }
  56.              else                                           // Rechts war kleiner
  57.              {
  58.                ausgabeArray[gesamtIndex] = rechts[rechtsIndex];//s.o. *rechts
  59.                gesamtIndex++;                               //s.o.
  60.                rechtsIndex++;                               //s.o. *rechts
  61.              }
  62.            }
  63.            //*************************************************
  64.            //****************REST**WIRD**EINGEFÜGT************
  65.            //*************************************************
  66.            
  67.            if( linksIndex == laengeLinks ) // Der "Rest" wird eingefügt
  68.            {
  69.              while( rechtsIndex < laengeRechts )
  70.              {
  71.                ausgabeArray[gesamtIndex] = rechts[rechtsIndex];    
  72.                gesamtIndex++;
  73.                rechtsIndex++;
  74.              }
  75.            }
  76.            else
  77.            {
  78.              while( linksIndex < laengeLinks )
  79.              {
  80.                ausgabeArray[gesamtIndex] = links[linksIndex];
  81.                gesamtIndex++;
  82.                linksIndex++;
  83.              }
  84.            }
  85.            //************************************************
  86.            //**********ENDE**DES**MERGE-VORGANGS*************
  87.            //************************************************
  88.            return ausgabeArray;
  89.            //************************************************
  90.            //****************▲cr34tiv3▲**********************
  91.          }
  92.  
  93.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement