linux c++ 快排,堆排序,插入排序时延对比

Posted fulianzhou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux c++ 快排,堆排序,插入排序时延对比相关的知识,希望对你有一定的参考价值。

heap.h

#ifndef _HEAP_H_
#define _HEAP_H_
#include <vector>

class MaxHeap

private:
    void heapAdj(std::vector<int> &num, int i, int len);

public:
    void heapSort(std::vector<int> &nums);
;

class MinHeap

private:
    void heapAdj(std::vector<int> &nums, int i, int len);
public:
    void heapSort(std::vector<int> &nums);
;


#endif

heap.cpp

#include "heap.h"
using namespace std;

void swap(int &a, int &b)

    int tmp = a;
    a = b;
    b = tmp;


void MaxHeap::heapAdj(vector<int>& nums, int i, int len)

    int j = i * 2 + 1;
    while(j < len)
    
        if(j + 1 < len && nums[j + 1] > nums[j])
        
            j++;
        

        if(nums[i] > nums[j])
        
            return;
        
        else
        
            swap(nums[i], nums[j]);
            i = j;
            j = j * 2 + 1;
        
    


void MaxHeap::heapSort(vector<int>& nums)

    int n = nums.size();
    // build heap
    for(int i = n / 2 - 1;i >= 0; i--)
    
        heapAdj(nums, i, n);
    

    for(int i = n - 1; i > 0; i--)
    
        swap(nums[0], nums[i]);
        heapAdj(nums, 0, i);
    


void MinHeap::heapAdj(vector<int> &nums, int i, int len)

    int j = i * 2 + 1;
    while(j < len)
    
        if(j + 1 < len && nums[j+1] < nums[j])
        
            j++;
        

        if(nums[i] < nums[j])
        
            break;
        
        else
        
            swap(nums[i], nums[j]);
            i = j;
            j = i * 2 + 1;
        
    


void MinHeap::heapSort(vector<int> &nums)

    int n = nums.size();

    for(int i = n / 2 - 1; i >= 0; i--)
    
        heapAdj(nums, i, n);
    

    for(int i = n - 1; i > 0; i--)
    
        swap(nums[i], nums[0]);
        heapAdj(nums, 0, i);
    


sort.cpp

#include <stdio.h>
#include <vector>
#include <time.h>
#include <stdlib.h>
#include <sys/time.h>
#include "heap.h"
#include <stdint.h>
using namespace std;

void showData(vector<int> nums)

    for(int i=0;i<nums.size();i++)
    
        printf("%d ", nums[i]);
    
    printf("\\n");


void genData(vector<int>& nums, int cnt)

    for(int i = 0; i < cnt; i++)
    
        nums.push_back(rand()%cnt);
    


void insertSort(vector<int>& nums)

    int n = nums.size();

    for(int i = 0;i < n; i++)
    
        int p = i;
        for(int j=i;j<n;j++)
        
            if(nums[j] < nums[p]) p = j;
        

        if(p != i)
        
            swap(nums[i], nums[p]);
        
    


void quickSort(vector<int>& nums, int left, int right)

    if(left >= right)
    
        return;
    

    int pivot = nums[left];
    int l = left;
    int r = right;

    while(l < r)
    while(l < r)
    
        while(l < r && nums[r] >= pivot) r--;
        nums[l] = nums[r];

        while(l < r && nums[l] <= pivot) l++;
        nums[r] = nums[l];
    

    nums[l] = pivot;

    quickSort(nums, left, l - 1);
    quickSort(nums, r + 1, right);


uint64_t getTimeNs()

/*
struct timespec 
    time_t   tv_sec;
    long     tv_nsec;
;
*/
    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);
    uint64_t timestamp = ts.tv_sec * 1000000 + ts.tv_nsec;

    return timestamp;


void sortTest(int cnt)

    MaxHeap maxheap;
    MinHeap minheap;

    uint64_t start_time = 0;
    uint64_t end_time = 0;

    uint64_t sum1 = 0;
    uint64_t sum2 = 0;
    uint64_t sum3 = 0;

    for(int i=0;i<cnt;i++)
    
        vector<int> nums1;
        vector<int> nums2;
        vector<int> nums3;
        genData(nums1, 20);
        nums2 = nums1;
        nums3 = nums1;

        start_time = getTimeNs();
        quickSort(nums1, 0, nums1.size() - 1);
        end_time = getTimeNs();
        sum1 += end_time - start_time;


        start_time = getTimeNs();
        maxheap.heapSort(nums2);
        //minheap.heapSort(nums2);
        end_time = getTimeNs();
        sum2 += end_time - start_time;

        start_time = getTimeNs();
        insertSort(nums3);
        end_time = getTimeNs();
        sum3 += end_time - start_time;
    

    printf("快排平均时延:%ld\\n", sum1/cnt);
    printf("堆排序平均时延:%ld\\n", sum2/cnt);
    printf("插入排序平均时延:%ld\\n", sum3/cnt);



int main()

    srand(time(0));

    sortTest(1000);

    return 0;


Makefile

CC=g++
SRC=$(wildcard *.cpp)
OBJ=$(patsubst %.cpp, %.o, $(SRC))
TARGET=sort
all:$TARGET

$TARGET:$OBJ
    $CC -o $@ $^

%.o:%.cpp
    $(CC) -c $<

clean:
    rm $OBJ $TARGET -f

测试结果:

结论:
快排 < 堆排序 < 插入排序

以上是关于linux c++ 快排,堆排序,插入排序时延对比的主要内容,如果未能解决你的问题,请参考以下文章

链表排序(冒泡选择插入快排归并希尔堆排序)

排序算法复习:直接插入排序堆排序快排冒泡排序

希尔堆快排

你所知道的十大排序算法的总结(冒泡,选择,插入,希尔,归并,快排,堆排序,计数排序,桶排序,基数排序)

基本排序算法(冒泡插入选择快排)

c++堆排序