Thursday, May 17, 2007

-=Selection Sort=-

This is a nice easy quadratic sorting algorithm similar to BubbleSort. Think of this one like arranging cards in your hand. You take the first card and note down its number (4) then you look along the cards and find one smaller (3), you note down where the smaller card is located and then you carry on trying to find smaller than that. Once you've found the smallest, you swap the first card with the smallest. Then you repeat starting from the second card. Easy!

view the php demostration here

and the source code here

-=bouj=-

-=Bubble Sort=-

i've been extra busy with exams and my project report (18,000 words) over the past few weeks.
as part of my algorithms revision i have been implementing the basic algorithms in php to help me remember them.

Bubble sort is whereby you pass through the array n times, where n = (array_length -1).
Each time you pass through you compare the 1st cell of the array with the 2nd, if the first is higher then you can swap them. repeat with the 2nd and 3rd cells etc...
This method pushes the largest number in the array to the end, and in the 2nd pass pushes the 2nd largest to the penultimate cell etc.

you can see the php running here (note that on each iteration the array has been printed to show workings)

and the php code can be found here

-=b0uj=-