Breaking News
recent

Please Help Community by G+

Heap Creation and Sorting in C++ with checks free source code

Heap Data Structure Creation


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>

using namespace std;

void heapCreation(int item);
void heapSorting();

int i, p, n=0;
const int heapSize=100;
int arr[heapSize];

void heapCreation(int item){
    if(n>=heapSize){
    cout<<"Heap is saturated: Insertion is void"<<endl;
    return;
}
else{
    int temp;
    cout<<item<<endl;
    n=n+1;
    arr[n]=item;
    i=n;
    p=i/2;
    while(p>0 && arr[p]<arr[i]){
        temp=arr[i];
        arr[i]=arr[p];
        arr[p]=temp;
        i=p;
        p=p/2;
    }
}
}

void heapSorting(){
    int item, j, temp, lchild, rchild, i=n;
    cout<<"Heap in sorted order"<<endl;
    while(i>=1){
        item=arr[1];
        arr[1]=arr[i];
        cout<<item<<endl;
        i=i-1;
        j=1;
        while(j<i){
            lchild=j*2;
            rchild=j*2+1;
            if(arr[j]<arr[lchild] && arr[lchild]>arr[rchild]){
                temp=arr[j];
                arr[j]=arr[lchild];
                arr[lchild]=temp;
                j=lchild;
            }
            else if(arr[j]<arr[rchild] && arr[rchild]>arr[lchild]){
                temp=arr[j];
                arr[j]=arr[rchild];
                arr[rchild]=temp;
                j=rchild;
            }
            else{
                break;
            }
        }
    }
}

int main()
{
    heapCreation(6);
    heapCreation(65);
    heapCreation(89);
    heapCreation(34);
    heapCreation(2);
    heapSorting();
}

Click on the download button for full source code

 Download Now

Hassnain Jamil

Hassnain Jamil

No comments:

Post a Comment

Powered by Blogger.