Friday, September 24, 2010

Quick Sort - for my students...

Quick Sort is a sorting algorithm where we apply Divide and Conquer policy. In this sorting algorithm, the n elements to be sorted are partitioned into three segments - a left segment, a middle segment and a right segment. The middle segment is called pivot. The middle segment exactly contains one element. The partition is done in such a way so that all the elements in the left segment have key less than the pivot and all the elements in the right segment have keys greater than the pivot. As a result, the left segment and the right segment can be sorted independently.

The basic principle of Quick sort is as follows:

  • Select an element from a[0:n-1] for middle. This element is called pivot.
  • Partition the remaining element in such a fashion that all elements in the left of the pivot have keys less than the pivot, and all the elements in the right segment have keys greater than the pivot.
  • Sort left segment using quick sort recursively
  • Sort right segment using quick sort recursively
  • The answer will be left followed by middle followed by right.

The algorithm of the quick sort can be found here.

Debugging of the Algorithm of the code:

Step I: To begin with we get m = 0, n = 6, hence k = 3. After doing swap(&list[m],&list[k]), we get key = 5. The following two steps make i = 1 and j = 6.

Step II: Now the loop starts.

II while loop fails as list[i] (7) is not less than key (5). Hence i does not increase and it remains 1. The III while loop succeeds (as list[j]  > key)and hence j is decremented. So j becomes 5. In the next iteration of the III while loop, the list[j]>k condition fails, hence j remains 5 and we exit the III while loop. So we exchange list[i] and list[j] and the array becomes {5,1,8,3,2,7,9}

Next the II while loop succeeds and we increment i. Hence i becomes 2. The III while loop also succeeds and hence j becomes 4. So list[i] = list [2] = 8 and list[j] = list[4] = 2. So when we swap between list[i] and list[j], the array becomes list {5,1,2,3 8,7,9}.

Next the II while loop succeeds two times and we get i = 4. The III while loop also succeeds and we get j = 3. As j becomes larger than i, we come out of the I while loop.

Next we do swap(&list[m],&list[j]). And the list becomes list {3,1,2,5,8,7,9}

After that we recursively sort list{3,1,2}, the left segment and list{8,7,9}, the right segment.

See how all the elements of the left segment are less than 5 and all the elements in the right segment are greater than 5.

This is how we achieve the Quick Sort.

Sunday, September 19, 2010

Pointer Semantics Vs Value Semantics

Value semantics means we directly deal with the values and we pass copies of that value around. We can say that nobody will change the value.

However, in pointer semantics we deal with pointer, and anyone can change the value at the pointer location.

Consider the following program.

/*
 * main.c
 *
 *  Created on: Sep 18, 2010
 *      Author: administrator
 */

#include

void f (int * p, int * q) {
p = q;
*p = 2;
}

int i = 0, j = 1;


int main ( )
{
f(&i, & j);
printf("%d %d\n", i, j) ;
return 0;
}

The result will be 0 and 2. Why? Let me explain it to you.

We have two global variable i and j and we pass the pointer of these two variables to the function f.  When we do p = q, we actually loose the reference of i, and we get two pointers namely p and q both pointing to j. then when we do * p = 2, we actually change the value of j to 2. 

The whole thing can be depicted by the following diagram...



However, as we lost the reference of i in the step p = q, in the main program, the value of i that gets printed is the global variable that is 0. Hence we get the result as i = 0 and j = 2.

Hope this explains the pointer arithmetic to my students...

Friday, August 13, 2010

For my Microprocessor Students

I got this resource by googling. Hope this helps the student community.

Microprocessor Training

A good simulator for 8085 programming on PC is GNUSIM8085 and it is available at http://gnusim8085.org/


The interface of GNUSIM8085 looks like the following.

Monday, July 19, 2010

Ubuntu video screen capture - XVidCap

I was wondering if i could get any tool for video screen capturing for Ubuntu. With a little bit of googling, i found XVidCap. Here is my first experience with the video screen capture of one of my Android Applications.



Hope this information helps others...

Monday, March 29, 2010

My first experience with OpenCV

OpenCV is an opensource library for computer vision. It is available at http://sourceforge.net/projects/opencvlibrary/

With the help of some of the internet samples, i tried to play around with OpenCV from past two days. And here is one such application. It actually plays an AVI file.


You can find a nice tutorial on OpenCV at http://www.site.uottawa.ca/~laganier/tutorial/opencv+directshow/cvision.htm

Here is a glimpse of how my application will look like.



Hope it gives some insights to others who are interested in computer vision.

Friday, March 12, 2010

Cubicles in Indian IT companies...




Let us break the cubicle culture...

The way we behave in a crowd or in solitude is totally different from the way we behave in a place surrounded by few known people... 

In the first case, we become carefree and tend to break all sorts of barriers... 

In the second case, we restrict ourselves to some extent... in the first case when we break free, we may create something new, do something highly innovative... 

however, it's just not possible in the second case...

hence I believe to get some Aha-product from our IT companies from #bharat, we must first break the cubicle culture that exists now and let the developers become care-free...

only when we unshackle the creator from all sorts of rules and regulations, the magic happens.

the #tandava dance was done without any written or prepared script or rhythm as such...

spontaneity and freedom are the first and only needs of a creator.

Thursday, March 4, 2010

Training on C++, Android, UML and Design Pattern

I am interested in providing training on subjects like C++, UML, Android, Design Pattern, Java/J2SE etc. Please have a look at my training website

som-itsolutions

to get an idea.

If you are interested, please email me.