Showing posts with label array. Show all posts

C program for quick sort

/*Program of sorting using quick sort through recursion*/



#include<stdio.h>

#define MAX 30

enum bool { FALSE,TRUE };



void display(int arr[],int,int);



void quick(int arr[],int low,int up);





main()

{



       int n,i;  int array[MAX];

    printf("Enter the number of elements : ");

         scanf("%d",&n);

  for(i=0;i<n;i++)

     {

     printf("Enter element %d : ",i+1);

           scanf("%d",&array[i]);

      }

   printf("Unsorted list is :\n");



display(array,0,n-1);



printf("\n");



quick(array,0,n-1);



printf("Sorted list is :\n");



display(array,0,n-1);



printf("\n");



return 0;



}       /*End of main() */





void quick(int arr[],int low,int up)

  {

       int piv,temp,left,right;

enum bool pivot_placed=FALSE;

left=low;

right=up;



piv=low; /*Take the first element of sublist as piv */



if(low>=up)

  return ;



printf("Sublist : ");



display(arr,low,up);



/*Loop till pivot is placed at proper place in the sublist*/



 while(pivot_placed==FALSE)

    {



   /*Compare from right to left  */



    while( arr[piv]<=arr[right] && piv!=right )

    

      right=right-1;

   



      if( piv==right )



pivot_placed=TRUE;



if( arr[piv] > arr[right] )

 {

       temp=arr[piv];

       arr[piv]=arr[right];

       arr[right]=temp;

       piv=right;

}



/*Compare from left to right */



while( arr[piv]>=arr[left] && left!=piv )

      left=left+1;



if(piv==left)

    pivot_placed=TRUE;



if( arr[piv] < arr[left] )

 {

     temp=arr[piv];

     arr[piv]=arr[left];

     arr[left]=temp;

     piv=left;

 }



}   /*End of while */



    printf("-> Pivot Placed is %d -> ",arr[piv]);



   display(arr,low,up);



    printf("\n");

 

   quick(arr,low,piv-1);

 

   quick(arr,piv+1,up);



 }/*End of quick()*/





void display(int arr[],int low,int up)

  {



    int i;

      for(i=low;i<=up;i++)

        printf("%d ",arr[i]);

}                                                         

You may like these .

 1. Bubble sort in c
 
 2. Heap sort in c
 
 3. Insertion sort in c 

C program for matrix multilplication , addition and subtraction


/*Matrix Add,Sub,Multi */
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int n,**p,**q,**r,i,j,k,ch;
printf("enter the order of matrix\n");
scanf("%d",&n);
p=(int**)malloc(n*sizeof(int));
q=(int**)malloc(n*sizeof(int));
r=(int**)malloc(n*sizeof(int));
for(i=0;i<n;i++)
p[i]=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
q[i]=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
r[i]=(int*)malloc(n*sizeof(int));

printf("enter the element of first matrix\n");  //inputing
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&p[i][j]);
printf("enter the element of second matrix");   //inputing
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&q[i][j]);
clrscr();
printf("your entered matrix is\n");     //printing
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
 printf("  %d",p[i][j]);
}
printf("\n");
}
printf("\n\n");                            //printing
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
 printf("  %d",q[i][j]);
}
printf("\n");
}
while(1)
{
clrscr();
printf("\n\tEnter 1. for Addtion :");
printf("\n\tEnter 2. for Mult :");
printf("\n\tEnter 3. for Subtract :");
printf("\n\tEnter 4. for Exit :");
printf("\n\tEnter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
for(i=0;i<n;i++)   //Add
{
for(j=0;j<n;j++)
{
r[i][j]=p[i][j]+q[i][j];
}
}
printf("\n Addtion matrix is\n");      //output printing
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",r[i][j]);
}
printf("\n");
} getch();
break;
}
case 2:
{
for(i=0;i<n;i++)   //multipicating
{
for(j=0;j<n;j++)
{
r[i][j]=0;
for(k=0;k<n;k++)
{
r[i][j]=r[i][j]+p[i][k]*q[k][j];
}
}
}
printf("\n multiplyed matrix is\n");      //output printing
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",r[i][j]);
}
printf("\n");
}getch();
break;
}
case 3:
{
for(i=0;i<n;i++)   //Add
for(j=0;j<n;j++)
{
r[i][j]=p[i][j]-q[i][j];
}
printf("\n Subtract matrix is\n");      //output printing
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",r[i][j]);
}
printf("\n");
}
}  getch();
break;
case 4 : return;
default : printf("\n\tWrong choice");
}
}
getch();
}

C program for heap sorting

/* Program of sorting through heapsort*/

# include <stdio.h>

void display();

void del_root(int last);

void create_heap();

void heap_sort();

void insert(int num,int loc);

int arr[20],n;

void main()

{
    int i;

    printf("Enter number of elements : ");

      scanf("%d",&n);

    for(i=0;i<n;i++)

       {
           printf("Enter element %d : ",i+1);

              scanf("%d",&arr[i]);

        }
     printf("Entered list is :\n");

       display();


       create_heap();


     printf("Heap is :\n");

       display();


     heap_sort();


     printf("Sorted list is :\n");

         display();

}

