7中排序算法c++版

来源:互联网 发布:java main spring 编辑:程序博客网 时间:2024/06/11 21:40

上次发布的java版,这次的c++版仅仅是上次的不同形式而已!
1、基类

#ifndef _SORT_H_#define _SORT_H_#include <iostream>using namespace std;class BaseSort{protected:    int * data;    int number;public :    BaseSort(int * d,int n)    {        this->data=d;        this->number=n;    }    virtual void sort()=0;    void display();    void swap(int i, int j);};#endif
#include "Sort.h"//显示void BaseSort::display(){    for(int i=0;i<number;i++)    {        if(i==number-1)            cout<<data[i]<<endl;        else            cout<<data[i]<<"、";    }}//交换数据void BaseSort::swap(int i, int j){    int temp=data[i];    data[i]=data[j];    data[j]=temp;}

2、冒泡排序法

#ifndef _BUBBLESORT_H_#define _BUBBLESORT_H_#include "Sort.h"/* *  归并排序 */class BubbleSort:public BaseSort{public :    BubbleSort(int * d,int n):BaseSort(d,n){}    void sort();};#endif
#include "BubbleSort.h"//冒泡排序法void BubbleSort::sort(){    // 标记是否有交换数据    bool flag=true;    for(int i=0;i<number&&flag;i++)    {        flag=false;        for(int j=number-1;j>i;j--)        {            if(data[j]<data[j-1])            {                swap(j-1,j);                flag=true;            }        }    }}

3、

#ifndef _HEAPSORT_H_#define _HEAPSORT_H_#include "Sort.h"/* * 堆排序 */class HeapSort:public BaseSort{public :    HeapSort(int * d,int n):BaseSort(d,n){};    void sort();    void createMaxdHeap(int lastIndex);};#endif
#include "HeapSort.h"void HeapSort::sort(){    for (int i = 0; i < number; i++) {        createMaxdHeap(number - 1 - i);        swap(0, number - 1 - i);    }}void HeapSort::createMaxdHeap(int lastIndex){    for (int i = (lastIndex - 1) / 2; i >= 0; i--) {        // 保存当前正在判断的节点        int k = i;        // 若当前节点的子节点存在        while (2 * k + 1 <= lastIndex) {            // biggerIndex总是记录较大节点的值,            // 先赋值为当前判断节点的左子节点            int biggerIndex = 2 * k + 1;            if (biggerIndex < lastIndex) {                // 若右子节点存在,                // 否则此时biggerIndex应该等于 lastIndex                if (data[biggerIndex] < data[biggerIndex + 1]) {                    // 若右子节点值比左子节点值大,                    // 则biggerIndex记录的是右子节点的值                    biggerIndex++;                }            }            if (data[k] < data[biggerIndex]) {                k = biggerIndex;            } else {                break;            }        }        if (i != k) {            swap(k, i);        }    }}

5、

#ifndef _MERGINGSORT_H_#define _MERGINGSORT_H_#include "Sort.h"class MergingSort:public BaseSort{public :    MergingSort(int * d,int n):BaseSort(d,n){}    void sort();    void merge(int low, int mid, int high);    void sortF(int low, int high);};#endif
#include "MergingSort.h"void MergingSort::sort(){    sortF(0,number-1);}void MergingSort::merge(int low, int mid, int high){    int * temp=new int[high-low+1];    int i = low;// 左指针    int j = mid + 1;// 右指针    int k = 0;    while(i<=mid&&j<=high)    {        if(data[i]<data[j])        {            temp[k++]=data[i++];        }        else        {            temp[k++]=data[j++];        }    }    // 把左边剩余的数移入数组    while (i <= mid) {        temp[k++] = data[i++];    }    // 把右边边剩余的数移入数组    while (j <= high) {        temp[k++] = data[j++];    }    // 把新数组中的数覆盖nums数组    for (int k2 = 0; k2 < high-low+1; k2++) {        data[k2 + low] = temp[k2];    }    delete [] temp;}void MergingSort::sortF(int low, int high){    int mid = (low + high) / 2;    if (low < high) {        // 左边        sortF(low, mid);        // 右边        sortF(mid + 1, high);        // 左右归并        merge(low, mid, high);    }}

6、

