Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Insertion Sort C Program Insertion Function Sorting Array

square C Program - Sort Array Using Insertion Function


This week I'm enjoying coding sorting techniques on old yet my favourite Borland Turbo C Complier. Yesterday I managed to code another sorting program in C language using Insertion Function. This C Program help you sort an array with any random integer values. If you're a computer science student learning to implement sorting techniques using C Programming language then following code may prove useful in your lab practicals. I feel great to share source code of my Insertion Sorting C Program.


C Program Insertion Function Sorting



square C Program - Sorting Array Using Insertion Function


//Analysis of Algorithms
//Insertion Sorting - C Program
//Sort Array Using Insertion Function.
//Program by:- GAURAV AKRANI. 
//TESTED:- OK

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

   void insertion(int a[],int n)
   {

    int i,j,x,k;

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

                       x=a[i];


                       while(a[j-1]>x && j>0)

                                 {

                                   a[j]=a[j-1];
                                    j=j-1;

                                 }

                        a[j]=x;


                       printf("\n\n The array after pass no.%d: ",i);
                       for(k=0;k<=n-1;k++)
                      printf("%4d",a[k]);


                 }//end for.


   } //end function.

   void main()

   {

     int a[1000],n,i;

     clrscr();

     printf("\n\nEnter an integer value for total no.s of elements to be sorted: ");
     scanf("%3d",&n);

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

          {

            printf("\n\nEnter an integer value for element no.%d: ",i+1);
            scanf("%4d",&a[i]);

          }


     insertion(a,n);


     printf("\n\n\nFinally sorted array is : ");
     for(i=0;i<=n-1;i++)
     printf("%4d",a[i]);


   }//end program.

/*

----------sample output------------------



Enter an integer value for total no.s of elements to be sorted: 6


Enter an integer value for element no.1: 67


Enter an integer value for element no.2: 34


Enter an integer value for element no.3: -23


Enter an integer value for element no.4: 100


Enter an integer value for element no.5: 0


Enter an integer value for element no.6: -68


The array after pass no.1: 34 67 -23 100 0 -68

The array after pass no.2: -23 34 67 100 0 -68

The array after pass no.3: -23 34 67 100 0 -68

The array after pass no.4: -23 0 34 67 100 -68

The array after pass no.5: -68 -23 0 34 67 100


Finally sorted array is : -68 -23 0 34 67 100



*/


Read More

Bubble Sorting C Program Bubble Sort - C Programming

square Write A C Program To Implement Bubble Sorting


Once again I started practicing and learning C Programming Language. Yesterday I was working on Sorting Techniques. I wrote following C Program to implement Bubble Sort Technique and used Borland Turbo C compiler. Feels great to share it on the internet. Interested students can try compiling and modifying following C Program on their Borland Turbo C Compiler.


C Program Bubble Sorting























square C Program - Implementing Bubble Sort Technique


//Analysis of Algorithms
//Sorting Techniques - C Data Structures
//WACP  to Iimplement Bubble Sort Technique.
//Program by:- GAURAV AKRANI. 
//TESTED:- OK


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

  void bubble(int a[],int n)
  {
        int i,j,t;
         for(i=n-2;i>=0;i--)
         {
            for(j=0;j<=i;j++)

                  {
                    if(a[j]>a[j+1])
                                    {
                                      t=a[j];
                                     a[j]=a[j+1];
                                     a[j+1]=t;
                                    }
                   }
       

           }//end for 1.

  }//end function.


  void main()
  {

      int a[100],n,i;

      clrscr();

      printf("\n\n Enter integer value for total no.s of elements to be sorted: ");
      scanf("%d",&n);

      for( i=0;i<=n-1;i++)
            { printf("\n\n Enter integer value for element no.%d : ",i+1);
              scanf("%d",&a[i]);
            }

       bubble(a,n);

       printf("\n\n Finally sorted array is: ");
       for( i=0;i<=n-1;i++)
       printf("%3d",a[i]);

  } //end program.

/*

--------SAMPLE OUTPUT----------------------


Enter integer value for total no.s of elements to be sorted: 6


Enter integer value for element no.1 : 89


Enter integer value for element no.2 : -4


Enter integer value for element no.3 : -67


Enter integer value for element no.4 : 5


Enter integer value for element no.5 : 78


Enter integer value for element no.6 : 11


Finally sorted array is: -67 -4 5 11 78 89

------------------------------------------

*/

Read More






© Kalyan City Life Blog