Quicksort and merge template

Quick row template

void quickSort(int nums[], int l, int r)
{
    
    
    if(l >= r) return;

    int x = nums[l];
    int lp = l-1, rp = r+1; // 定义左右两指针

    while(lp < rp)
    {
    
    
        do lp++; while (q[lp] < x);
        do rp--; while (q[rp] > x);

        if (lp < rp) swap(q[lp], q[rp]);
    }
	//写法一:
    quickSort(nums, l, rp);
    quickSort(nums, rp+1, r);
    /**
   	写法二:
    quickSort(nums, r, lp-1);
    quickSort(nums, lp, r);  	
    **/
}

Note: There is a small detail in the selection of recursive intervals. If you choose notation 2, then x cannot select the left end of the interval. Why does this happen? Consider an extreme case as follows: 3, 6, 4, 7, 9, 8; at this time, the left pointer will not move, and when the recursive call is made, the entire interval is still obtained, resulting in an endless loop. In the same way, when the wording is adopted, x cannot be taken to the right end of the interval.
For the positions of lp and rp after one division: If the selected division value is odd, then lp and rp overlap, if the selected division value is odd and even value, then rp is located at the position before lp.

Example: Portal
 
Solution:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
const int N = 100010;
int nums[N], n;

void quickSort(int q[], int l, int r)
{
    
    
    if(l >= r) return;

    int x = q[rand()%(r-l+1)+l]; //防止特殊数据,随机选择区间内的点
    int lp = l-1, rp = r+1;

    while(lp < rp)
    {
    
    
        do lp++; while (q[lp] < x);
        do rp--; while (q[rp] > x);

        if (lp < rp) swap(q[lp], q[rp]);

    }

    quickSort(nums, l, rp);
    quickSort(nums, rp+1, r);
}


int main ()
{
    
    
    srand((unsigned int)time(NULL));
    cin >> n;
    for (int i = 0; i < n; i++) cin >> nums[i];

    quickSort(nums, 0, n-1);

    for(int i = 0; i < n; i++)
    {
    
    
        cout << nums[i];
        if(i == n-1) cout << endl;
        else cout << " ";
    }

    return 0;
}

 

Merge template

void mergeSort(int q[], int l, int r)
{
    
    
    if (l >= r) return;
    
    int mid = (l + r) >> 1;
    
    mergeSort(q, l, mid), mergeSort(q, mid+1, r);
    
    // 利用临时数组temp进行合并
    int k = 0, f = l, s = mid + 1;// f为左区间的指针,s为右区间的指针
    while (f <= mid && s <= r)
    {
    
    
        if(q[f] <= q[s]) temp[k++] = q[f++];
        else temp[k++] = q[s++];
    }
    
    while (f <= mid) temp[k++] = q[f++];
    while (s <= r) temp[k++] = q[s++];
    
    for(int i = l, j = 0; i <= r; i++, j++) q[i] = temp[j];
}

Example: Portal
 
Solution:

#include <iostream>
using namespace std;

const int N = 100010;

int nums[N], temp[N], n;

void mergeSort(int q[], int l, int r)
{
    
    
    if (l >= r) return;
    
    int mid = (l + r) >> 1;
    
    mergeSort(q, l, mid), mergeSort(q, mid+1, r);
    
    int k = 0, f = l, s = mid + 1;
    while (f <= mid && s <= r)
    {
    
    
        if(q[f] <= q[s]) temp[k++] = q[f++];
        else temp[k++] = q[s++];
    }
    
    while (f <= mid) temp[k++] = q[f++];
    while (s <= r) temp[k++] = q[s++];
    
    for(int i = l, j = 0; i <= r; i++, j++) q[i] = temp[j];

}

int main()
{
    
    
    cin >> n;
    for (int i = 0; i < n; i++) cin >> nums[i];
    
    mergeSort(nums, 0, n-1);
    
    for (int i = 0; i < n; i++) cout << nums[i] << " ";
    
    return 0;
}

Guess you like

Origin blog.csdn.net/PBomb/article/details/107403107