小屋創作

日誌2019-05-12 01:34

Quick Sort (快速排序法)

作者:パっパっパスタ


#include <iostream>

using namespace std;

void swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

int Partition(int arr[], int front, int end)
{
    int pivot = arr[end];
    int i = front - 1;
    for (int j = front; j < end; j++)
    {
        if (pivot > arr[j])
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    i++;
    swap(&arr[i], &arr[end]);
    return i;
}

void QuickSort(int arr[], int front, int end)
{
    if (front < end)
    {
        int pivot = Partition(arr, front, end);
        QuickSort(arr, front, pivot - 1);
        QuickSort(arr, pivot + 1, end);
    }
}

int main()
{
    int n = 9;
    int arr[] = {8, 6, 4, 2, 9, 7, 1, 3, 5};
    cout << "before quick: " << endl;
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
    QuickSort(arr, 0, n - 1);
    cout << "after quick: " << endl;
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
    //system("pause");
}


VSCode會把大括弧自動換行 所以就變成這樣了
最近都很少翻Python的書  上課都快聽不懂了.....
是時候爆讀一波Python了
除了Python之外,日檢進度也是有點緩慢  
背單字真的好容易忘記啊~  N1文法有點小複雜啊~~~

昨天剛破完人中之龍0  真的太感動了!
真的是神作欸!  在玩人中之龍0之前,唯一我有破關的單機大作就只有巫師3而已
劇情棒、結局感人、真島吾郎帥炸天(関西弁ええやん!)、女主角是澤城美雪配音的所以推爆!、打鬥超爽快,慶幸自己當初有買Xbox手把 ><




おおきに
ほな

2

3

LINE 分享

相關創作

基礎&造型。 0502 道中 (上班趕進度喵

焦慮恐慌症的治療日記

マーサファーム(烏拉拉退休牧場)巡禮

留言

開啟 APP

face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】