New Android apps UniqueKey

Friday, 20 February 2015

Heap Sort


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

main()
{
 int heap[50],i,j,ch,size,loc,par;
    int temp;
 
 printf("enter the size of heap tree\n");
 scanf("%d",&size);
 
 printf("all the elements\n");
 for(i=0;i<size;i++)
 scanf("%d",&heap[i]);
 
 for(i=0;i<size;i++)
 {
  loc=i;
  do
  {
   par=(loc-1)/2;
   if(heap[par]<heap[loc])
   {
    temp=heap[par];
    heap[par]=heap[loc];
    heap[loc]=temp;
   }
   loc=par;
  }while(loc!=0);
 }
 printf("heap tree is \n");
 for(i=0;i<size;i++)
 printf("%d\t",heap[i]);
 
 for(j=size-1;j>=0;j--)
 {
  temp=heap[0];
    heap[0]=heap[j];
    heap[j]=temp;
  par=0;
  do
  {
   ch=2*par+1;
   if(heap[ch]<heap[ch+1]&&ch<j-1)
   ch++;
   if(heap[par]<heap[ch]&&ch<j)
   {
    temp=heap[par];
    heap[par]=heap[ch];
    heap[ch]=temp;
   }
   par=ch;
  }while(ch<j);
 }
 printf("\nsorted array\n");
 for(i=0;i<size;i++)
 printf("%d\t",heap[i]);
}

Binary search


#include<stdio.h>

main()
{

    int arr[100],i,n,key,c=0,first,last,mid;

    printf("Enter the number of element in array :   ");
    scanf("%d",&n);

    printf("Enter the elements in ascending order :   ");
    for(i=0;i<n;i++)
 {
         scanf("%d",&arr[i]);
    }

    printf("Enter the number to be search :   ");
    scanf("%d",&key);

    first=0;
 last=n-1;
    
    while(first<=last)
 {
         mid=(first+last)/2;
         
         if(key==arr[mid])
   {
             c=1;
             break;
         }
         else if(key<arr[mid]){
             last=mid-1;
         }
         else
             first=mid+1;
    }
    if(c==0)
         printf("The number is not found.");
    else
         printf("The number is found.");

    return 0;
}

Binary search ( Recursion )


#include<stdio.h>

int binary(int [],int,int,int,int);

main()
{
 int a[50],i,n,m,first,last,c;
 
 printf("enter the number of elements\n");
 scanf("%d",&n);
 
 printf("enter all elements\n");
 for(i=0;i<n;i++)
 scanf("%d",&a[i]);
 
 printf("enter the number to be searched\n");
 scanf("%d",&m);
 
 first=0;
 last=n-1;
 
 c=binary(a,n,m,first,last);
 
 if(c==0)
 printf("element NOT FOUND\n");
 else
 printf("element found\n");
}

int binary(int a[],int n,int m, int first, int last)
{
 int mid,c=0;
 
 if(first<=last)
 {
   mid=(first+last)/2;
   
   if(m==a[mid])
   {
    c=1;
      }
   else
   {
    if(m<a[mid])
    return binary(a,n,m,first,mid-1);
    else
    return binary(a,n,m,mid+1,last); 
   }
}
 return c;
}

Quick sort on String


/* A c program to perform quick sort on String */

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

void quick(char [],int,int);
main()
{
 int first,last,i,j,n;
 char a[50];
 
 printf("enter string\n");
 gets(a);
 n=strlen(a);
 
 quick(a,0,n-1);
 printf("new string\n");
 printf("%s",a);
}
void quick(char a[],int first,int last)
{
 int i,j,pivot,temp;
 
 if(first<last)
 {
  pivot=first;
  i=first;
  j=last;
  
  while(i<j)
  {
   while(tolower(a[i])<=tolower(a[pivot])&&i<last)
   i++;
   while(tolower(a[j])>tolower(a[pivot]))
   j--;
   
   if(i<j)
   {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
   }
  }
  temp=a[pivot];
  a[pivot]=a[j];
  a[j]=temp;
  quick(a,first,j-1);
  quick(a,j+1,last);
 }
}

Tower Of Hanoi


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

int tower(int,char,char,char);

main()
{
 char source,temp,dest;
 int num;
 
 printf("ENTER THE NUMBER OF DISK FOR 'TOWER OF HANOI'\n");
 scanf("%d",&num);
 
 tower(num,'A','B','C');
 
}
int tower(int num,char source,char temp,char dest)
{
 if(num==1)
 {
  printf("move %d from %c to %c\n\n",num,source,dest);
 }
 else
 {
  tower(num-1,source,dest,temp);
  printf("move %d from %c to %c\n\n",num,source,dest);
  tower(num-1,temp,source,dest);
 }
}