File tree Expand file tree Collapse file tree 2 files changed +111
-0
lines changed Expand file tree Collapse file tree 2 files changed +111
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+ vector <bool > vis ;
5+ vector < vector <int > > adj ;
6+
7+ void BFS (int s)
8+ {
9+ queue <int > q ;
10+ q.push (s) ;
11+ vis[s] = true ;
12+ while (!q.empty ())
13+ {
14+ int f = q.front () ;
15+ q.pop () ;
16+ cout<<f<<" " ;
17+ for (auto u : adj[f])
18+ {
19+ if (vis[u]==false )
20+ {
21+ q.push (u) ;
22+ vis[u] = true ;
23+ }
24+ }
25+ }
26+
27+ }
28+
29+ int main ()
30+ { cout<<" Enter number of Nodes and Edges\n " ;
31+ int n , e ;
32+ cin>>n>>e ;
33+ vis.assign (n ,false ) ;
34+ adj.assign (n , vector <int > ()) ;
35+ int x , y ;
36+ cout<<" Enter Edges\n " ;
37+ for (int i = 0 ; i < e; i++) {
38+ cin>>x>>y ;
39+ adj[x].push_back (y) ;
40+ adj[y].push_back (x) ;
41+ }
42+ cout<<" BFS path from source node 0 : " ;
43+ BFS (0 ) ;
44+ }
Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+
6+
7+
8+ void heapify (int arr[], int n, int i)
9+ {
10+ int largest = i;
11+ int l = 2 *i + 1 ;
12+ int r = 2 *i + 2 ;
13+
14+ if (l < n && arr[l] > arr[largest])
15+ largest = l;
16+
17+ if (r < n && arr[r] > arr[largest])
18+ largest = r;
19+
20+ if (largest != i)
21+ {
22+ swap (arr[i], arr[largest]);
23+ heapify (arr, n, largest);
24+ }
25+ }
26+
27+ void heapSort (int arr[], int n)
28+ {
29+
30+ for (int i = n / 2 - 1 ; i >= 0 ; i--)
31+ heapify (arr, n, i);
32+
33+
34+ for (int i=n-1 ; i>0 ; i--)
35+ {
36+
37+ swap (arr[0 ], arr[i]);
38+
39+
40+ heapify (arr, i, 0 );
41+ }
42+ }
43+ int main ()
44+ {
45+ int n ;
46+ cout<<" Enter Size of Array\n " ;
47+ cin>>n ;
48+ cout<<" Enter Numbers\n " ;
49+ int arr[n] ;
50+ for (int i = 0 ; i<n ; i++)
51+ {
52+ cin>>arr[i] ;
53+ }
54+ cout<<" Unsorted Array: " ;
55+ for (int i = 0 ; i<n ; i++)
56+ {
57+ cout<<arr[i]<<" " ;
58+ }
59+ cout<<endl ;
60+ cout<<" Sorted Array: " ;
61+ heapSort (arr ,n ) ;
62+ for (int i = 0 ; i<n ; i++)
63+ {
64+ cout<<arr[i]<<" " ;
65+ }
66+
67+ }
You can’t perform that action at this time.
0 commit comments