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: }
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 Heap Sort. Show all posts
Showing posts with label Heap Sort. Show all posts
Tuesday, 29 September 2015
Write a C Program To perform Heap Sort in Data Structure(Sorting Techniques)
Subscribe to:
Posts (Atom)