Skip to content

Instantly share code, notes, and snippets.

@kazh98
Created July 31, 2011 17:07
Show Gist options
  • Save kazh98/1116972 to your computer and use it in GitHub Desktop.
Save kazh98/1116972 to your computer and use it in GitHub Desktop.
Bubble Sort
/* 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