数据结构八种排序方法作业示例(无讲解)

#include <cstdio>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <fstream>
using namespace std;

class insertSort{
public:
    insertSort(int * a,int size){
        this->a = a;
        this->size = size;
        b = new int[size];
    }
    ~insertSort(){
        delete b;
    }

    void sort(){
        for(int i =0;i<size;i++)b[i] =a[i];
        swapTime = 0;
        compTime = 0;
        start = time(&start);
        for(int i = 0;i<size; i++){
          //  printf("%d\n",a[i]);
          //  int minind = i;
            int j = i;
            for(j = i-1;j>=0;j--){
                compTime++;
                if(b[j]<=b[i]){
                    break;
                }
            }
            if(b[j]<=b[i]||j<0)j++;//要插入位置
            int tmp = b[i];
            for(int k = i;k>j;k--){
                    b[k] = b[k-1];
                    swapTime ++;
            }
            b[j] = tmp;
        }
        stop= time(&stop);
    }

    void print(){
        puts("1 直接插入排序");
        puts("展示结果");
        for(int i =0;i<size;i++)printf("%d%c",b[i],i==size-1?‘\n‘:‘ ‘);
        printf("关键字比较次数:%d\n",compTime);
        printf("关键字排序次数:%d\n",swapTime);
        printf("用时:%ld ms\n",stop-start);
    }
    int swapTime;
    int compTime;
private:
    int * a;
    int * b;
    int size;
    time_t start,stop;

};

class halfInsertSort{
public:
    halfInsertSort(int * a,int size){
        this->a = a;
        this->size = size;
        b = new int[size];
    }
    ~halfInsertSort(){
        delete b;
    }

    void sort(){
       for(int i=0;i<size;i++)b[i]=a[i];
        swapTime = 0;
        compTime = 0;
        start = time(&start);
        for(int i = 0;i<size; i++){
            int l=0,r=i;
            while(r-l>1){
                int mid = (l+r)>>1;
                if(b[mid] > b[i]){
                    r = mid;
                }
                else {
                    l = mid;
                }
                compTime++;
            }
            if(b[l]<=b[i])l++;
            int tmp = b[i];
            for(int k = i;k>l;k--){
                    b[k]=b[k-1];
                    swapTime ++;
            }
            b[l]=tmp;
        }
        stop= time(&stop);
    }

    void print(){
        puts("2 折半插入排序");
        puts("展示结果");
        for(int i =0;i<size;i++)printf("%d%c",b[i],i==size-1?‘\n‘:‘ ‘);
        printf("关键字比较次数:%d\n",compTime);
        printf("关键字排序次数:%d\n",swapTime);
        printf("用时:%ld ms\n",stop-start);
    }
    int swapTime;
    int compTime;
private:
    int * a;
    int * b;
    int size;
    time_t start,stop;

};

class shellSort{
public:
    shellSort(int * a,int size){
        this->a = a;
        this->size = size;
        b = new int[size];
    }
    ~shellSort(){
        delete b;
    }

    void sort(){
        for(int i =0;i<size;i++)b[i] =a[i];
        swapTime = 0;
        compTime = 0;
        start = time(&start);

        int times = int(log2(size+1));
        for(int i = 1;i <=times;i++){
            int d = int(pow(2,times-i+1)-1);
            for(int j = 0;j < size;j++){
                int tmp = b[j];
                int k;
                for(k= j-d;k>=0&&++compTime&&b[k]>tmp;k-=d){
                    b[k+d] = b[k];swapTime++;
                }
                k+=d;
                b[k] = tmp;
            }
        }

        stop= time(&stop);
    }

    void print(){
        puts("3 Shell排序");
        puts("展示结果");
        for(int i =0;i<size;i++)printf("%d%c",b[i],i==size-1?‘\n‘:‘ ‘);
        printf("关键字比较次数:%d\n",compTime);
        printf("关键字排序次数:%d\n",swapTime);
        printf("用时:%ld ms\n",stop-start);
    }
    int swapTime;
    int compTime;
private:
    int * a;
    int * b;
    int size;
    time_t start,stop;

};

class bubbleSort{
public:
    bubbleSort(int * a,int size){
        this->a = a;
        this->size = size;
        b = new int[size];
    }
    ~bubbleSort(){
        delete b;
    }

    void sort(){
        for(int i =0;i<size;i++)b[i] =a[i];
        swapTime = 0;
        compTime = 0;
        start = time(&start);
        for(int i = 0;i<size; i++){
                for(int j = size - 1;j > i; j--){
                    compTime++;
                    if(b[j]<b[j-1]){
                            swap(b[j-1],b[j]);
                            swapTime+=3;
                    }
                }
        }
        stop= time(&stop);
    }

