Showing posts with label program. Show all posts
Showing posts with label program. Show all posts
Sunday, February 15, 2015
C Program for Implementation of Merge Sort
Merge sort runs in O (n log n) running time. It is very efficient sorting algorithm with near optimal number of comparison. Recursive algorithm used for merge sort comes under the category of divide and conquer technique. An array of n elements is split around its centre producing two smaller arrays. After these two arrays are sorted independently, they can be merged to produce the final sorted array. The process of splitting and merging can be carried recursively till there is only one element in the array. An array with 1 element is always sorted.
Also Read: C Program for Sorting an Array using Heap Sort
Also Read: What is Quick Sort? Algorithm and C Program to Implement Quick Sort
An example of merge sort is given below. First divide the list into the smallest unit (1 element), then compare each element with the adjacent list to sort and merge the two adjacent lists. Finally all the elements are sorted and merged.


#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("
Sorted array is :");
Sorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;
while(i<=j1 && j <=j2) //while elements in both lists
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1) //copy remaining elements of the first list
temp[k++]=a[i++];
while(j<=j2) //copy remaining elements of the second list
temp[k++]=a[j++];
//Transfer elements from temp[] back to a[]
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

Saturday, February 14, 2015
C program to convert given binary number into decimal number

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long int i,n,x=0,a;
clrscr();
printf("Enter any Binary number: ");
scanf("%ld",&n);
printf("
The Decimal conversion of %ld is ",n);
for(i=0;n!=0;++i)
{
a=n%10;
x=(a)*(pow(2,i))+x;
n=n/10;
}
printf("%ld",x);
getch();
}
Wednesday, February 11, 2015
C Program to check whether a number is odd or even
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
cout<<"enter the number:";
cin>>a;
if(a%2==0)
cout<<"
Even number";
else
cout<<"
Odd number";
getch();
}
#include<conio.h>
void main()
{
clrscr();
int a;
cout<<"enter the number:";
cin>>a;
if(a%2==0)
cout<<"
Even number";
else
cout<<"
Odd number";
getch();
}
Tuesday, February 10, 2015
Java Program to Find IP Address
In this program we will find ip address by using InetAddress class present in java.net package. Method getLocalHost() prints the ip of your local machine while method getByName() prints the ip of a particular url.
Also Read: Java Program to Find Union of two Arrays
class FindIP
{
public static void main(String...s)throws Exception
{
System.out.println(InetAddress.getLocalHost());
System.out.println(InetAddress.getByName("www.google.com"));
}
}

Also Read: Java Program to Find Union of two Arrays
Program:-
import java.net.*;class FindIP
{
public static void main(String...s)throws Exception
{
System.out.println(InetAddress.getLocalHost());
System.out.println(InetAddress.getByName("www.google.com"));
}
}

Thursday, January 22, 2015
How to Program Google Android
How to Program Google Android
LEARN TO PROGRAM
Subscribe to:
Posts (Atom)