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

    #include <iostream>
    
    using namespace std;
    const int N = 10;
    int data[]= {6,1,2,7,9,3,4,5,10,8};
    void quickSort(int* data, int low,int high);
    void printArray(int* data, int n);
    int partition(int* data,int low, int high);
    int main()
    {
        quickSort(data,0,N-1);
        printArray(data,N);
        return 0;
    }
    
    void quickSort(int* data, int low, int high)
    {
        if(low>=high) return;
        int key = partition(data,low,high);
        quickSort(data,low,key-1);
        quickSort(data,key+1,high);
    
    }
    void printArray(int* data, int n)
    {
        for(int i=0;i<n;++i)
        {
    
            cout<<data[i]<<"	";
        }
        cout<<endl;
    }
    int partition(int* data,int l, int h)
    {
        int low=l,high=h;
        int temp = data[low];
        while(low<high)
        {
            while(low<high && data[high]>=temp) --high;
            while(low<high && data[low]<=temp) ++low;
    
            if(low<high)
            {
                data[low] ^= data[high];
                data[high] ^= data[low];
                data[low] ^= data[high];
            }
        }
    
        data[l] = data[low];
        data[low] = temp;
        return low;
    
    
    }
    
  • 相关阅读:
    表格标签
    常用标签
    标签笔记
    基础标签与格式
    态度!
    如何修改数据 练习
    增删查练习
    登陆注册练习
    PHP 数据访问
    PHP 基础知识测试题 答案分析
  • 原文地址:https://www.cnblogs.com/yldf/p/6249896.html
Copyright © 2011-2022 走看看