void display()

   {     
       int i;


   for(i=0;i<n;i++)

         printf("%d  ",arr[i]);


 printf("\n");


  }

void create_heap()

    {
        int i;

     for(i=0;i<n;i++)


         insert(arr[i],i) ;
    }

 void insert(int num,int loc)

      {
          int par ;

   while(loc>0)

       {

              par=(loc-1)/2;

           if(num<=arr[par])

             {

               arr[loc]=num;

                return;
             }

             arr[loc]=arr[par];

            loc=par;
      }


         arr[0]=num;

  }

void heap_sort()

     {
          int last;

  for(last=n-1; last>0; last--)

         del_root(last);

     }

  void del_root(int last)

        {
            int left,right,i,temp;

            i=0; /*Since every time we have to replace root with last*/
 /*Exchange last element with the root */

         temp=arr[i];

         arr[i]=arr[last];

         arr[last]=temp;

        left=2*i+1;   /*left child of root*/

        right=2*i+2;  /*right child of root*/

        while( right < last)

           {
               
   if( arr[i]>=arr[left] && arr[i]>=arr[right] )

                  return;

               if( arr[right]<=arr[left] )

                 {
                    temp=arr[i];

                    arr[i]=arr[left];

                    arr[left]=temp;

                    i=left;
                 }
             else

                {
                   temp=arr[i];

                   arr[i]=arr[right];

                   arr[right]=temp;

                    i=right;
                }

         left=2*i+1;

         right=2*i+2;

      }

                
 if( left==last-1 && arr[i]<arr[left] )/*right==last*/

              {
                     temp=arr[i];

                     arr[i]=arr[left];

                     arr[left]=temp;
               }


         }       /  *End of del_root*/














You may like these .

 1. Bubble sort in c
2. Insertion sort in c 3. Quick sort in c

Bubble sort in c

/* Program of sorting using bubble sort */

#include <stdio.h>

#define MAX 20

main()
 {

          int arr[MAX], i, j, k, temp ,n , xchanges;

          printf("Enter the number of elements : ");

                 scanf("%d",&n);

       for (i = 0; i < n; i++)

           {
              printf("Enter element %d : ",i+1);

                 scanf("%d",&arr[i]);
           }

           printf("Unsorted list is :\n");
 
      for (i = 0; i < n; i++)

          printf("%d ", arr[i]);
              
                 printf("\n");

 // Bubble sort works start from here 

 for (i = 0; i < n-1 ; i++)

       {
            xchanges=0;

     for (j = 0; j <n-1-i; j++)

          {

     if (arr[j] > arr[j+1])
           {
                 temp = arr[j];

                 arr[j] = arr[j+1];

                 arr[j+1] = temp;

                 xchanges++;
   
            }   //End of if


          } //End of inner for loop

        if(xchanges==0)    //  If list is sorted

            break;
 
printf("After pass%d,arrangement of elements",i+1);

             for (k = 0; k < n; k++)

                 printf("%d ", arr[k]);

            printf("\n");

      }  //End of outer for loop

           printf("Sorted list is :\n");

                 for (i = 0; i < n; i++)

                     printf("%d ", arr[i]);

               printf("\n");


    } //End of main() 

Array of pointer and pointer to an array in c

Array of pointer
 each element of array contains the address of memory location is called  array of pointer .each index      treated as a pointer

 Ex-
  main()
   {
      int *arr[4] ;
      int a,b,c,d;
      arr[0]=&a;
      arr[1]=&b;
       arr[3]=&c;
       arr[4]=&d;
   }

 All elements of this array is containing address of these variable .

Pointer to an array

pointer to an array is a pointer which can point whole array and in simple way we can say the base address of and array is Pointer to an array.
                                     since using  base address we can access the whole array that means base address of a array points to whole array .

Ex :_

    main()
     {
         int array[6] ={1,2,3,4,5,6,};
          int *point ;
        point =array // Pointer to an array

    }
 






Count frequency of element in array

// This program will help you to generate the frequency of all element present in array


#include<stdio.h>

int main()

  int  *arr,i,j,k,n,temp,f=0;

  printf("Enter the numer of array \n  ");
   scanf("%d",&n);

  arr=(int*)malloc(sizeof(int)*n);    // allocating  the space         

  printf("Enter the element now \n");
    for(i=0;i<n;i++)
      scanf("%d",(arr+i));          // feeding the value in array

 // sorting the array

  for(i=0;i<n;i++)
     {
       for(j=i;j<n;j++)
         {
              if(arr[i]>arr[j])
              {
                 temp=arr[i];
                 arr[i]=arr[j];
                 arr[j]=temp;
              }
         }
     }
  for(i=0;i<n;i++)
    {
     if(arr[i]==arr[i+1])
         f++;
    else
      {
        printf("Frequency of %d is %d \n ",arr[i],(f+1));
          f=0;
      }
    }
  return 0;
 }