Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Saturday, November 17, 2007

Sorting techniques and algorithms (2)

Before reading this article please refer to part 1.

Selection Sort:
Selection sort is the most conceptually simple of all the sorting algorithms. It's inefficient on large lists, and generally performs worse than the similar insertion sort. It works by selecting the smallest (or largest, if you want to sort from big to small) element of the array and placing it at the head of the array. Then the process is repeated for the remainder of the array; the next largest element is selected and put into the next slot, and so on down the line.

Because a selection sort looks at progressively smaller parts of the array each time (as it knows to ignore the front of the array because it is already in order), a selection sort is slightly faster than bubble sort, and can be better than a modified bubble sort.

Here is an example of this sort algorithm sorting five elements:

31 25 12 22 11
11 25 12 22 31
11 12 25 22 31
11 12 22 25 31
And here is the code for a simple selection sort:


Insertion Sort:

Insertion sort is a simple sorting algorithm. It inserts each element of the array into its proper position, leaving progressively larger stretches of the array sorted. What this means in practice is that the sort iterates down an array, and the part of the array already covered is in order; then, the current element of the array is inserted into the proper position at the head of the array, and the rest of the elements are moved down, using the space just vacated by the element inserted as the final space.

It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort, but it has various advantages:
  • Simple to implement
  • Efficient on (quite) small data sets
  • Efficient on data sets which are already substantially sorted
  • More efficient in practice than most other simple algorithms such as selection sort or bubble sort.
  • Stable (does not change the relative order of elements with equal keys)
  • In-place (only requires a constant amount of extra memory space)
  • It is an online algorithm, in that it can sort a list as it receives it
Here is an example: for sorting the array 52314 First, 2 is inserted before 5, resulting in 25314 Then, 3 is inserted between 2 and 5, resulting in 23514 Next, one is inserted at the start, 12354 Finally, 4 is inserted between 3 and 5, 12345

Here's a simple implementation of insertion sort in C/C++:

For a complete list of popular sorting algorithms please refer to this Wikipedia article

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 :)

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)