make_head and sort_head
#include <iostream>
#include <algorithm>
#include <vector>
#include "log/logx.h"
namespace {
template<class RandomAccessIterator>
void displayData(RandomAccessIterator _begin, RandomAccessIterator _end) {
while (_begin != _end) {
std::cout << *_begin << " ";
_begin++;
}
std::cout << std::endl;
}
}
namespace TSTL {
template<class RandomAccessIterator, class Compare>
static void headAdjuest(RandomAccessIterator first, RandomAccessIterator last,RandomAccessIterator index , Compare comp) {
using distance_type = typename std::iterator_traits<RandomAccessIterator>::difference_type;
//RandomAccessIterator::difference_type
distance_type target_index = index - first;
distance_type child_left = target_index * 2 + 1;
distance_type child_right = target_index * 2 + 2;
distance_type all_len = last-first;
if (child_left < all_len && (comp(*(first + target_index), *(first + child_left)))) target_index = child_left;
if (child_right < all_len && (comp(*(first + target_index), *(first + child_right)))) target_index = child_right;
if ((first + target_index) != index) {
std::swap(*(first + target_index), *index);
headAdjuest(first, last, first+target_index, comp);
}
}
template <class RandomAccessIterator>
void make_heap(RandomAccessIterator first, RandomAccessIterator last) {
make_heap(first, last,std::less());
}
template <class RandomAccessIterator, class Compare>
void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
const auto range = last - first;
for (auto cur = first + range / 2 - 1; cur >= first; --cur) {
headAdjuest(first, last, cur, comp);
if (cur == first) return;
}
}
template <class RandomAccessIterator, class Compare>
void sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
TSTL::make_heap(first, last, comp);
using distance_type = typename std::iterator_traits<RandomAccessIterator>::difference_type;
distance_type all_len = last - first;
for (auto i= all_len - 1; i !=0 ; i--){
std::swap(*(first + i),*(first+0));
headAdjuest(first, first + i, first,comp);
}
}
}
int main(int argc, char** argv) {
std::vector<int> vec2{ 1,4,2,1,5678,123,34,53,123,4,5 };
LOGXA("make heap");
std::vector<int> vec=vec2;
LOGXT("old vec");
displayData(vec.begin(), vec.end());
TSTL::make_heap(vec.begin(), vec.end(), [](int a, int b) {return (a > b); });
LOGXT("min head");
displayData(vec.begin(), vec.end());
vec = vec2;
LOGXT("max head");
TSTL::make_heap(vec.begin(), vec.end(), [](int a, int b) {return (a < b); });
displayData(vec.begin(), vec.end());
LOGXA("sort heap");
vec = vec2;
LOGXT("old vec");
displayData(vec.begin(), vec.end());
TSTL::sort_heap(vec.begin(), vec.end(), [](int a, int b) {return (a < b); });
LOGXT("min sort");
displayData(vec.begin(), vec.end());
vec = vec2;
TSTL::sort_heap(vec.begin(), vec.end(), [](int a, int b) {return (a > b); });
LOGXT("max sort");
displayData(vec.begin(), vec.end());
//TinySTL::make_heap(vec.begin(), vec.end());
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dce43d8de4107693faae46ba77775aad.png)
sort
bubble_sort
#include <iostream>
#include <algorithm>
#include <vector>
#include "log/logx.h"
namespace {
template<class RandomAccessIterator>
void displayData(RandomAccessIterator _begin, RandomAccessIterator _end) {
while (_begin != _end) {
std::cout << *_begin << " ";
_begin++;
}
std::cout << std::endl;
}
}
namespace TSTL {
template<class RandomIterator, class BinaryPredicate>
void bubble_sort(RandomIterator first, RandomIterator last, BinaryPredicate pred) {
auto len = last - first;
for (auto i = len; i != 0; --i) {
bool swaped = false;
for (auto p = first; p != (first + i - 1); ++p) {
if (pred(*(p + 1), *p)) {
std::swap(*(p + 1), *p);
swaped = true;
}
}
if (!swaped)
break;
}
}
}
int main(int argc, char** argv) {
std::vector<int> vec2{ 1,4,2,1,5678,123,34,53,123,4,5 };
std::vector<int> vec=vec2;
LOGXT("old vec");
displayData(vec.begin(), vec.end());
TSTL::bubble_sort(vec.begin(), vec.end(), std::less());
displayData(vec.begin(), vec.end());
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d21438a30629dfda4394bc97e2a98f83.png)
heap_sort
template<class RandomAccessIterator, class Compare>
static void headAdjuest(RandomAccessIterator first, RandomAccessIterator last,RandomAccessIterator index , Compare comp) {
using distance_type = typename RandomAccessIterator::difference_type;
distance_type target_index = index - first;
distance_type child_left = target_index * 2 + 1;
distance_type child_right = target_index * 2 + 2;
distance_type all_len = last-first;
if (child_left < all_len && (comp(*(first + target_index), *(first + child_left)))) target_index = child_left;
if (child_right < all_len && (comp(*(first + target_index), *(first + child_right)))) target_index = child_right;
if ((first + target_index) != index) {
std::swap(*(first + target_index), *index);
headAdjuest(first, last, first+target_index, comp);
}
}
template <class RandomAccessIterator>
void make_heap(RandomAccessIterator first, RandomAccessIterator last) {
make_heap(first, last,std::less());
}
template <class RandomAccessIterator, class Compare>
void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
const auto range = last - first;
for (auto cur = first + range / 2 - 1; cur >= first; --cur) {
headAdjuest(first, last, cur, comp);
if (cur == first) return;
}
}
template <class RandomAccessIterator, class Compare>
void sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
TSTL::make_heap(first, last, comp);
using distance_type = typename std::iterator_traits<RandomAccessIterator>::difference_type;
distance_type all_len = last - first;
for (auto i= all_len - 1; i !=0 ; i--){
std::swap(*(first + i),*(first+0));
headAdjuest(first, first + i, first,comp);
}
}
template <class RandomAccessIterator, class Compare>
void sort_heap2(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
TSTL::make_heap(first, last, comp);
auto all_len = last - first;
for (auto i = all_len ; i != 1; i--) {
pop_heap(first, (first + i), comp);
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ff4e5310fee3e8d4007b01716217eb4f.png)
select_sort
template <class RandomAccessIterator, class Compare>
void sort_select(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
auto all_len = last - first;
for (decltype(all_len) i = 0; i <all_len-1; i++) {
decltype(all_len) target_index = i;
for (decltype(all_len)j = i+1; j < all_len; j++) {
if (comp(*(first + j), *(first + target_index)))target_index = j;
}
if (target_index != i)std::swap(*(first + target_index), *(first + i));
}
}
insert_sort
template <class RandomAccessIterator, class Compare>
void sort_insert(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
auto all_len = last - first;
decltype(all_len) j = 0;
for (decltype(all_len) i = 1; i < all_len ; i++) {
auto insert_data = *(first+i);
j = i - 1;
for (; j >= 0 && comp(insert_data ,*(first + j)); j--) {
*(first + (j + 1))=*(first+j);
}
*(first + (j+1) ) = insert_data;
}
}