几种常见的排序算法(C++)

来源:互联网 发布:软件开发环境 编辑:程序博客网 时间:2024/04/24 13:33

说到排序,网上一搜就有一大堆的博客资料,涵盖各种语言实现,而许多算法书中更是写的很详细,写此博客只是记录下所敲的这几行代码,以便日后查看。
直接贴domo:

#ifndef     SORT_H#define     SORT_H#include <algorithm>#include<iostream>using namespace std;template<typename T>class Sort{public :    void BubbleSort(T *array, const int length);            //冒泡排序    void SelectSort(T *array, const int length);            //选择排序     void InsertSort(T *array, const int length);            //插入排序      void QuickSort(T *array, int left,int right);           //快速排序     void ShellSort(T *array, const int length);             //希尔排序    void mergearray(T array[], int first, int mid, int last, T temp[]);    void Merge(T a[], int first, int last, T temp[]);       //归并排序     void MinHeapifix(T *array, int i, int heapSize);//调整最小堆              void MakeMinHeap(T *array,int heapSize);                //创建最小堆     void HeapSort(T *array, const int length);              //堆排序    void display(T *array,const int length);                //显示 };template<typename T>void Sort<T>::BubbleSort(T *array, const int length)        //冒泡排序{    T temp;    for(int i=0;i<length-1;i++)    {        for(int j=i;j<length-2;j++)        if(array[j]>array[j+1])        {            temp=array[j];            array[j]=array[j+1];            array[j+1]=temp;        }           }}template<typename T>void Sort<T>::SelectSort(T *array, const int length)         //选择排序 {    T temp;     for(int i=0;i<length;i++)    {        array[i];        for(int j=i+1;j<length;j++)        {            if(array[i]>array[j])            {                temp=array[i];                array[i]=array[j];                array[j]=temp;              }        }       }}template<typename T>void Sort<T>::InsertSort(T *array, const int length)        //插入排序  {    T temp;    int j;    for(int i=1;i<length;i++)                               //第一个数据默认插入第一个位置     {        temp = array[i];                                    //待插入的元素         for( j=i;j>0 && temp<array[j-1];j--)            {            array[j]= array[j-1];                           //所有比待插入大的往后移         }         array[j]=temp;                                      //空出来就是要插入的位置     } }template<typename T>void Sort<T>::QuickSort(T *array, int left,int right)        //快速排序 {    if(left<right)    {        int i=left,j=right;        T baseval=array[i];                                 //选择基准值        while(i<j)        {            while(array[j]>=baseval && i<j)                 j--;            if(i<j)                                         //判断为防止i=j时无效操作             array[i++]=array[j];                while(array[i]<=baseval && i<j)                i++;            if(i<j)                                             array[j--]=array[i];                    }         array[i]=baseval;        QuickSort(array,left,i-1);        QuickSort(array,j+1,right);    }} template<typename T>void Sort<T>::ShellSort(T *array, const int length)         //希尔排序{    int i,j,gap;    for(gap=length/2;gap>0;gap/=2)    {        for(i=0;i<gap;i++)        {            for(j=i+gap;j<length;j+=gap)                    //相当于步长可变的插入排序             {                if(array[j]<array[j-gap])                {                    T temp=array[j];                        //递增排序,先暂存后一元素                     int k=j-gap;                    while(k>=0 &&array[k]>temp)                    {                        array[k+gap]=array[k];              //已保存可后移将其覆盖                         k-=gap;                    }                    array[k+gap] =temp;                     //插入的位置                 }            }        }    }}template<typename T> void Sort<T>:: mergearray(T array[], int first, int mid, int last, T temp[]){    int i = first, j = mid + 1;    int m = mid,   n = last;    int k = 0;    while (i <= m && j <= n)    {        if (array[i] <= array[j])            temp[k++] = array[i++];        else            temp[k++] = array[j++];    }    while (i <= m)        temp[k++] = array[i++];    while (j <= n)        temp[k++] = array[j++];    for (i = 0; i < k; i++)        array[first + i] = temp[i];}template<typename T>void Sort<T>::Merge(T array[], int first, int last,T temp[])                //归并排序 {    if (first < last)    {        int mid = (first + last) / 2;        Merge(array, first, mid, temp);    //左边有序        Merge(array, mid + 1, last, temp); //右边有序        mergearray(array, first, mid, last, temp); //再将二个有序数列合并    }} /** 二叉堆是完全二叉树或者是近似完全二叉树。    二叉堆满足二个特性:1.父结点的键值总是大于或等于(小于或等于)任何一个子节点的键值。2.每个结点的左子树和右子树都是一个二叉堆(都是最大堆或最小堆)。        i结点的父结点下标就为(i – 1) / 2        它的左右子结点下标分别为2 * i + 1和2 * i + 2 **/template<typename T>void Sort<T>::MinHeapifix(T *array, int i, int heapSize)    {//  T temp=array[i];//  while(leftChild<heapSize)//  {//      if(rightChild <heapSize && array[rightChild] <array[leftChild])//          leftChild++;//      if(array[leftChild]>=temp)//          break;                      //相对与满足假设已是最小堆无需调整 //      array[i] = array[leftChild];//      i=leftChild;//      leftChild = 2*leftChild+1;                          //  }//  array[i]=temp;  for(int n =(heapSize-1)/2;n>=i;n--)  {    int leftChild=2*n+1;    int rightChild=2*n+2;    if( array[n] > array[leftChild] && leftChild <heapSize)        swap(array[n],array[leftChild]) ;    if(array[n] > array[rightChild]&& rightChild <heapSize)        swap(array[n],array[rightChild]);    }} template<typename T>void Sort<T>::MakeMinHeap(T *array,int heapSize)                //创建最小堆 {    for(int n=(heapSize-1)/2;n>=0;n--)        MinHeapifix(array,n,heapSize);} template<typename T>void Sort<T>:: HeapSort(T *array, const int length)             //堆排序 {//      MakeMinHeap(array, length);//      for(int i=0;i<length;i++)//      cout<<array[i]<<endl;    for (int i = length - 1; i >= 1; i--)    {        swap(array[i], array[0]);        MinHeapifix(array,0, i);    }}template<typename T>void Sort<T>::display(T *array,const int length){    for(int i=0;i<length;i++)     {        cout<<array[i]<<"  ";    }}#endif

以上代码,在本地都跑过了,以下是验证代码:

#include<iostream>#include"Sort.h"using namespace std;int main(){    int testarray[]={11,25,78,12,64,84,13,48,38,94};//{9,12,17,30,50,20,60,65,4,19};    int array[10];    const int length = 10;    Sort<int> *sort = new Sort<int>();     sort->display(testarray,length);    cout<<endl<<"after sort: "<<endl;//  sort->BubbleSort(testarray,length);                     //冒泡排序 //  sort->SelectSort(testarray,length);                     //选择排序 //  sort->InsertSort(testarray,length);                     //插入排序 //  sort->QuickSort(testarray,0,9);    /******堆排序*****///  sort->MakeMinHeap(testarray,length);//  sort->HeapSort(testarray,length);    /********/ //  sort->ShellSort(testarray,length);    sort->Merge(testarray,0,9,array);    sort->display(testarray,length);    delete sort;    sort = NULL;    return 0;}

关于时间复杂度O的比较和稳定性分析:
这里写图片描述


注:
1.以上代码的思路参考自博主 MoreWindows的白话经典算法。传送门
2.此外还有慕课网的这篇文章。传送门

0 1