## Posts

Showing posts from 2017

### Pancake Sorting using C++(STL)

This is a variation of selection sort, it bring the largest pancake not yet sorted to the top with one flip; take it down to its final position with one more flip; and repeat this process for the remaining pancakes. The algorithm has been discussed in following wiki link: Pancake Sorting The algorithm is visually represented in following link: Pancake Sorting(Visualization) The idea of this sorting is similar to Selection Sort, here taking maximum items one by one and started placing those from last and at the same time reducing the vector size one by one. To place bigger items in order from last using flips two move to their respective positions. Here is the implementation using std::rotate() and std::reverse(). The output:

### Insertion Sort (C++ Way) Using Standard Template Library

This is an effort to implement insertion sort in C++ way using Standard Template Library. The below implementation is for vector of any type. Two STL libraries algorithm has been used. 1. std::lower_bound 2. std::rotate The implementation cross compiler compatible, tested on GNU C++ (5.4.0) and Microsoft C++ (2010). //g++  5.4.0 #include < iostream > #include < algorithm > #include < vector > template< typename T > void insertionSort(std::vector< T > &vec) { typedef typename std::vector< T >::value_type vt; typename std::vector< vt >::iterator itBegin = vec.begin(); typename std::vector< vt >::iterator itEnd = vec.end(); for(typename std::vector< vt >::iterator it = itBegin; it != itEnd; ++it) { typename std::vector ::iterator ins = std::lower_bound(itBegin, it, *it); std::rotate(ins, it, std::next(it)); } } int main() {     int arr[] = {34, 20, 99, 10, 23}; std::vector< int > v(s

### Selection Sort (C++ way) for vector of any type

In my last article have seen how we can leverage C++ STL to do selection sort on the vector of integers only. However, the selection sort wasn't very generic to accept vectors of any basic type and sort accordingly. The below program was an effort to make the same selection sort (ascending order) implementation for vectors of any type like vectors of integers, doubles, chars, etc. #include < iostream > #include < algorithm > #include < vector > template < class ForwardIterator1, class ForwardIterator2 > void iters_swap (ForwardIterator1 a, ForwardIterator2 b) {     std::swap (*a, *b); } template < typename T > void selectionSort(std::vector< T > &v) { // Selection sort typedef typename std::vector ::value_type vt; typename std::vector ::iterator it = v.begin(); while(it != v.end()) {     typename std::vector ::iterator i_Min = std::min_element (it, v.end()); iters_swap(i_Min, it); ++it; } } int main() {

### Selection Sort (C++ Way)

Selection Sort using Standard Template Libraries (not C++ 11): Selection sort works by finding smallest element from the list unsorted list, swap with leftmost element and proceed until all the elements are in order. Selecting the lowest element requires scanning all n elements (this takes n − 1 comparisons) and then swapping it into the first position. Finding the next lowest element requires scanning the remaining n − 1 elements and so on. Hence the complexity is  O(N^2) . The C++ implementation is like below: #include < iostream > #include < algorithm > #include < vector > template < class ForwardIterator1, class ForwardIterator2 > void iters_swap (ForwardIterator1 a, ForwardIterator2 b) {      std::swap (*a, *b); } template < typename T > void selectionSort(T &v) { // Selection sort std::vector ::iterator it = v.begin(); while(it != v.end()) {         std::vector ::iterator i_Min = std::min_element(it,