Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int[] mergeSort(int[] pArray)
- {
- int n=pArray.length;
- //****************************************************
- //******************STARTBEDINGUNG********************
- //****************************************************
- if(n<=1) // Wenn das Array die Laenge 0 oder 1 hat, braucht man nichts
- // zu sortieren.
- {
- return pArray;
- }
- else // Jetzt ist die Laenge also mindestens 2.
- {
- int[] ausgabeArray = new int[n];
- //*************************************************
- //******DEFINITION**DER**HILFSARRAYS***************
- //*************************************************
- int laengeLinks = n/2; // Die Hälfte wird genommen
- int laengeRechts = n - laengeLinks; // Der "Rest" wird genommen
- int[] links = new int[laengeLinks];
- int[] rechts = new int[laengeRechts];
- //*************************************************
- //*********BELEGUNG**DER**HILFSARRAYS**************
- //*************************************************
- for (int i=0; i<laengeLinks; i++)
- {
- links[i] = pArray[i]; //der "linke" Teil wird in das Hilfsarray gespeichert
- }
- for (int i=0; i<laengeRechts; i++)
- {
- rechts[i] = pArray[i+laengeLinks]; //s.o. *rechte
- }
- //*************************************************
- //*************REKURSION**HILFSARRAYS**************
- //*************************************************
- links = mergeSort(links);
- rechts = mergeSort(rechts);
- //*************************************************
- //****MERGE-VORGANG(d.h. Verschmelz-Vorgang)*******
- //*************************************************
- int linksIndex = 0; //Indizes die für die "Verwaltung" des Merges verantwortlich sind
- int rechtsIndex = 0;
- int gesamtIndex = 0;
- while( (linksIndex < laengeLinks) && (rechtsIndex < laengeRechts) )
- {
- if(links[linksIndex] < rechts[rechtsIndex]) // Welcher Wert ist kleiner?
- {
- ausgabeArray[gesamtIndex] = links[linksIndex];//Die "x"te Stelle wird mit dem "x"ten wert von links genommen
- gesamtIndex++; // gesamtIndex + 1, damit es beim nächsten Mal um die 'nächste' Stelle geht
- linksIndex++; // links Index + 1, damit nicht schon wieder dieselbe Stelle genommen wird
- }
- else // Rechts war kleiner
- {
- ausgabeArray[gesamtIndex] = rechts[rechtsIndex];//s.o. *rechts
- gesamtIndex++; //s.o.
- rechtsIndex++; //s.o. *rechts
- }
- }
- //*************************************************
- //****************REST**WIRD**EINGEFÜGT************
- //*************************************************
- if( linksIndex == laengeLinks ) // Der "Rest" wird eingefügt
- {
- while( rechtsIndex < laengeRechts )
- {
- ausgabeArray[gesamtIndex] = rechts[rechtsIndex];
- gesamtIndex++;
- rechtsIndex++;
- }
- }
- else
- {
- while( linksIndex < laengeLinks )
- {
- ausgabeArray[gesamtIndex] = links[linksIndex];
- gesamtIndex++;
- linksIndex++;
- }
- }
- //************************************************
- //**********ENDE**DES**MERGE-VORGANGS*************
- //************************************************
- return ausgabeArray;
- //************************************************
- //****************▲cr34tiv3▲**********************
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement