Showing posts with label data structure. Show all posts
Showing posts with label data structure. Show all posts

Tuesday, 29 September 2015

Write a C rogram To Perform Radix Sort in Data Structure

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void radix_sort(int arr[], int n);  
4:  void main()  
5:  {  
6:   int a[20],n,i;  
7:   clrscr();  
8:   printf("Enter the number of elements to be sorted:\n");  
9:   scanf("%d",&n);  
10:   printf("Enter the elements:\n");  
11:   for(i=0;i<n;i++)  
12:   {  
13:   scanf("%d",&a[i]);  
14:   }  
15:   printf("\nArray elements before Radix Sort:\n");  
16:   for(i=0;i<n;i++)  
17:   {  
18:   printf("%d\t",a[i]);  
19:   }  
20:   radix_sort(a,n);  
21:   printf("\nArray elemants after Radix Sort:\n");  
22:   for(i=0;i<n;i++)  
23:   {  
24:   printf("%d\t",a[i]);  
25:   }  
26:   getch();  
27:  }  
28:  void radix_sort(int arr[],int n)  
29:  {  
30:   int bucket[10][5],buck[10],b[10];  
31:   int i,j,k,l,num,div,large,passes;  
32:   div=1;  
33:   num=0;  
34:   large=arr[0];  
35:   for(i=0;i<n;i++)  
36:   {  
37:   if(arr[i]>large)  
38:   {  
39:    large=arr[i];  
40:   }  
41:   while(large>0)  
42:   {  
43:    num++;  
44:    large=large/10;  
45:   }  
46:   for(passes=0;passes<num;passes++)  
47:   {  
48:    for(k=0;k<10;k++)  
49:    {  
50:    buck[k]=0;  
51:    }  
52:    for(i=0;i<n;i++)  
53:    {  
54:    l=((arr[i]/div)%10);  
55:    bucket[l][buck[l]++]=arr[i];  
56:    }  
57:    i=0;  
58:    for(k=0;k<10;k++)  
59:    {  
60:    for(j=0;j<buck[k];j++)  
61:    {  
62:     arr[i++]=bucket[k][j];  
63:    }  
64:    }  
65:    div*=10;  
66:   }  
67:   }  
68:  }  

Write a C Program To Perform Shell Sort

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void main()  
4:  {  
5:   int a[20];  
6:   int i,j,k,temp,n;  
7:   clrscr();  
8:   printf("Enter the number of elements to be sorted:\n");  
9:   scanf("%d",&n);  
10:   printf("Enter the elements:\n");  
11:   for(i=0;i<n;i++)  
12:   {  
13:   scanf("%d",&a[i]);  
14:   }  
15:   for(i=n/2;i>0;i=i/2)  
16:   {  
17:   for(j=i;j<n;j++)  
18:   {  
19:    for(k=j-i;k>=0;k=k-i)  
20:    {  
21:    if(a[k+i]>=a[k])  
22:     break;  
23:    else  
24:    {  
25:     temp=a[k];  
26:     a[k]=a[k+i];  
27:     a[k+i]=temp;  
28:    }  
29:    }  
30:   }  
31:   }  
32:   printf("\nArray elements after shell sorting are:\n");  
33:   for(i=0;i<n;i++)  
34:   {  
35:   printf("%d\t",a[i]);  
36:   }  
37:   getch();  
38:  }  

Write a C Program That performing Merge Sort(Sorting Techniues)

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #define MAX 20  
4:  void mergesort(int a[],int low,int high);  
5:  void merge(int a[],int beg,int mid,int end);  
6:  void display(int a[],int num);  
7:  void main()  
8:  {  
9:   int n,i,a[MAX];  
10:   clrscr();  
11:   printf("Enter the number of elements (<MAX )\n");  
12:   scanf("%d",&n);  
13:   printf("Enter the elements\n");  
14:   for(i=0;i<n;i++)  
15:   {  
16:   scanf("%d",&a[i]);  
17:   }  
18:   printf("\nArray elements before sorting are:\n");  
19:   display(a,n);  
20:   mergesort(a,0,n-1);  
21:   printf("\nArray elements after sorting are:\n");  
22:   display(a,n);  
23:   getch();  
24:  }  
25:  void mergesort(int a[],int low,int high)  
26:  {  
27:   int mid;  
28:   if(low<high)  
29:   {  
30:   mid=(low+high)/2;  
31:   mergesort(a,low,mid);  
32:   mergesort(a,mid+1,high);  
33:   merge(a,low,mid,high);  
34:   }  
35:  }  
36:  void merge(int a[],int beg, int mid,int end)  
37:  {  
38:   int temp,b[MAX],left,right,i;  
39:   left=beg;  
40:   right=mid+1;  
41:   temp=beg;  
42:   while((left<=mid) && (right<=end))  
43:   {  
44:   if(a[left]<a[right])  
45:   {  
46:    b[temp]=a[left];  
47:    left=left+1;  
48:   }  
49:   else  
50:   {  
51:    b[temp]=a[right];  
52:    right=right+1;  
53:   }  
54:   temp=temp+1;  
55:   }  
56:   while(left<=mid)  
57:   {  
58:   b[temp]=a[left];  
59:   left=left+1;  
60:   temp=temp+1;  
61:   }  
62:   while(right<=end)  
63:   {  
64:   b[temp]=a[right];  
65:   right=right+1;  
66:   temp=temp+1;  
67:   }  
68:   for(i=beg;i<=end;i++)  
69:   {  
70:   a[i]=b[i];  
71:   }  
72:  }  
73:  void display(int a[],int num)  
74:  {  
75:   int i;  
76:   for(i=0;i<num;i++)  
77:   {  
78:   printf("%d\t",a[i]);  
79:   }  
80:  }  

Tuesday, 22 September 2015

Write a C program to create linear linked list interactively and print out the list and total number of items in the list.

Write a C program to create linear linked list interactively and print out the list and total number of items in the list.
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<process.h>  
4:  #include<stdlib.h>  
5:  struct node  
6:  {  
7:        int info;  
8:        struct node *next;  
9:  }*head,*last,*temp,*n;  
10:  void menu();  // to select the choise  
11:  void create(); // insert at last  
12:  void display(); // to print the link list  
13:  int i;  
14:  void main()  
15:  {  
16:        i=0;  
17:        clrscr();  
18:        menu();  
19:        getch();  
20:  }  
21:  void menu()  
22:  {  
23:        int ch;  
24:        printf("1. Create\n");  
25:        printf("2. Display\n");  
26:        printf("3. Exit\n");  
27:        printf("Enter choise : ");  
28:        scanf("%d",&ch);  
29:        switch(ch)  
30:        {  
31:              case 1 : create();  
32:              case 2 : display();  
33:              case 3 : printf("You have choose to exit\n");  
34:                     getch();  exit(0);  
35:              default : printf("Invalid choise\n"); menu();  
36:        }  
37:  }  
38:  void create()  
39:  {  
40:        n= new node;  
41:        printf("Enter the information : ");  
42:        scanf("%d",&n->info);  
43:        if(i==0)  
44:        {  
45:              head=n;  
46:              i++;  
47:        }  
48:        else  
49:        {  
50:              last->next=n;  
51:              i++;  
52:        }  
53:        last=n;  
54:        last->next=NULL;  
55:        menu();  
56:  }  
57:  void display()  
58:  {  
59:        if(head==NULL)  
60:        {  
61:              printf("No information in list\n");  
62:              menu();  
63:        }  
64:        temp=head;  
65:        while(temp!=NULL)  
66:        {  
67:              printf("%d->",temp->info);  
68:              temp=temp->next;  
69:        }  
70:        printf("\n");  
71:        printf("Total number of elements in the link list are : %d\n",i);  
72:        menu();  
73:  }  
74:  Output:  
75:  1. Create  
76:  2. Display  
77:  3. Exit  
78:  Enter choise : 1  
79:  Enter the information : 12  
80:  1. Create  
81:  2. Display  
82:  3. Exit  
83:  Enter choise : 1  
84:  Enter the information : 23  
85:  1. Create  
86:  2. Display  
87:  3. Exit  
88:  Enter choise : 1  
89:  Enter the information : 45  
90:  1. Create  
91:  2. Display  
92:  3. Exit  
93:  Enter choise : 2  
94:  12->23->45->  
95:  Total number of elements in the link list are : 3  
96:  1. Create  
97:  2. Display  
98:  3. Exit  
99:  Enter choise : 3  
100:  You have choose to exit  

Wednesday, 12 August 2015

Write a C Program To Implement Quick Sort

 Write a program to implement Quick sort.
/* program to sort elements of an array using Quick Sort */
#include<stdio.h>

void quicksort(int[ ],int,int);
void main( )
{
int low, high, pivot, t, n, i, j, a[10];
clrscr( );
printf("\nHow many elements you want to sort ? ");
scanf("%d",&n);
printf("\Enter elements for an array:");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
low=0;
high=n-1;
quicksort(a,low,high);
printf("\After Sorting the elements are:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch( );
}
void quicksort(int a[ ],int low,int high)
{
int pivot,t,i,j;
if(low<high)
{
pivot=a[low];
i=low+1;
j=high;
while(1)
{

while(pivot>a[i]&&i<=high)
i++;
while(pivot<a[j]&&j>=low)
j--;
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
else
break;
}
a[low]=a[j];
a[j]=pivot;
quicksort(a,low,j-1);
quicksort(a,j+1,high);
}
}

Write a C Program To Implement Bubble Sort

/* bubble sort implementation */
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,n,temp,j,arr[25];
    clrscr();
    printf("Enter the number of elements in the Array: ");
    scanf("%d",&n);
    printf("\nEnter the elements:\n\n");
    for(i=0 ; i<n ; i++)
    {
        printf(" Array[%d] = ",i);
        scanf("%d",&arr[i]);
    }   
    for(i=0 ; i<n ; i++)
    {
        for(j=0 ; j<n-i-1 ; j++)
        {
            if(arr[j]>arr[j+1]) //Swapping Condition is Checked
            {
                temp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=temp;
            }
        }
    }
        printf("\nThe Sorted Array is:\n\n");
        for(i=0 ; i<n ; i++)
{
printf(" %4d",arr[i]);
}
getch();
}