Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

给你一个简单易懂的快速排序! #6

Open
kejianfeng opened this issue Aug 31, 2020 · 0 comments
Open

给你一个简单易懂的快速排序! #6

kejianfeng opened this issue Aug 31, 2020 · 0 comments

Comments

@kejianfeng
Copy link
Owner

要知道,快速排序利用的是分治的思想,基于交换!就是元素通过比较最终确定好每个元素的位置。

快速排序简单易懂之灵魂画手模拟快速排序过程

快速排序之简单代码实现

void QuickSort(int R[], int low, int high) //对R[low]到R[r]的关键字进行快速排序
{
    int temp;
    int i = low , j = high;
    if (low < high) {
        temp = R[low];
        //下面这个循环完成了一趟排序,将数组中小于temp的关键字放在左边,大于temp的关键字放在右边
        while (i != j) {
            while (j > i && R[j] >= temp) {
                --j //从左往右扫描,找一个小于temp的关键字
            }
            if(i < j) {
                R[i] = R[j];//挖j位置的数出来放在i位置上
                i++; //i右移一位
            }
            while (i < j && R[i] < R[j]) {
                i++;
            }
            if(i < j) {
                R[j] = R[i];
                j--;
            }
        }
        R[i] = temp;
        QuickSort(R, low, i-1);
        QuickSort(R, i+1,high);
    }
    return R;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant