Tuesday, October 23, 2007

Monday, October 22, 2007

Sorting techniques and algorithms (1)

Have an array you need to put in order? Keeping business records and want to sort them by ID number or last name of client? Then you'll need a sorting algorithm. To understand the more complex and efficient sorting algorithms, it's important to first understand the simpler, but slower algorithms. In this article, I'll talk about the 3 algorithms we studied at college which are bubble sort, including a modified bubble sort that's slightly more efficient; insertion sort; and selection sort. Any of these sorting algorithms are good enough for most small tasks, though if you were going to process a large amount of data, you would want to choose one of the sorting algorithms that I'll talk about in the next posts isA.

Bubble Sort:
The bubble sort is the oldest, simplest and generally considered to be the most inefficient sorting algorithm in common usage. Unfortunately, it's also the slowest. It works by comparing each item in the list with the item next to it, and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items (in other words, all items are in the correct order). This causes larger values to "bubble" to the end of the list while smaller values "sink" towards the beginning of the list.

Since the worst case scenario is that the array is in reverse order, and that the first element in sorted array is the last element in the starting array, the most exchanges that will be necessary is equal to the length of the array. Here is a simple example:

Given an array 23154 a bubble sort would lead to the following sequence of partially sorted arrays: 21354, 21345, 12345. First the 1 and 3 would be compared and switched, then the 4 and 5. On the next pass, the 1 and 2 would switch, and the array would be in order.

The basic code for bubble sort for sorting an integer array using C looks like this:

A better version of bubble sort, known as modified bubble sort, includes a flag that is set if an exchange is made after an entire pass over the array. If no exchange is made, then it should be clear that the array is already in order because no two elements need to be switched. In that case, the sort should end. The new best case order for this algorithm is O(n), as if the array is already sorted, then no exchanges are made. After modifying the code it will be like this:

In part two I will talk about insertion sort and selection sort. Please post a comment if you have any questions :)

Tuesday, October 16, 2007

Developers developers developers developers...!!

A couple of years at Microsoft and you should start acting like this!!



In this video: Steve Ballmer, Microsoft CEO.

Monday, October 15, 2007

You are a Programmer IF...

1- You have more than a document file on your desktop called "To Do".
2- You start counting from zero.
3- You can find some code in a folder on ur PC & you wonder when & how you made this code.
4- You may stay awake a whole night trying to fix a bug, then when u go asleep, you dream of the solution.
5- You don't like most of the softwares in the market.
6- You forget to eat or sleep cause you were too busy writing some piece of code.
7- You stress the words IF, THEN & ELSE while speaking.
8- You always search for the undo button (ctrl+z) when u mistake in doing any handwritten stuff.
9- Most of your speech is not understood except by your colleagues in college or work.
10-You find yourself writing a semi-colon at the end of any sentence instead of a full stop;
11-You estimate lengths better in pixels than in meters & centimeters.
12-You use the eye-drops as frequent as you use the toothpaste.
13-The First words you write when trying sth new are "Hello World".
14-You never used that device that looks like a computer screen & called "TV".
15-You - every now & then - wake up with the keyboard imprinted on your face.
16- You have a volatile Memory like RAM.
17- You write C++,C# or Java better than u write your own language.
18- You don't think wearing sports shoes with a suit is strange.
19- You remember all your passwords, but fail to remember you own birth date.
20- The word 'Engine' reminds you of games more than of cars.
21- Anytime you see a penguin, you think of Linux.
22- You have a LAN in your room.
23- You're the highest-paid yet the worst-dressed person in the office.
24- You follow the instructions only if everything else fails.
25- You use F1 instead of SOS.


Source: JetBrain blog (a blog by Roaa Mohammed, an MSP at Ain Shams university)