The blog has now moved!

Tuesday, March 27, 2007

Sorting stuff out

I got up, did some general WoA stuff and then attended my lectures. The first was okay, the second not amazing and the third okay too. Then the Haskell test. In the lecture we were told it would be open book, then during the exam everybody without a laptop said that laptops shouldn't be allowed, me not being a spiteful person I didn't try and remove the open book stuff altogether but I must say that some of the people's spiteful attitudes of "If I can't use a laptop nobody can" was a bit annoying to see, yes, at least one person actually said that. Incidentally I failed the test, no idea of my actual result but I got a fail. However, I needed only 32% this test to pass the entire unit so I don't think I'm doing too badly overall considering there are still two more test then a 6th test which will allow the 5 best marks to be used. CU tonight was pretty cool, loads of stuff about proving the Resurrection. Now for today's "topic". I figured I'd explain a programming concept and even an Algorithm of how to do it! Before you panic, an Algorithm is simply a fancy way of saying "A list of instructions". The topic is of course sorting. On our left we have an example of a "list" or "array" of numbers. Quite clearly you can see that they are in order of smallest to biggest. They are sorted. This has several uses, in particular, looking for a particular thing is much easier and faster if everything is sorted. Thus there are many different methods of sorting, we'll look at one called the "Bubble" or "Ripple" sort. The above list will now be twisted a little, lets mix it up by moving the "4", two spaces to the left and swapped the 3 and 2 around, making the list you see to the left of this paragraph. We're now going to perform a Bubble sort on it. The method of the Bubble sort is to go through every item on the list and look at the item to it's right. Is the item to the right of the item it's looking at smaller than it? (We're assuming you want to sort it from smaller to bigger). If the item to the right of our current item is smaller than us, we swap places with it. The first item is smaller than the second, so on the picture to the left we'll not swap. The colour coding is that anything already looked at is blue coloured, the two items being looked at are bright turquoise (the main item we are looking at is on the left, the one we are comparing it to is on the right) and anything yet to be checked is in pale turquoise. As can be seen to our left, now that we've moved onto the 2nd item, the one to it's right is smaller than it. Therefore we'll swap the currently selected value and the value on the right. SWIP SWAP! Okay, I added in the noise myself, you don't normally get the noise. However, now we've swapped the 4 and 3 around, lets see, oh look, it turns out that 4 is in fact bigger than 2 and we'll need to swap. SWOOPITY SWAPITY! It's swapped, 4 is smaller than 5 so it's not swapped. We cannot check 5 as it's the last item in the list, there's nothing after it to compare it with! What now? Our list isn't sorted yet we're at the end? We go back to the start! Back to good old 1 again. Lets just skip ahead a little... Ah ha! 3 is bigger than 2 so we're going to swap them to come out with.... SWEESH SWOOP The final result! Looking something like this. I'm not going to cover how this algorithm knows how to stop as that's not needed, I hope you've all found this interesting and enjoyed reading, I'm sure the images helped a little :)

3 comments:

Anonymous said...

Go Go Go! Thanks for the comment. Go ahead! I would love to see what's up from your side. And let me know, where on 'Addicted to your Blog' you find it appropriate, to be placed.

Allan.

Anonymous said...

the way i see it is like this. Everybody has notes made from labs and lectures. Some people have text books. Very very few people have laptops (mainly because they just don't have the money). Therefore, it should be make so that it suits everyone in the group which means that only notes should be allowed.

Teifion said...

I have no problem with that. I had a problem with the reason they wanted Laptops removed, not because "it would be fair" but "if I can't have a laptop, you can't" which to me is simply spiteful.