Tuesday 24 June 2014

                                     Quick Sort

                            


                        /* Quick Sort taking first element as pivot element*/

#include<stdio.h>
#include<stdlib.h>
void QuickSort(int*,int ,int );
     
int main()
{
    int i,j,n,m,*arr;
    printf("enter the amount of number to be sort\n");
    scanf("%d",&n);
    printf("enter the values to be sort\n");
    arr=(int*)malloc(sizeof(int)*n);
    for(i=0;i<n;i++)
    {
                    scanf("%d",&arr[i]);
    }
     QuickSort(arr,0,n-1);
     for(i=0;i<n;i++)
     {
                     printf("%d\t",arr[i]);
     }
     system("\n pause");
     return 0;
}
    
void QuickSort(int* arr,int start,int last)
{
     int i=start+1,j=last,temp;
     if(i>j)
     return;
     while(i<=j)
     {
              if(arr[i]<arr[start])
              {
                               i++;
              }
              if(arr[j]>arr[start])
              {
                               j--;                
              }
              if(i<=j)
              {
                  temp=arr[i];
                  arr[i]=arr[j];
                  arr[j]=temp;
              }
      }

       temp=arr[start];
       arr[start]=arr[j];
       arr[j]=temp;

       QuickSort(arr,start,j-1);
       QuickSort(arr,j+1,last);
}
     

Monday 23 June 2014

                                   Insertion Sort

                      an example of how the algorithm works on a list of 5 integers:

code for Insertion Sort:-

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

int main()
{
    int i,j,n,m,*arr,temp,temp1,loc=0,flag;
    printf("enter the amount of number to be sort\n");
    scanf("%d",&n);
    printf("enter the values to be sort\n");
    arr=(int*)malloc(sizeof(int)*n);
    for(i=0;i<n;i++)
    {
                    scanf("%d",&arr[i]);
    }
    for(i=1;i<n;i++)
    {
                    flag=0;
                    temp=arr[i];
                    for(j=i-1;j>=0;j--)
                    {
                       if(temp<arr[j])
                       {          
                           arr[j+1]=arr[j];
                           loc=j;
                           flag++;
                          
                       }                                                         
                    }
                   if(flag>0)
                    arr[loc]=temp;
                  
    }
    
     for(i=0;i<n;i++)
     {
                     printf("%d\t",arr[i]);
     }
     system("\n pause");
     return 0;
}
    



                            Selection Sort

    an example of how the algorithm works on a list of 5 integers:




code for Selection Sort:-


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

int main()
{
    int i,j,n,m,*arr,temp=0,min,loc;
    printf("enter the amount of number to be sort\n");
    scanf("%d",&n);
    printf("enter the values to be sort\n");
    arr=(int*)malloc(sizeof(int)*n);
    for(i=0;i<n;i++)
    {
                    scanf("%d",&arr[i]);
    }
    for(i=0;i<n;i++)
    {
                    min=arr[i];
                    for(j=i;j<n;j++)
                    {
                                    if(arr[j]<=min)
                                    {
                                    min=arr[j];
                                    loc=j;
                                    }                
                    }
                    temp=arr[i];
                    arr[i]=min;
                    arr[loc]=temp;
    }
    
     for(i=0;i<n;i++)
     {
                     printf("%d\t",arr[i]);
     }
     system("\n pause");
     return 0;
}
    




                                       -:Bubble Sort:-


             Sorting is the process of arranging items in some sequence and/or in different sets

1).Bubble Sort:-
                            Here is an example of how the algorithm works on a list of 5 integers:



code for Bubble Sort


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

int main()
{
    int i,j,n,m,*arr,temp=0;
    printf("enter the amount of number to be sort\n");
    scanf("%d",&n);
    printf("enter the values to be sort\n");
    arr=(int*)malloc(sizeof(int)*n);
    for(i=0;i<n;i++)
    {
                    scanf("%d",&arr[i]);
    }
    for(i=0;i<n;i++)
    {
                    for(j=0;j<n-i;j++)
                    {
                                      if(arr[j]>arr[j+1])
                                      {
                                                         temp=arr[j];
                                                         arr[j]=arr[j+1];
                                                         arr[j+1]=temp;
                                      }
                    }
    }
    
     for(i=0;i<n;i++)
     {
                     printf("%d\t",arr[i]);
     }
     system("\n pause");
     return 0;
}
    

Tuesday 17 June 2014

palindrome and prime

                                 palindrome prime

this is a program for finding a smallest number that is prime and also  palindrome
you have to enter a number and it finds the number which is greater than and equal to this number and  smallest number that is prime and palindrome both.



#include<stdio.h>
#include<math.h>

int primecheck(int);
int palindromecheck(int);
int main()
{
    int i,n,m,p;
    scanf("%d",&n);
    for(i=n;;i++)
    {
         m=palindromecheck(i);
        if(m) 
        { 
          p=primecheck(i); 
          if(p)  
          {
                  printf("%d",i) ;
                  break;
          } 
        } 
    }  
    system("pause");
    return 0;
}  

int primecheck(int n)
{
    int i;
    if(n==0||n==1)
    return 0;
    for(i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)
        return 0; 
    } 
    return 1;
}  

int palindromecheck(int n)
{
    int temp,rev=0,x;
    temp=n;
    while(temp)
    {
               x=temp%10;
               rev=rev*10+x;
               temp=temp/10;
    }    
    if(rev==n)
    return 1;
    else
    return 0;
}