Wednesday, August 10, 2011

Sorting Techniques in C: Bubble Sort Implementation


Sorting is one of the most common used techniques in business as well any organizations as such. For arranging the database of an employee or student information you would definitely need to use any one of the sorting techniques. 

There many sorting techniques with simple ones being the bubble sort, insertion sort and selection which are used to sort small amount of data, whereas for sorting larger amounts of data you can use Heap, Merge or even Quick sort!

Bubble Sort

This is one of the basic sorting techniques to start of with; all you need to is traverse down the array from the first element to the last one and iterate until the array is completely sorted. 

It is always preferred to have flag which is set on an exchange of two elements in one iteration and if no exchange takes place then the flag is not set as such.

Let us now consider a simple example: 

Let the elements in the array be 3 1 2 4, and you need to sort them in ascending order

First iteration: 1 3 2 4

I and 3 are swapped, since 3 is greater than 1

Second iteration: 1 2 3 4

2 and 3 are swapped, since 3 is greater than 2

Code Snippet:

Let bsort[ ] be an array with n number of elements to be sorted.

// for n number of iteration

for( int i=0 ; i
{
            // to traverse from first element to last element (i.e. n-1 since it is an array)
            for( int j=0 ; j
            {
                        //logic for swapping two numbers
                        if( bsort[ j ] > bsort[ j+1] )
                        {
                                    int temp = bsort[ j+1];
                                   
                                    bsort[ j+1] = bsort [ j ];
                                   
bsort [j] = temp;
                        }
            }
}




Tuesday, February 1, 2011

C Program to Check How Function Calls Are Made Using Stack

A stack is used by programming languages for implementing function calls. Here's a handy C program to check how function calls are made using stack.



#include < stdio.h >
#include < conio.h >
#include < stdlib.h >
#include < dos.h >

unsigned int far *ptr ;
void ( *p )( void ) ;

void f1( ) ;
void f2( ) ;

void main( )
{
f1( ) ;
f2( ) ;

printf ( "\nback to main..." ) ;
exit ( 1 ) ;
}

void f1( )
{
ptr = ( unsigned int far * ) MK_FP ( _SS, _SP + 2 ) ;
printf ( "\n%d", *ptr ) ;

p = ( void ( * )( ) ) MK_FP ( _CS, *ptr ) ;
( *p )( ) ;
printf ( "\nI am f1( ) function " ) ;
}

void f2( )
{
printf ( "\nI am f2( ) function" ) ;
}

Sunday, June 6, 2010

C Program to implement a circular queue as a linked list

Here's a simple C Program to implement a circular queue as a linked list





#include < stdio.h >
#include < conio.h >
#include < alloc.h >

/* structure containing a data part and link part */
struct node
{
int data ;
struct node * link ;
} ;

void addcirq ( struct node **, struct node **, int ) ;
int delcirq ( struct node **, struct node ** ) ;
void cirq_display ( struct node * ) ;

void main( )
{
struct node *front, *rear ;

front = rear = NULL ;

addcirq ( &front, &rear, 10 ) ;
addcirq ( &front, &rear, 17 ) ;
addcirq ( &front, &rear, 18 ) ;
addcirq ( &front, &rear, 5 ) ;
addcirq ( &front, &rear, 30 ) ;
addcirq ( &front, &rear, 15 ) ;

clrscr( ) ;

printf ( "Before deletion:\n" ) ;
cirq_display ( front ) ;

delcirq ( &front, &rear ) ;
delcirq ( &front, &rear ) ;
delcirq ( &front, &rear ) ;

printf ( "\n\nAfter deletion:\n" ) ;
cirq_display ( front ) ;
}

/* adds a new element at the end of queue */
void addcirq ( struct node **f, struct node **r, int item )
{
struct node *q ;

/* create new node */
q = malloc ( sizeof ( struct node ) ) ;
q - > data = item ;

/* if the queue is empty */
if ( *f == NULL )
*f = q ;
else
( *r ) - > link = q ;

*r = q ;
( *r ) - > link = *f ;
}

/* removes an element from front of queue */
int delcirq ( struct node **f, struct node **r )
{
struct node *q ;
int item ;

/* if queue is empty */
if ( *f == NULL )
printf ( "queue is empty" ) ;
else
{
if ( *f == *r )
{
item = ( *f ) - > data ;
free ( *f ) ;
*f = NULL ;
*r = NULL ;
}
else
{
/* delete the node */
q = *f ;
item = q - > data ;
*f = ( *f ) - > link ;
( *r ) - > link = *f ;
free ( q ) ;
}
return ( item ) ;
}
return NULL ;
}

/* displays whole of the queue */
void cirq_display ( struct node *f )
{
struct node *q = f, *p = NULL ;

/* traverse the entire linked list */
while ( q != p )
{
printf ( "%d\t", q - > data ) ;

q = q - > link ;
p = f ;
}
}

Monday, April 19, 2010

Which is the Best Place to Find Saving Deals and Discount Coupon Offers?

More often than not, we tend to look for discount deals on various merchandise and goods while shopping online, or looking for services such as car rentals. But, just when you really need something urgently, you don’t seem to get hold of the right deal! Well, if this has happened to you in past, then here are some great deals on Enterprise Coupons.

Monday, February 15, 2010

Sorting a Structure of Multiple Keys

C Program that accepts a set of 5 records for students -

Ask user to enter name, age and height of a student. Sort these records in ascending order of their names. If the names are alike then sort according to their age.

/* Sorting of a structure of multiple keys. */

#include  < stdio.h >
#include  < conio.h >
#include  < string.h >

struct stud
{
      char name[25] ;
      int age ;
      float height ;
} ;

void main( )
{
      int i, j, choice ;
      struct stud s[5], temp ;

      float ff ( float ) ;

      clrscr( ) ;

      printf ( "Enter student's name, age and height in cm :-\n") ;
      for ( i = 0 ; i  < = 4 ; i++ )
      {
            fflush ( stdin ) ;
            gets ( s[i].name ) ;
            scanf ( "%d %f", &s[i].age, &s[i].height ) ;
      }

      clrscr( ) ;

      for ( i = 0 ; i  < = 3 ; i++ )
      {
            for ( j = 0 ; j  < = 3 - i ; j++ )
            {
                  if ( strcmp ( s[j].name, s[j + 1].name )  > = 0 )
                  {
                        if ( strcmp ( s[j].name, s[j + 1].name ) == 0 )
                        {
                              if ( s[j].age  >  s[j + 1].age )
                              {
                                    temp = s[j] ;
                                    s[j] = s[j + 1] ;
                                    s[j + 1] = temp ;
                              }
                        }
                        else
                        {
                              temp = s[j] ;
                              s[j] = s[j + 1] ;
                              s[j + 1] = temp ;
                        }
                  }
            }
      }

      printf ( "Records after sorting :-\n") ;
      printf ( "Students Name\t\tAge Height\n" ) ;

      for ( i = 0 ; i  < = 4 ; i++ )
      {
            printf ( "%-20s %2d %.2f\n", s[i].name, s[i].age, s[i].height ) ;
      }

      getch( ) ;
}

float ff ( float f )
{
      float *f1 = &f ;
    return *f1 ;
}

So, how is that for a Tutorial?