maandag 25 juni 2012

Insertion Sort

In order to understand just how important a good sorting algorithm is, I quickly whipped up an Insertion Sort:

template<typename T>
void insertionSort(std::vector<T>& nums)
{
for(int i = 0; i < nums.size(); i++)
{
int pos = i;
for(int j = i-1; j >= 0; j--)
{
if(nums[j] > nums[pos])
{
swap(nums[j], nums[pos]);
pos = j;
}
else
break;
}
}
}

I applied both of them on a list of 100,000 numbers. This was the result, in seconds: Online Graphing
Graphing

Geen opmerkingen:

Een reactie posten