Tuesday, 8 December 2015

Quick Sort

Quick Sort:

       Quicksort is a recursive sorting technique used to sort elements in decreasing or non-decreasing order. It is based on the concept of Divide-and-Conquer(also used in Merge-sort).

       In this sorting technique we start by choosing a Pivot element which could be any element and at the end of each iteration the pivot element reaches its correct position in the set of elements. This way the quick sort makes it to completion in O(nlog n) complexity which makes it efficient over most other sorting algorithms which have time complexity O(n^2).

Here's the code for quick sort:

#include<stdio.h>
#define MAX 10

int partition(int[],int,int);
void quick(int[],int,int);



void main()
{
int A[MAX]={12,44,10,1,94,5,78,23,56,3};int b=0;int e=9;int i;
printf("Original array is: ");
for(i=0;i<10;i++)
{
printf("%d ",A[i]);
}
quick(A,b,e);
    printf("\n\nQuick sorted array is: ");
for(i=0;i<10;i++)
{
printf("%d ",A[i]);
}
}

void quick(int A[],int b,int e)
{
int q;
if(b>=e)
   return;
else
{
q=partition(A,b,e);
quick(A,b,q-1);
quick(A,q+1,e);
}    
}

int partition(int A[],int b,int e)
{
int temp;
int left=b;int right=e;int loc=b;
while(1)
{
while(A[loc]<=A[right]&&loc!=right)
   right--;
if(loc==right)
   return loc;
if(A[loc]>A[right])
{
temp=A[right];
A[right]=A[loc];
A[loc]=temp;
loc=right;
right--;
}    
while(A[loc]>=A[left] && loc!=left)
   left++;
if(loc==left)
   return loc;
if(A[loc]<A[left])
{
temp=A[left];
A[left]=A[loc];
A[loc]=temp;
loc=left;
left++;
}
}
}

Below is the output:



Note: The code is written in DEVC++ and it might not run in TurboC++ or other IDEs because DEVC++ allows us to omit some code(which is useful). So if you are facing any problem regarding compilation please let me know in the comment box and i will try to resolve your query as ASAP!!!.

Friday, 4 December 2015

Magic number in C

Magic Number

Magic Number is a number whose sum of digits (until one digit remains) is 1.
There can be many ways to implement the Magic Number.This is one of the methods to do so.

Examples of Magic number:

235 is a magic number because 2 + 3 + 5 = 10  => 1 + 0 = 1
657 is not a magic number because 6 + 5 +7 = 18 => 1 + 8 = 9

Below is the code for magic number in C:

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

int main()
{
int n,s1=0,s=0,r;
printf("enter a number\n");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
s=s+r;
n=n/10;
}
while(s!=0)
{
s1=s1+(s%10);
s=s/10;
}
if(s1==10|| s1==1)
printf("Magic number");
else
printf("Not a magic number");
getch();
}

Below is the Output:

1st test case:







2nd test case:












Tuesday, 1 December 2015

Finding Mininum Spanning Tree using Kruskal's Algorithm

Hello guys,
                You must be looking for an easy way to grasp the concept of all those sorts of algorithm out there. If you really want to learn how to find the MST using Kruskal's Algorithm follow the link given below:

https://m.youtube.com/watch?v=_7Bsx88aLns

Please share and subscribe if you like the video..it will help my channel grow and motivate me to post more videos soon.
Enjoy.. :)