快速排序Makefile版

quick_sort_test.cpp
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <quick_sort.h>
#include <test.h>
using namespace std;

int main() {
    srand(time(0));
    #define MAX_N 10000000
    int *arr = getRandData(MAX_N);
    TEST(quick_sort_v1, arr, MAX_N);
    #undef MAX_N
    return 0;
}
quick_sort.cc

void quick_sort_v1(int *arr, int l, int r) {
    if (l >= r) return ;
    int x = l, y = r, z = arr[l];
    // partition
    while (x < y) {
        while (x < y && arr[y] >= z) --y;
        if (x < y) arr[x++] = arr[y];
        while (x < y && arr[x] <= z) ++x;
        if (x < y) arr[y--] = arr[x];
    }
    arr[x] = z;
    quick_sort_v1(arr, l, x - 1);
    quick_sort_v1(arr, x + 1, r);
    return ;
}
quick_sort.h
#ifndef _QUICK_SORT_H
#define _QUICK_SORT_H

void quick_sort_v1(int *arr, int l, int r);

#endif
Makefile
all:
	g++ -I./ quick_sort_test.cpp quick_sort.cc
/*************************************************************************
	> File Name: test.h
	> Author: huguang
	> Mail: hug@haizeix.com
	> Created Time: 三  8/26 15:30:44 2020
 ************************************************************************/

#ifndef _TEST_H
#define _TEST_H

#define COLOR(msg, code) "\033[1;" #code "m" msg "\033[0m"
#define RED(msg) COLOR(msg, 31)
#define GREEN(msg) COLOR(msg, 32)
#define YELLOW(msg) COLOR(msg, 33)
#define BLUE(msg) COLOR(msg, 34)

#define TEST(func, arr, n) test_func(#func, __FILE__, __LINE__, func, arr, n)

int *getRandData(int n) {
    int *arr = (int *)malloc(sizeof(int) * n);
    for (int i = 0; i < n; i++) arr[i] = rand() % n;
    return arr;
}

bool check(int *arr, int n) {
    for (int i = 1; i < n; i++) {
        if (arr[i] < arr[i - 1]) return false;
    }
     return true;
}

void test_func(
    const char *func_name,
    const char *file_name,
    int line_no,
    void (*func)(int *, int, int), 
    int *arr,
    int n 
) {
    int *temp = (int *)malloc(sizeof(int) * n);
    for (int i = 0; i < n; i++) temp[i] = arr[i];
    long long b = clock();
    func(temp, 0, n - 1);
    long long e = clock();
    if (check(temp, n)) {
        printf(GREEN("[    OK    ]") " %s", func_name);
    } else {
        printf(RED("[  FAILED  ]") " %s", func_name);
    }
    printf(YELLOW(" (%lld ms)\n"), 1000 * (e - b) / CLOCKS_PER_SEC);
    free(temp);
    return ;
}

#endif

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值