You know it’s an easy algorithm to program when you’re done writing it, before the teacher is done explaining it…

#include "GSort.h"
 
void	GSort::sort( int numbers[], int size )
{
	int i = 0;
	int previous, current;
	while( true )
	{
		if( i == 0 ) //No previous, step forward.
		{
			i++;
			continue;
		}
		else if( i == size ) //No next, break out.
		{
			break;
		}
		else
		{
			previous = numbers[i - 1];
			current = numbers[i];
			if( previous < = current )
			{
				i++;
			}
			else if( previous >= current )
			{
				swap( numbers[i-1], numbers[i] );
				i--;
			}
		}
	}
}
 
void	GSort::swap( int &previous, int &current )
{
	int t = previous;
	previous = current;
	current = t;
}

This program is what happens when your C++ teacher tries to confuse us by writing a random sorting algorithm that, oh I dunno, actually works? The story goes that she got an email about this sorting algorithm, asking if she had any use for it. She immediately thought of my class of programmers and handed out the specs. The problem was, I was going over the specs, and had finished writing this BEFORE SHE EVEN FINISHED EXPLAINING THEM.

I like to think of it as a reverse bubble sort of sorts. Or a selection swap sort of some kind. I dunno. Thought I’d share it with you though.