完成sort
This commit is contained in:
@@ -457,8 +457,7 @@ namespace TinySTL{
|
|||||||
while (n++){
|
while (n++){
|
||||||
--it;
|
--it;
|
||||||
}
|
}
|
||||||
}
|
}else{
|
||||||
else{
|
|
||||||
while (n--){
|
while (n--){
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
@@ -478,6 +477,62 @@ namespace TinySTL{
|
|||||||
typedef iterator_traits<InputIterator>::iterator_category iterator_category;
|
typedef iterator_traits<InputIterator>::iterator_category iterator_category;
|
||||||
_advance(it, n, iterator_category());
|
_advance(it, n, iterator_category());
|
||||||
}
|
}
|
||||||
|
//********** [sort] ******************************
|
||||||
|
//********* [Algorithm Complexity: O(NlogN)] ****************
|
||||||
|
namespace {
|
||||||
|
template<class RandomIterator, class BinaryPredicate>
|
||||||
|
typename iterator_traits<RandomIterator>::value_type
|
||||||
|
mid3(RandomIterator first, RandomIterator last, BinaryPredicate pred){//[first, last]
|
||||||
|
auto mid = first + (last + 1 - first) / 2;
|
||||||
|
if (pred(*mid, *first)){
|
||||||
|
swap(*mid, *first);
|
||||||
|
}
|
||||||
|
if (pred(*last, *mid)){
|
||||||
|
swap(*last, *mid);
|
||||||
|
}
|
||||||
|
if (pred(*last, *first)){
|
||||||
|
swap(*last, *first);
|
||||||
|
}
|
||||||
|
auto ret = *mid;
|
||||||
|
swap(*mid, *(last - 1));//<2F><>mid item<65><6D>λ<EFBFBD><CEBB>Ϊ<EFBFBD>ڱ<EFBFBD>
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
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){
|
||||||
|
for (auto p = first; p != (first + i - 1); ++p){
|
||||||
|
if (pred(*(p + 1), *p))
|
||||||
|
swap(*(p + 1), *p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template<class RandomIterator>
|
||||||
|
void sort(RandomIterator first, RandomIterator last){
|
||||||
|
return sort(first, last, less<typename iterator_traits<RandomIterator>::value_type>());
|
||||||
|
}
|
||||||
|
template<class RandomIterator, class BinaryPredicate>
|
||||||
|
void sort(RandomIterator first, RandomIterator last, BinaryPredicate pred){
|
||||||
|
static int n = 1;
|
||||||
|
if (first >= last || first + 1 == last)
|
||||||
|
return;
|
||||||
|
if (last - first <= 20)//<2F><><EFBFBD>䳤<EFBFBD><E4B3A4>С<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>20<32>IJ<EFBFBD><C4B2><EFBFBD>ð<EFBFBD><C3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
return bubble_sort(first, last, pred);
|
||||||
|
auto mid = mid3(first, last - 1, pred);
|
||||||
|
auto p1 = first, p2 = last - 2;
|
||||||
|
while (p1 < p2){
|
||||||
|
while (pred(*p1, mid) && p1 < p2) ++p1;
|
||||||
|
while (!pred(*p2, mid) && p1 < p2) --p2;
|
||||||
|
if (p1 < p2){
|
||||||
|
swap(*p1, *p2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
swap(*p1, *(last - 2));//<2F><><EFBFBD><EFBFBD>Ϊ<EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD>mid item<65><6D><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
||||||
|
sort(first, p1);
|
||||||
|
sort(p1 + 1, last);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -189,6 +189,29 @@ namespace TinySTL{
|
|||||||
TinySTL::advance(lit, -5);
|
TinySTL::advance(lit, -5);
|
||||||
assert(*vit == 0 && *lit == 0);
|
assert(*vit == 0 && *lit == 0);
|
||||||
}
|
}
|
||||||
|
void testSort(){
|
||||||
|
int arr1[1] = { 0 };
|
||||||
|
TinySTL::sort(std::begin(arr1), std::end(arr1));
|
||||||
|
assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
|
||||||
|
|
||||||
|
int arr2[2] = { 1, 0 };
|
||||||
|
TinySTL::sort(std::begin(arr2), std::end(arr2));
|
||||||
|
assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
|
||||||
|
|
||||||
|
int arr3[3] = { 2, 1, 3 };
|
||||||
|
TinySTL::sort(std::begin(arr3), std::end(arr3));
|
||||||
|
assert(std::is_sorted(std::begin(arr3), std::end(arr3)));
|
||||||
|
|
||||||
|
int arr4[10000];
|
||||||
|
std::random_device rd;
|
||||||
|
for (auto i = 0; i != 100; ++i){
|
||||||
|
for (auto& n : arr4){
|
||||||
|
n = rd() % 65536;
|
||||||
|
}
|
||||||
|
TinySTL::sort(std::begin(arr4), std::end(arr4));
|
||||||
|
assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void testAllCases(){
|
void testAllCases(){
|
||||||
@@ -211,6 +234,7 @@ namespace TinySTL{
|
|||||||
testIsPermutation();
|
testIsPermutation();
|
||||||
testSearch();
|
testSearch();
|
||||||
testAdvance();
|
testAdvance();
|
||||||
|
testSort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <random>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "../BinarySearchTree.h"
|
#include "../BinarySearchTree.h"
|
||||||
@@ -39,6 +40,7 @@ namespace TinySTL{
|
|||||||
void testIsPermutation();
|
void testIsPermutation();
|
||||||
void testSearch();
|
void testSearch();
|
||||||
void testAdvance();
|
void testAdvance();
|
||||||
|
void testSort();
|
||||||
|
|
||||||
void testAllCases();
|
void testAllCases();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user