Chapter VI - quick sort, merge sort

Beep beep miles miles explain address data structure: https://space.bilibili.com/356198029

Video to explain the process address: https://www.bilibili.com/video/av75201323

Quick Sort

#include<iostream>
using namespace std;
void quicksort(int[], int, int);
int partition(int[], int, int);
int main()
{
    int array [] = {55,2,6,4,32,12,9,73,26,37};

    int len = sizeof(array) / sizeof(int ;) 

    COUT << " original input sequence:   " ;
     for ( int I = 0 ; I <len; I ++) // output prosequence 
        COUT << Array [I] << " , " ; 
    COUT << endl < < endl; 

    quicksort (Array, 0 , len . 1 ); // call to the sort function 
    COUT << "   ---- quicksort results ---- " << endl;
     for ( int I = 0 ; I <len; ++ I ) 
        COUT <<array[i]<<" , " ; 
    COUT << endl;
     return  0 ; 
} 

void quicksort ( int A [], int left, int right) // row fast algorithm 
{
     IF (left < right) 
    { 
        int pivotpos = Partition (A, left, right ); // sorted reference element 
        quicksort (a, left, pivotpos- . 1 ); // the reference elements divided blocks, recursive 
        quicksort (a, pivotpos + . 1 , right); // the divided block elements reference, recursion 
    } 

} 

intPartition ( int A [], int left, int right) // partitioning algorithm, the core 
{
     int Pivot = A [left];
     the while (left <right) // two ends meet 
    {
         the while (left <right && A [right ]> = Pivot) --right; // the last bit of the beginning portion of each inspection 
        IF (left <right) a [left ++] = a [right]; // than the reference small binding while on the left side of the reference DESCRIPTION less than right Pivot 
        the while (left <right && a [left] <= Pivot) left ++; // from an initial start checking each part 
        IF (left <right) a [right -] = [left] a ; // larger than the reference in the reference binding while the right side is greater than the left pivot DESCRIPTION
     }
    A [left] = Pivot; // the reference element in the final position, are smaller than that of his left, to the right is greater than his 
    return left; 
}

Merge sort

#include<iostream>
using namespace std;

void Merge(int [], int, int, int, int []);
// 归并排序划分过程
void MergeSort(int a[], int left, int right, int temp[])
{
    if(left < right)
    {
        int mid = (left + right) >> 1;
        MergeSort(a,left,mid,temp);
        MergeSort(a,mid+1,right,temp);
        The Merge (A, left, MID, right, TEMP); 
    } 
} 

// merge sort merge process
 // array subscript range [left, mid] and [mid + 1, right] ordered into a sequence of ordered merge sequence 
void the Merge ( int a [], int left, int MID, int right, int TEMP []) 
{ 
    int p0 = 0 ;
     int P1 = left, + P2 = MID . 1 ;
     // first auxiliary array points p0
     @ p1 points to an array [left, mid] the first, p2 point to the array [mid + 1, right] the first
     // start sorting, <ascending to put a small element,> descending 
    the while (p1 <P2 = MID && <= right) 
    { 
        if (a [P1] < a [P2])
            TEMP [P0 ++] = A [++ P1 ];
         the else 
            TEMP [P0 ++] = A [P2 ++ ]; 
    } 
    /// / If there is remaining elements, the auxiliary array directly into 
    the while (P1 <= MID) 
        temp [P0 ++] = a [P1 ++ ];
     the while (P2 <= right) 
        temp [P0 ++] = a [P2 ++ ];
     // element in good order to copy the temp array a row,
     // Note array a starting from the left, the default is not 0, and a total right-left + 1 element element. 
    for ( int I = 0 ; I <+ right-left . 1 ; ++ I) 
        A [left + I] =TEMP [I]; 
} 
int main () 
{ 
    int Array [] = { 55 , 2 , . 6 , . 4 , 32 , 12 is , . 9 , 73 is , 26 is , 37 [ };
     int len = the sizeof (Array) / the sizeof ( int ) ; 
    COUT << " original input sequence:   " ;
     for ( int I = 0 ; I <len; I ++) // output prosequence 
        COUT << Array [I] << ", " ; 
    COUT << endl << endl; 

    COUT << "   ---- merge sort results ---- " << endl;
     int * TEMP = new new  int [len]; 
    mergesort (Array, 0 , len . 1 , TEMP); // call to the sort function 
    for ( int J = 0 ; J <len; J ++ ) 
        COUT << Array [J] << " , " ; 

    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/xwxz/p/11867812.html