Monday 28 September 2015

Quick sort

Quick sorting is an important algorithm as it uses divide and conquer approach and here in this algorithm we divide the array into two arrays where in one array all elements smaller than pivot will be stored and in second all the elements greater than pivot will store. 
After getting two arrays we will again apply the same procedure and in last we will be getting the sorted array 


the idea is this 
and the program is 

#include<stdio.h>
#define first 0
void main()
{
    int A[]={23,6,5,17,54,2,4,9,76,24,84,286};
    int size= sizeof A/sizeof A[0];
    quick_sort(A,first,size-1);
    output(A,size);

}
void quick_sort(int *A,int p,int r)
{
    if(p<r)
    {
        int pivot=A[r];
        int i=p-1,j;
        for(j=p;j<r;j++)
        {
            if(A[j]<pivot)
            {
            i++;
            swap(&A[i],&A[j]);
            }
        }
        A[r]=A[i+1];    /*change the pivot position to its original position where the boundary between                                         two arrays(smaller than pivot and larger than pivot) will exist*/
        A[i+1]=pivot;
        quick_sort(A,p,i); 
                                 /* as the pivot element that we choose is now on i+1 position see above line */
        quick_sort(A,i+2,r);  
                          /*we are not  passing the pivot(i.e. i+1position) as it is already on its right position*/
    }
}

void swap(int *m, int *n)
{
    int temp = *m;
    *m=*n;
    *n=temp;
}

void output(int *A,int size)
{
    int i=0;
    for(i=0;i<size;i++)
    {
        printf("%d\n",A[i]);

    }
}

Bubble Sort

Today we will be discussing about bubble sort . In this sorting the biggest bubble(ie maximum number) will go on the end position after first iteration . 
Then we will move the second largest bubble(ie second largest number) on the second last  position.
and so on . 
So the simple program for bubble sort is 

 #include<stdio.h>
void main()
{
    int A[]={23,76,4,12,76,32,98,54,13,65};
    int size = sizeof A/sizeof A[0];
    bubble_sort(A,size);
    output(A,size);
}

void bubble_sort(int *A,int size)
{
    int i,j;
    for(i=0;i<size-1;i++)
    {
        for(j=0;j<size-i-1;j++)
        {
            if(A[j]>A[j+1])
            {
                swap(&A[j],&A[j+1]); /* swap the number if first number is larger as we are trying to                                                                             move the larger number in end */
            }
        }
    }
}
void swap(int *m, int *n)
{
    int temp = *m;
    *m=*n;
    *n=temp;
}

void output(int *A,int size)
{
    int i=0;
    for(i=0;i<size;i++)
    {
        printf("%d\n",A[i]);

    }
}

Sunday 27 September 2015

Selection sort

Now today i will be discussing about selection sort too . in this sorting we first try to find the maximum element and then we place it in array's first position . 
After this step we find second greatest element and place it in second position of array and so on. 
So the simple program for selection sort is

 #include<stdio.h>
void main()
{
    int array1[]={32,67,8,54,21,43,76,83,12,32},i;
    selection(array1,9);
    for(i=0;i<=9;i++)
    {
    printf("%d\n",array1[i]);
    }
}
void selection(int *A,int n )
{

    int i,j,temp;
    for(i=0;i<=n-1;i++)
    {
        for (j=i;j<=n;j++)
        {
            if (A[i]>A[j])
                                     /*checking if the element that we are comparing is smaller than our assumption of minimum change it */
            {
                temp = A[j];
                A[j]=A[i];
                A[i]=temp;
            }
        }
    }
}




Insertion sorting

Today we will be talking abut insertion sort . This is a very simple program but an important algorithm for sorting the numbers . The approach is quite similar to sorting of playing cards . when we find a new number we will move that card there and will shift the other cards.
here is the program

       #include<stdio.h>
void main()
{
    int A[]= {12,65,32,87,98,21,43,14,65,76};
    int size= sizeof(A)/ sizeof(A[0]) ;        /*it will give number of elements*/
    insertion_sort(A,size);
    output(A,size);

}
void insertion_sort(int *A,int size)
{
    int i ,j;
    for (i=1;i<size;i++)                   /*as last index=size-1*/
    {
        int key =A[i];
        j=i-1;
        while (A[j]>key&&j>0)      /* loop will go on and on & swaps until A[j] is smaller than key */
        {
            A[j+1]=A[j];
            j--;
        }
        A[j+1]=key;
    }
}

void output(int *A,int size)
{
    int i=0;
    for(i=0;i<size;i++)
    {
        printf("%d\n",A[i]);

    }
}