Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
(Added the implementation for Dart. This was translated from the Java version of the algorithm) |
m (some comments) |
||
Line 1,146: | Line 1,146: | ||
#include <iterator> |
#include <iterator> |
||
// std::rotate is used to shift the sub-region |
|||
// if the predicate p is true |
|||
template <typename RandomAccessIterator, typename Predicate> |
template <typename RandomAccessIterator, typename Predicate> |
||
void insertion_sort(RandomAccessIterator begin, RandomAccessIterator end, |
void insertion_sort(RandomAccessIterator begin, RandomAccessIterator end, |
||
Line 1,154: | Line 1,156: | ||
} |
} |
||
// calls with default Predicate std::less (sort ascending) |
|||
template <typename RandomAccessIterator> |
template <typename RandomAccessIterator> |
||
void insertion_sort(RandomAccessIterator begin, RandomAccessIterator end) { |
void insertion_sort(RandomAccessIterator begin, RandomAccessIterator end) { |
||
⚫ | |||
insertion_sort( |
|||
begin, end, |
|||
std::less< |
|||
⚫ | |||
} |
} |
||
int main() { |
int main() { |
||
int a[] = { 100, 2, 56, 200, -52, 3, 99, 33, 177, -199 }; |
int a[] = { 100, 2, 56, 200, -52, 3, 99, 33, 177, -199 }; |
||
insertion_sort(std::begin(a), std::end(a)); |
insertion_sort(std::begin(a), std::end(a)); |
||
// 'iterates' numbers to std::cout |
|||
// converts ints to strings for output to screen |
|||
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " ")); |
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " ")); |
||
std::cout << "\n"; |
|||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |