Skip to content

Instantly share code, notes, and snippets.

@kazh98
Created August 9, 2011 09:50
Show Gist options
  • Save kazh98/1133687 to your computer and use it in GitHub Desktop.
Save kazh98/1133687 to your computer and use it in GitHub Desktop.
Gnome Sort
/* Gnome Sort [License: CC-BY-SA]
* - Copyright(C) 2011 Kazh. All Rights Reserved.
*/
#ifndef __INC_GNOMESORTH
#define __INC_GNOMESORTH
#include <iterator>
#include <algorithm>
/** Sorts the array by Gnome 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 gnomeSort (
iter begin,
iter end
)
{
for ( iter p = begin; p != end; ++p )
{
for ( iter q = p; q != begin; --q )
{
if ( *q < *( q - 1 ) )
{
std::iter_swap ( q, q - 1 );
}
else
{
break ;
}
}
}
return ;
}
#endif /* __INC_GNOMESORTH */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment