冒泡、选择和快速排序算法的C++实现

冒泡、选择和快速排序算法的C++实现

#include <iostream>
using namespace std;

void print(int* pData, int count){
    for (int i = 0; i< count; i++) {
        cout << pData[i] << " ";
    }
    cout << endl;
}
//C++实现冒泡排序(升序)
void BubbleSort(int* pData, int count)
{
    int temp;
    for (int i = 1; i < count; i++)
    {    //每一轮选出 最大的
        for (int j = count - 1; j >= i; j--)
        {
            if (pData[j-1] > pData[j])
            {
                temp = pData[j - 1];
                pData[j - 1] = pData[j];
                pData[j] = temp;
            }
        }
        cout<<"第"<<i<<"轮排序后:";
        print(pData, count);
    }
}
//C++实现冒泡排序(升序)
void BubbleSort1(int a[],int size)
{
    int temp;
    for(int pass=1; pass<size; ++pass)
    {    //每一轮选出 最大的
        for(int k=0; k<size-pass; ++k) 
        {	
            if(a[k]>a[k+1])
            {
                temp=a[k];
                a[k]=a[k+1];
                a[k+1]=temp;       
            }
        }
        cout<<"第"<<pass<<"轮排序后:";
        for(int i=0;i<size;i++)
        {	cout<<a[i]<<","; }
        cout<<endl;
    }
}
//选择排序(升序)
void SelectSort(int *a, int len)
{
    int k = 0;
    for (int i=0; i<len-1; i++)
    {
        k = i;
        for (int j=i+1; j<len; j++)
        {
            if (a[j]<a[k])
            {
                k = j;
            }
        }
        if (k!=i)
        {	swap(a[i], a[k]); }
        cout << "The "<< i <<" round:" << endl;
        print(a, len);
        cout << "----------------------------" << endl;
    }
}
//快速排序(升序) 
void quick_sort(int a[], int left, int right)
{
    if(left>=right)
    {
        //std::cout<<"left is bigger than right,please check"<<std::endl;
        return ;
    }

    int i = left;
    int j = right;
    int x = a[i];

    while(i<j)
    {
        while(i<j && a[j]>x) //向前搜索
        {	j--;  }
        if(i<j)
        {
            a[i] = a[j];
            i++;
        }

        while(i<j && a[i]<x) //向后搜索
        {	i++;	}
        if(i<j)
        {
            a[j] = a[i];
            j--;
        }
    }
    a[i] = x;
    quick_sort(a, left, i-1);
    quick_sort(a, i+1, right);
}

int main(int argc, char *argv[])
{
    int data[] = {10, 8, 9, 7, 4, 5};
    BubbleSort(data, 6);
    cout << "The BubbleSort result:" << endl;
    print(data, 6);
    cout<<std::endl;

    int data1[] = {10, 8, 9, 7, 4, 5};
    BubbleSort1(data1, 6);
    cout << "The BubbleSort1 result:" << endl;
    print(data1, 6);
    cout<<std::endl;

    int data2[] = {10, 8, 9, 7, 4, 5};
    SelectSort(data2,6);
    cout << "The SelectSort result:" << endl;
    print(data2, 6);
    cout<<std::endl;

    int a[] = {21,35,15 ,37,26,13,27,49,55,14}; 
    cout<<"Before quick_sort: ";
    for(int i =0 ; i<10;i++)
    {	cout<<a[i]<<" ";	}
    cout<<endl;
    quick_sort(a, 0, 9);
    cout<<"After quick_sort: ";
    for(int i =0 ; i<10;i++)
    {	cout<<a[i]<<" ";	}
    cout<<endl;
    return 0;
}

    原文作者:排序算法
    原文地址: https://blog.csdn.net/chinawangfei/article/details/50765327
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