Sorting is a fundamental requirement in many programs and libraries. As such it has been throughly explored by many people since the dawn of computers and there are many efficient algorithms and implementations available. This doesn't stop us pulling them apart, finding out all about them and seeing if we can't do better though! This is the first in a series of blogs on the subject where I want to go from looking at sorting algorithms to developing our own generic (of course!) sorting function.

I'll leave you with the good, old (and very, very slow) bubble sort ... we'll make it generic in part II...

void bubbleSort(int numbers[], size_t size)

{

bool swap = true;

while(swap)

{

swap = false;

for(int i = 1; i<size; ++i)

{

if(numbers[i]<numbers[i-1])

{

int tmp = numbers[i];

numbers[i] = numbers[i-1];

numbers[i-1] = tmp;

swap = true;

}

}

}

}

## No comments:

Post a Comment