    void print(){
        puts("4 冒泡排序");
        puts("展示结果");
        for(int i =0;i<size;i++)printf("%d%c",b[i],i==size-1?‘\n‘:‘ ‘);
        printf("关键字比较次数:%d\n",compTime);
        printf("关键字排序次数:%d\n",swapTime);
        printf("用时:%ld ms\n",stop-start);
    }
    int swapTime;
    int compTime;
private:
    int * a;
    int * b;
    int size;
    time_t start,stop;

};

class quickSort{
public:
    quickSort(int * a,int size){
        this->a = a;
        this->size = size;
        b = new int[size];
    }
    ~quickSort(){
        delete b;
    }
    void partition2(int s,int t){
        int low = s,high = t;
        int key = b[s];
        while(low < high){
            while(low<high&&high--&&b[high] >= key){compTime++;}
            if(low==high)break;
            b[low] = b[high];
            swapTime++;
            while(low<high&&low++&&b[low]<=key){compTime++;}
            if(low==high)break;
            b[high] = b[low];
            swapTime++;
        }
        b[low] = key;
        if(low>s+1)partition2(s,low);
        if(low+1<t)partition2(low+1,t);
    }
    void sort(){
        for(int i =0;i<size;i++)b[i] =a[i];
        swapTime = 0;
        compTime = 0;
        start = time(&start);

        partition2(0,size);

        stop= time(&stop);
    }

    void print(){
        puts("5 快速排序");
        puts("展示结果");
        for(int i =0;i<size;i++)printf("%d%c",b[i],i==size-1?‘\n‘:‘ ‘);
        printf("关键字比较次数:%d\n",compTime);
        printf("关键字排序次数:%d\n",swapTime);
        printf("用时:%ld ms\n",stop-start);
    }
    int swapTime;
    int compTime;
private:
    int * a;
    int * b;
    int size;
    time_t start,stop;

};

class simpleSelectSort{
public:
    simpleSelectSort(int * a,int size){
        this->a = a;
        this->size = size;
        b = new int[size];
    }
    ~simpleSelectSort(){
        delete b;
    }
    void sort(){
        for(int i =0;i<size;i++)b[i] =a[i];
        swapTime = 0;
        compTime = 0;
        start = time(&start);

        for(int i=0;i<size;i++){
            int minind =i;
            for(int j=i+1;j<size&&++compTime;j++){
                if(b[minind]>b[j]){
                    minind = j;
                }
            }
            if(i!=minind){
                    swap(b[minind],b[i]);
                    swapTime+=3;
            }
        }

        stop= time(&stop);
    }

    void print(){
        puts("6 简单选择排序");
        puts("展示结果");
        for(int i =0;i<size;i++)printf("%d%c",b[i],i==size-1?‘\n‘:‘ ‘);
        printf("关键字比较次数:%d\n",compTime);
        printf("关键字排序次数:%d\n",swapTime);
        printf("用时:%ld ms\n",stop-start);
    }
    int swapTime;
    int compTime;
private:
    int * a;
    int * b;
    int size;
    time_t start,stop;

};

class heapSort{
public:
    heapSort(int * a,int size){
        this->a = a;
        this->size = size;
        b = new int[size];
    }
    ~heapSort(){
        delete b;
    }
    void heapAdjust(int i,int size){
        int lchild = 2*i;
        int rchild = 2*i+1;
        int mx = i;
        if(i<=size/2){
            if(lchild<size&&b[lchild]>b[mx]){
                mx = lchild;
                compTime++;
            }
            if(rchild<size&&b[rchild]>b[mx]){
                mx = rchild;
                compTime++;
            }
        }
        if(mx!=i){
            swapTime+=3;
            swap(b[mx],b[i]);
            heapAdjust(mx,size);
        }
    }
    void sort(){
        for(int i =0;i<size;i++)b[i] =a[i];
        swapTime = 0;
        compTime = 0;
        start = time(&start);
        for(int i = size/2;i>=0;i--){
            heapAdjust(i,size);
        }
        for(int i = size;i>0;i--){
            swap(b[0],b[i-1]);
            heapAdjust(0,i-1);
        }
        stop= time(&stop);
    }

    void print(){
        puts("7 堆排序");
        puts("展示结果");
        for(int i =0;i<size;i++)printf("%d%c",b[i],i==size-1?‘\n‘:‘ ‘);
        printf("关键字比较次数:%d\n",compTime);
        printf("关键字排序次数:%d\n",swapTime);
        printf("用时:%ld ms\n",stop-start);
    }
    int swapTime;
    int compTime;
private:
    int * a;
    int * b;
    int size;
    time_t start,stop;

};

class mergeSort{
public:
    mergeSort(int * a,int size){
        this->a = a;
        this->size = size;
        b = new int[size];
    }
    ~mergeSort(){
        delete b;
    }
    void merge(int s,int t){
        //printf("s %d t %d\n",s,t);
        int mid = (s+t)>>1;
        if(s+1<mid)merge(s,mid);
        if(t>mid+1)merge(mid,t);
        int* tmp = new int[t-s+1];
        int i = s,j = mid,k = s;
        while(k<t){
            if(i<mid&&j<t){
                compTime++;
                if(b[i]<b[j])tmp[k++-s]=b[i++];
                else  tmp[k++-s]=b[j++];
            }
            else if(i<mid){
                tmp[k++-s]=b[i++];
            }
            else{
                tmp[k++-s]=b[j++];
            }
            swapTime ++;
        }
        while(--k>=s){
                b[k]=tmp[k-s];swapTime++;
        }
      //  print();
        delete tmp;
    }
    void sort(){
        for(int i =0;i<size;i++)b[i] =a[i];
        swapTime = 0;
        compTime = 0;
        start = time(&start);

        merge(0,size);

        stop= time(&stop);
    }

    void print(){
        puts("8 归并排序");
        puts("展示结果");
        for(int i =0;i<size;i++)printf("%d%c",b[i],i==size-1?‘\n‘:‘ ‘);
        printf("关键字比较次数:%d\n",compTime);
        printf("关键字排序次数:%d\n",swapTime);
        printf("用时:%ld ms\n",stop-start);
    }
    int swapTime;
    int compTime;
private:
    int * a;
    int * b;
    int size;
    time_t start,stop;

};

int printInstruction(){
    puts("*************************************************");
    puts("0 退出");
    puts("1 直接显示结果");
    puts("2 比较八种不同排序");
    puts("3 重新生成数据");
    puts("*************************************************");
    int op;
    scanf("%d",&op);
    return op;
}
class generator{
    public:
    generator(){
        this->size = 200;
        this->bon = 200;
        a = new int[size];
        for(int i= 0;i<size;i++){
            a[i] = rand()*bon/RAND_MAX;
        }
    }
    generator(int size,int bon){
        this->size = size;
        this->bon = bon;
        a = new int[size];
        for(int i= 0;i<size;i++){
            a[i] = rand()*bon/RAND_MAX;
        }
    }
    ~generator(){
        delete a;
    }
    void print(){
        printf("容量:%d 生成数最大值:%d %p\n",size,bon,a);
        for(int i =0;i<size;i++)printf("%d%c",a[i],i==size-1?‘\n‘:‘ ‘);
    }
    int* a;
    int size;
    int bon;
};
void sortPrint(generator * g){
    insertSort * sort1=new insertSort(g->a,g->size);
    halfInsertSort * sort2 = new halfInsertSort(g->a,g->size);
    shellSort * sort3 = new shellSort(g->a,g->size);
    bubbleSort * sort4 = new bubbleSort(g->a,g->size);
    quickSort * sort5 = new quickSort(g->a,g->size);
    simpleSelectSort* sort6 = new simpleSelectSort(g->a,g->size);
    heapSort * sort7 = new heapSort(g->a,g->size);
    mergeSort * sort8 = new mergeSort(g->a,g->size);

    ofstream out("data.txt");
    sort1->sort();
    sort1->print();
    out<<"("<<sort1->compTime<<","<<sort1->swapTime<<") ";
    sort2->sort();
    sort2->print();
    out<<"("<<sort2->compTime<<","<<sort2->swapTime<<") ";
    sort3->sort();
    sort3->print();
    out<<"("<<sort3->compTime<<","<<sort3->swapTime<<") ";
    sort4->sort();
    sort4->print();
    out<<"("<<sort4->compTime<<","<<sort4->swapTime<<") ";
    sort5->sort();
    sort5->print();
    out<<"("<<sort5->compTime<<","<<sort5->swapTime<<") ";
    sort6->sort();
    sort6->print();
    out<<"("<<sort6->compTime<<","<<sort6->swapTime<<") ";
    sort7->sort();
    sort7->print();
    out<<"("<<sort7->compTime<<","<<sort7->swapTime<<") ";
    sort8->sort();
    sort8->print();
    out<<"("<<sort8->compTime<<","<<sort8->swapTime<<") ";
    out<<endl;

    delete sort1;
    delete sort2;
    delete sort3;
    delete sort4;
    delete sort5;
    delete sort6;
    delete sort7;
    delete sort8;
}
int main(){
    generator* g = new generator();

    int op;
    while((op=printInstruction())!=0){
        switch(op){
        case 1:
            g->print();
            break;
        case 2:
            sortPrint(g);
            break;
        case 3:
            delete g;
            int sz,bon;
            puts("请输入样本容量");
            scanf("%d",&sz);
            puts("请输入随机生成数最大值");
            scanf("%d",&bon);
            g = new generator(sz,bon);
            g->print();
            break;
        default:
            break;
        }
    }
    delete g;
    return 0;
}

  

时间: 2024-10-05 20:15:31

数据结构八种排序方法作业示例(无讲解)的相关文章

八种排序算法(内部排序)

八种排序算法很长时间没有使用了,今天做一个总结,方便以后自己用的时候参考. 这八种排序算法都是内部算法,这八种排序算法分别是: 1. 插入排序 1)直接插入排序 2)希尔排序 2.选择排序 1)简单选择排序 2)堆排序 3.交换排序 1)冒泡排序 2)快速排序 4.归并排序 5.基数排序 一.直接插入排序 将一个记录插入到已经排好序的有序表中,从而得到一个新的.记录数增1的有序表.在实际操作中,先将序列的第一个记录看成是一个有序的子序列,然后从第二个.第三个.……记录逐个进行插入,直至整个序列有

两种排序方法(直接判断)

题目描述 考拉有n个字符串字符串,任意两个字符串长度都是不同的.考拉最近学习到有两种字符串的排序方法: 1.根据字符串的字典序排序.例如:"car" < "carriage" < "cats" < "doggies < "koala"2.根据字符串的长度排序.例如:"car" < "cats" < "koala" <

八种排序算法

最近一段时间自己在研究各种排序算法,于是自己写了一个八种排序算法的集合: /************************************************************************* > Copyright (c)2014 stay hungry,stay foolish !!! > File Name: sort.cpp > Author: kanty > Mail: [email protected] > Created Time:

JAVA中运用数组的四种排序方法

JAVA中在运用数组进行排序功能时,一般有四种方法:快速排序法.冒泡法.选择排序法.插入排序法. 快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现. 冒泡法是运用遍历数组进行比较,通过不断的比较将最小值或者最大值一个一个的遍历出来. 选择排序法是将数组的第一个数据作为最大或者最小的值,然后通过比较循环,输出有序的数组. 插入排序是选择一个数组中的数据,通过不断的插入比较最后进行排序.下面我就将他们的实现方法一一详解供大家参考. <1>利用Arrays带有的排序方法快

两种排序方法 网易2017内推编程题

考拉有n个字符串字符串,任意两个字符串长度都是不同的.考拉最近学习到有两种字符串的排序方法: 1.根据字符串的字典序排序.例如: "car" < "carriage" < "cats" < "doggies < "koala" 2.根据字符串的长度排序.例如: "car" < "cats" < "koala" < &

WordPress慢的八种解决方法(用排查法解决)

WordPress的打开速度慢会影响到用户体验和关键词的稳定排名,WordPress为什么加载慢呢?其实很简单的,就是WordPress水土不服,用WordPress的大家都知道,WordPress是外国人开发的,在国内用肯定会卡的,今天笔者给大家整理了几个解决各种WordPress慢的慢的问题,希望能够帮助到大家! 方法一:删除不必要的插件 什么叫不必要的插件呢?就是能用代码解决的就用代码不用插件,不是非常需要的功能类的插件,美化网站的各种插件,常见的久这两种. 方法二:关闭主题的谷歌字体 这

直观感受7种排序方法

1. 快速排序 介绍: 快速排序是由东尼·霍尔所发展的一种排序算法.在平均状况下,排序 n 个项目要Ο(n log n)次比较.在最坏状况下则需要Ο(n2)次比较,但这种状况并不常见.事实上,快速排序通常明显比其他Ο(n log n) 算法更快,因为它的内部循环(inner loop)可以在大部分的架构上很有效率地被实现出来,且在大部分真实世界的数据,可以决定设计的选择,减少所需时间的二次方项之可能性. 步骤: 从数列中挑出一个元素,称为 “基准”(pivot), 重新排序数列,所有元素比基准值

c#实现几种排序方法

插入排序 1.简介 插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间. 2.算法描述 一般来说,插入排序都采用in-place在数组上实现.具体算法描述如下:1.从第一个元素开始,该元素可以认为已经被排序2.

PHP的几种排序方法

<?php /** * PHP最常用的四个排序方法及二种查找方法 * 下面的排序方法全部都通过测试 * auther : soulence * date : 2015/06/20 */ //PHP冒泡排序法 function bubbleSort(&$arr){ //这是一个中间变量 $temp=0; //我们要把数组,从小到大排序 //外层循环 $flag=false;//这个优化之后效率会很高,一般够用 for($i=0;$i<count($arr)-1;$i++){ for($j