[알고리즘] 빨리 배열 하 세 요.

4512 단어 알고리즘

빠 른 배열 알고리즘, 인쇄 빠 른 배열 과정:
  
#include <iostream>
using namespace std;

void PrintArr(int * arr, int length)
{
    for (int i = 0; i < length; i++)
    {
        cout << " " << arr[i] << " ";
    }
    cout << endl;
}

void QuickSort(int *arr, int nfirst, int nlast)
{
    if (nfirst > nlast)
    {
        return;
    }
    int first = nfirst;
    int last = nlast;
    int key = arr[first];
    while (first < last)
    {
        while (first < last && arr[last] >= key)
            --last;        
        arr[first] = arr[last];
        PrintArr(arr, 16);
        while (first < last && arr[first] <= key)
            ++first;
        arr[last] = arr[first];
        PrintArr(arr, 16);
    }
    arr[first] = key;
    PrintArr(arr, 16);
    cout <<"----------------------------------" <<endl;
    QuickSort(arr, nfirst, first - 1);
    QuickSort(arr, first + 1, nlast);
}


void main()
{
    int arr[] = { 5, 6, 3, 7, 1, 2, 5, 3, 2, 7, 4, 9, 2, 6, 3, 4 };
    QuickSort(arr, 0, 15);
    cout << endl << endl;
    PrintArr(arr,16);
    getchar();
}

좋은 웹페이지 즐겨찾기