排序算法通用头文件——template_head.h

排序算法C++实现的通用头文件

// template_head.h
#pragma once
//#include "../sort_algorithms/BinaryHeap.tpp"
#include <vector>
#include <string>

template <typename Container>
void printList(Container& list) 
{
	for (auto &x : list) {
		std::cout << x << " ";
	}
	std::cout << std::endl;
}

template <typename Iterator>
void printList(const Iterator& begin, const Iterator& end)
{
	for (Iterator i = begin; i != end; i++) {
		std::cout << *i << " ";
	}
	std::cout << std::endl;
}


template <class T> struct greater_
{
	bool operator () (const T& x, const T& y) const { return x > y; }
	typedef T first_argument_type;
	typedef T secont_argument_type;
	typedef bool result_type;
};

template <class T> struct less_
{
	bool operator () (const T& x, const T& y) const { return x < y; }
	typedef T first_argument_type;
	typedef T secont_argument_type;
	typedef bool result_type;
};

/* 插入排序 */
template <typename Iterator>
void insertionSort(const Iterator& begin, const Iterator& end);

template <typename Iterator, typename Comparator>
void insertionSort(const Iterator& begin, const Iterator& end, Comparator lessThan);


/* 希尔排序 */
template <typename Iterator>
void shellSort(const Iterator& begin, const Iterator& end);

template <typename Iterator, typename Comparator>
void shellSort(const Iterator& begin, const Iterator& end, Comparator lessThan);

/* 二叉堆 */
//template <typename Comparable>
//class BinaryHeap
//{
//public:
//	explicit BinaryHeap(int capacity = 100);
//	explicit BinaryHeap(const vector<Comparable>& items);
//
//	bool isEmpty() const;
//	const Comparable& findMin() const;
//
//	void insert(const Comparable& x);
//	void insert(Comparable&& x);
//
//	void deleteMin();
//	void deleteMin(Comparable& minItem);
//	void makeEmpty();
//private:
//	int currentSize;
//	vector<Comparable> arr;
//
//	void buildHeap();
//	void percolateDown(int hole);   // 过滤向下
//
//};
//
//template <typename Comparable>
//BinaryHeap<Comparable>::BinaryHeap(int capacity) {
//	ca
//}

/* 归并排序 */
template <typename Iterator>
void mergeSort(const Iterator& begin, const Iterator& end);

template <typename Iterator, typename Comparator>
void mergeSort(const Iterator& begin, const Iterator& end, Comparator lessThan);

template <typename Iterator, typename Comparator>
void merge(const Iterator& begin, const Iterator& mid, const Iterator& end, Comparator lessThan);

/* 快速排序 */
template <typename Iterator>
void quickSort(const Iterator& begin, const Iterator& end);

template <typename Iterator, typename Comparator>
void quickSort(const Iterator& begin, const Iterator& end, Comparator lessThan);

template <typename Iterator, typename Comparator>
Iterator swapLeftRight(const Iterator& begin, const Iterator& end, Comparator lessThan);

/* 桶排序 */
template <typename Iterator>
void bucketSort(const Iterator& begin, const Iterator& end);

template <typename Iterator, typename Comparator>
void bucketSort(const Iterator& begin, const Iterator& end, Comparator lessThan);

/* 基数排序 */
void radixSort(int arr[], int length);

/* 堆排序 */
template <typename T>
void heapSort(const T& arr);

template <typename Iterator>
void heapSort(const Iterator& begin, const Iterator& end);

template <typename Iterator, typename Comparator>
void heapSort(const Iterator& begin, const Iterator& end, Comparator lessThan);

/* 冒泡排序 */
template <typename Iterator>
void bubbleSort(const Iterator& begin, const Iterator& end);

template <typename Iterator, typename Comparator>
void bubbleSort(const Iterator& begin, const Iterator& end, Comparator lessThan);

/* 插入排序 */
template <typename T>
//void selectionSort(const vector<int>& list);

template <typename Iterator>
void selectionSort(const Iterator& begin, const Iterator& end);

template <typename Iterator, typename Comparator>
void selectionSort(const Iterator& begin, const Iterator& end, Comparator lessThan);







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值