Created
July 31, 2011 17:07
-
-
Save kazh98/1116972 to your computer and use it in GitHub Desktop.
Bubble Sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Bubble Sort [License: CC-BY-SA] | |
* - Copyright(C) 2011 Kazh. All Rights Reserved. | |
*/ | |
#ifndef __INC_BUBBLESORTH | |
#define __INC_BUBBLESORTH | |
#include <algorithm> | |
/** Sorts an array by Bubble Sort Algorithm | |
* @tparam iter Random Access Iterator to access the array to sort | |
* @param[in,out] begin Pointer to head of the array to sort | |
* @param[in,out] end Pointer to tail of the array to sort | |
*/ | |
template < | |
typename iter | |
> | |
void bubbleSort ( | |
iter begin, | |
iter end | |
) | |
{ | |
iter p, q; | |
for ( q = end; q > begin; --q ) | |
{ | |
for ( p = begin + 1; p < q; ++p ) | |
{ | |
if ( *( p - 1 ) <= *p ) continue ; | |
std::iter_swap ( p - 1, p ); | |
} | |
} | |
return ; | |
} | |
#endif /* __INC_BUBBLESORTH */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment