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: }
getsprogramming Tutorials , C Programming, CPP Programming and JAVA Programming , Python Programming , Javascript Programming Welcome to getsprogramming Blog Here You Find Different Programs in Easier way to Explain and gain Knowledge and Try Your Self..Thanks
Showing posts with label sorting techniques. Show all posts
Showing posts with label sorting techniques. Show all posts
Tuesday, 29 September 2015
Write a C rogram To Perform Radix Sort in Data Structure
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: }
Write a C Program To perform Heap Sort in Data Structure(Sorting Techniques)
1: #include<stdio.h>
2: #include<conio.h>
3: void heapsort(int a[50], int n);
4: void heapify(int[], int);
5: void adjust(int[], int);
6: void main()
7: {
8: int a[50],n,i;
9: clrscr();
10: printf("Enter the no. of elements to be sorted:\n");
11: scanf("%d",&n);
12: printf("Enter the elements:\n");
13: for(i=0;i<n;i++)
14: {
15: scanf("%d",&a[i]);
16: }
17: printf("\nArray elements before Heapsort:\n");
18: for(i=0;i<n;i++)
19: {
20: printf("%d\t",a[i]);
21: }
22: heapsort(a,n);
23: printf("\nArray elements after Heapsort:\n");
24: for(i=0;i<n;i++)
25: {
26: printf("%d\t",a[i]);
27: }
28: getch();
29: }
30: void heapsort(int a[50], int n)
31: {
32: int i,t;
33: heapify(a,n);
34: for(i=n-1;i>0;i--)
35: {
36: t=a[0];
37: a[0]=a[i];
38: a[i]=t;
39: adjust(a,i);
40: }
41: }
42: void heapify(int a[50], int n)
43: {
44: int item,i,j,k;
45: for(k=1;k<n;k++)
46: {
47: item=a[k];
48: i=k;
49: j=(i-1)/2;
50: while((i>0) && (item>a[j]))
51: {
52: a[i] = a[j];
53: i=j;
54: j=(i-1)/2;
55: }
56: a[i]=item;
57: }
58: }
59: void adjust(int a[50], int n)
60: {
61: int item,i,j;
62: j=0;
63: item=a[j];
64: i=2*j+1;
65: while(i<=n-1)
66: {
67: if(i+1<=n-1)
68: if(a[i]<a[i+1])
69: i++;
70: if(item<a[i])
71: {
72: a[j]=a[i];
73: j=i;
74: i=2*j+1;
75: }
76: else
77: break;
78: }
79: a[j]=item;
80: }
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);
}
}
/* 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();
}
#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();
}
Subscribe to:
Posts (Atom)