Sunday, April 21, 2013
Tuesday, March 26, 2013
Matrix Problems
Sunday, March 17, 2013
Quick Sort
#include<stdio.h>
#define MAX 10
void qsort(int A[], int p, int r);
int partition(int A[], int p, int r);
main()
{
int Arr[MAX]={10,4,5,6,2,3,8,1,9,7};
int i;
qsort(Arr,0,MAX);
for(i=0;i<MAX;i++)
printf("%d ",Arr[i]);
}
void qsort(int A[], int p, int r)
{
int q;
if(p<r)
{
q=partition(A,p,r);
qsort(A,p,q-1);
qsort(A,q+1,r);
}
}
int partition(int A[], int p, int r)
{
int i , j , x,tmp;
x=A[r];
j=p;
i=p-1;
while(j<r)
{
if(A[j]<x)
{
i=i+1;
// Exchange A[i] & A[j]
tmp=A[j];
A[j]=A[i];
A[i]=tmp;
}
j++;
}
// Exchange A[i+1] & A[r]
tmp=A[i+1];
A[i+1]=A[r];
A[r]=tmp;
return i+1;
}
Output:
1 2 3 4 5 6 7 8 9 10
Time Complexity:
Best - O( n log n )
Average - O(n log n )
Worst - O( n^2)
Pair Wise Swapping in a Linked List
#include<stdio.h>
#include<stdlib.h>
typedef struct list
{
int info;
struct list *next;
}node;
node * head;
void insert(int data);
void display();
void pairwise_swapping(node *ptr);
void swap(int *a, int *b);
void insert(int data)
{
node *ptr , *tmp;
ptr=(node*)malloc(sizeof(node));
if(ptr==NULL)
return;
(*ptr).info=data;
(*ptr).next=NULL;
if(head==NULL)
{
head=ptr;
}
else
{
tmp=head;
while((*tmp).next!=NULL)
tmp=(*tmp).next;
(*tmp).next=ptr;
}
}
void display()
{
node * ptr;
if(head==NULL)
{
printf("list is empty\n");
return;
}
for(ptr=head;ptr!=NULL;ptr=(*ptr).next)
printf("%d ",(*ptr).info);
}
void swap(int *x , int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
void pairwise_swapping(node *ptr)
{
node *tmp , *tmp_next;
tmp= ptr;
while (tmp!=NULL && (*tmp).next!=NULL)
{
tmp_next = (*tmp).next;
swap(&(*tmp).info , &(*tmp_next).info);
tmp=(*tmp_next).next;
}
}
main()
{
insert(1);
insert(2);
insert(3);
insert(4);
insert(5);
insert(6);
pairwise_swapping(head);
display();
}
Output:
2 1 4 3 6 5
Friday, March 15, 2013
Merge Sort
#include<stdio.h>
#define MAX 10
void MergeSort(int Arr[],int low, int high);
void Merge(int Arr[],int low, int mid,int high);
main()
{
int A[MAX]={9,8,7,6,5,4,3,2,1,10};
int i;
MergeSort(A,0,MAX-1);
for(i=0;i<MAX;i++)
printf("%d ",A[i]);
}
void MergeSort(int Arr[], int low, int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
MergeSort(Arr,low,mid);
MergeSort(Arr,mid+1,high);
Merge(Arr,low,mid,high);
}
}
void Merge(int Arr[], int low, int mid, int high)
{
int l,m,i,k;
int temp[MAX];
l=i=low;
m=mid+1;
while((l<=mid) && (m<=high))
{
if(Arr[l]<=Arr[m])
{
temp[i]=Arr[l];
l++;
}
else
{
temp[i]=Arr[m];
m++;
}
i++;
}
if(l>mid)
{
for(k=m;k<=high;k++)
{
temp[i]=Arr[k];
i++;
}
}
else
{
for(k=l;k<=mid;k++)
{
temp[i]=Arr[k];
i++;
}
}
for(k=low;k<=high;k++)
Arr[k]=temp[k];
}
Time Complexity: O(n logn)
Insertion Sort ( reverse)
Orginal insertion sort will sort elements from starting of the array. This program will sort the elements from end of the array.
/*Insertion Sort*/
#include<stdio.h>
void insertion(int A[], int n);
void print_array(int A[],int n);
main()
{
int arr[]={7,6,5,4,2,3,1,8,10,9}; // Input Array
int size = (int)(sizeof(arr)/sizeof(arr[0])); //Calculate size of array
int itr ; // Iterator
insertion(arr,size); // Insertion function
print_array(arr,size);
}
void print_array(int A[], int n)
{
int itr;
// Printing array after sorting
for(itr=0;itr<n;itr++)
printf("%d ",A[itr]);
}
void insertion(int A[], int n)
{
int i , j , key;
for(j=n-1;j>=0;j--)
{
key=A[j];
i=j+1;
while((i<=n)&&(A[i]<key))
{
A[i-1]=A[i];
i=i+1;
}
A[i-1]=key;
}
}