zoukankan      html  css  js  c++  java
  • 快速排序

    笔试快速排序,没写出来,回宿舍后好好写了写,去弄清了原理。开始看不懂,自己写了几个数模拟了一遍,弄懂了。

    #include<iostream>
    using namespace std;
    
    void Qsort(int a[], int low, int high)
    {
        if (low >= high)
        {
            return;
        }
        int first = low;
        int last = high;
        int key = a[first];/*用字表的第一个记录作为枢轴*/
    
        while (first < last)
        {
            while (first < last && a[last] >= key)
            {
                --last;
            }
    
            a[first] = a[last];/*将比第一个小的移到低端*/
    
            while (first < last && a[first] <= key)
            {
                ++first;
            }
    
            a[last] = a[first];
            /*将比第一个大的移到高端*/
        }
        a[first] = key;/*枢轴记录到位*/
        Qsort(a, low, first - 1);
        Qsort(a, first + 1, high);
    }
    
    void show(int list[], int n) {    // 输出列表中元素
        for (int i = 0; i<n; i++)
            printf("%d ", list[i]);
            printf("
    ");
    }
    
    int main(int argc, char* argv[]) {
        int list[10] = { 23, 65, 26, 1, 6, 89, 3, 12, 33, 8 };
        show(list, 10);      // 输出排序前序列
        Qsort(list, 0, 9);     // 快速排序
        show(list, N);      // 输出排序后序列
    
        system("pause");
        return 0;
    }

     运行结果:

  • 相关阅读:
    判断DataSet为空
    msxml3.dll 错误 '800c0008'
    google Map api地理位置坐标转换
    C# .net中cookie值为中文时的乱码解决方法
    windows pear 安装
    smarty2 设置、变量、函数
    简单模板类
    mysql 1366 插入错误
    Oracle修改账户口令
    C# Winform验证码
  • 原文地址:https://www.cnblogs.com/zhaokewei/p/5119966.html
Copyright © 2011-2022 走看看