我的C++一直比较水,平时都用C写程序,今天想写一个排序类模板,练习一下。
希尔排序,快速排序,归并排序,选择排序,冒泡排序,堆排序,插入排序
再添加一个二分法查找吧。
这些代码只经过了简单的测试。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
template <class Type>
class sort {
public:
sort(int (*cmp)(Type t1, Type t2));
~sort() {}
void adjust_heap(Type *root, int i, int size);
void create_heap(Type *root, int size);
void heap_sort(Type *root, int size);
void select_sort(Type *root, int size);
void shell_sort(Type *root, int size);
void quick_sort(Type *root, int l ,int r);
int partition(Type *root, int l, int r);
void merge_sort(Type *root, int l, int r);
void merge(Type *root, int l, int mid, int r);
void bubble_sort(Type *root, int size);
void insert_sort(Type *root, int size);
Type *binary_search(Type *root, int size, Type *s);
private:
int (*compare)(Type t1, Type t2);
};
template <class Type>
Type * sort<T

本文介绍了作者在C++中创建排序类模板的实践过程,包括希尔排序、快速排序、归并排序、选择排序、冒泡排序、堆排序和插入排序的实现,并提及了二分查找的添加。代码经过了初步测试。
最低0.47元/天 解锁文章
4140

被折叠的 条评论
为什么被折叠?