#ifndef _QUICKSORT_H_#define _QUICKSORT_H_#include "Sort.h"/* * 快速排序 */class QuickSort:public BaseSort{public :    QuickSort(int * d,int n):BaseSort(d,n){};    void sort();    void sortF(int low, int high);    int partition(int low, int high);};#endif
#include "QuickSort.h"void QuickSort::sort(){    sortF(0,number-1);}int QuickSort::partition(int low, int high){    int pivotkey;    // 优化点    int m = low + (high - low) / 2;    if(data[low]>data[high])        swap(low,high);    if(data[m]>data[high])        swap(m,high);    if(data[m]>data[high])        swap(m,high);    pivotkey=data[low];    while (low < high) {        // 重表的两端像中间扫描        while (low < high && data[high] >= pivotkey) {            high--;        }        if (low != high) {            swap(low, high);        }        while (low < high && data[low] <= pivotkey) {            low++;        }        if (low != high) {            swap(low, high);        }    }    return low;}void QuickSort::sortF(int low, int high){    int pivot;    if (low < high) {        pivot = partition(low, high);        sortF(low, pivot - 1);        sortF(pivot + 1, high);    }}

7、

#ifndef _SHELLSORT_H_#define _SHELLSORT_H#include "Sort.h"/* * 希尔排序 */class ShellSort:public BaseSort{public :    ShellSort(int * d,int n):BaseSort(d,n){}    void sort();};#endif
#include "ShellSort.h"void ShellSort::sort(){    // 步长    for (int gap = number / 2; gap > 0; gap /= 2) {        for (int j = gap; j < number; j += gap) {            // 从数组第gap个元素开始            if (data[j] < data[j - gap])// 每个元素与自己组内的数据进行直接插入排序            {                int temp = data[j];                int k = j - gap;                while (k >= 0 && data[k] > temp) {                    data[k + gap] = data[k];                    k -= gap;                }                data[k + gap] = temp;            }        }    }}

8、

#ifndef _SIMPLESELECTIONSORT_H_#define _SIMPLESELECTIONSORT_H_#include "Sort.h"/* * 简单选择排序 */class SimpleSelectionSort:public BaseSort{public :    SimpleSelectionSort(int * d,int n):BaseSort(d,n){};    void sort();};#endif
#include "SimpleSelectionSort.h"void SimpleSelectionSort::sort(){    int min = 0;    for (int i = 0; i < number; i++) {        min = i;        for (int j = i + 1; j < number; j++) {            if (data[j] < data[min])                min = j;        }        if (min != i) {            swap(min, i);        }    }}

9、

#ifndef _STRAIGHTINERTIONSORT_H_#define _STRAIGHTINERTIONSORT_H_#include "Sort.h"class StraightInsertionSort:public BaseSort{public:    StraightInsertionSort(int * d,int n):BaseSort(d,n){}    void sort();};#endif
#include "StraightInsertionSort.h"void StraightInsertionSort::sort(){    int temp = 0;    for (int i = 1; i < number; i++) {        if (data[i] < data[i - 1]) {            temp = data[i];            int j;            for (j = i - 1; j >= 0 && data[j] > temp; j--) {                data[j + 1] = data[j];            }            data[j + 1] = temp;        }    }}

10、测试类

#include <iostream>#include "Sort.h"#include "BubbleSort.h"#include "BucketSort.h"#include "HeapSort.h"#include "MergingSort.h"#include "QuickSort.h"#include "ShellSort.h"#include "SimpleSelectionSort.h"#include "StraightInsertionSort.h"#include "RadixSort.h"using namespace std;int main(){    int in[]={1,34,54,2,44,8,100,23,223,459,5645,12,23,45,67,123,456,8,25,67,89,234,565,199,328,100};    int len=sizeof(in) / sizeof(in[0]);    //BaseSort * sort=new BubbleSort(in,len);    //BaseSort * sort=new BucketSort(in,len);    //BaseSort * sort=new HeapSort(in,len);    //BaseSort * sort=new MergingSort(in,len);    //BaseSort * sort=new QuickSort(in,len);    //BaseSort * sort=new ShellSort(in,len);    //BaseSort * sort=new SimpleSelectionSort(in,len);    //BaseSort * sort=new StraightInsertionSort(in,len);    BaseSort * sort=new RadixSort(in,len);    sort->sort();    sort->display();    system("pause");      return 0;  }

下载地址在评论中。。。。

0 0
原创粉丝点击