Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
HeapDoubLinArr
//g++ 7.4.0 //Sorting and searching template array using heap sort and linear search in double linked list //heapSort, heapify codes credit to Geeks for Geeks //template modifications,swap and test driver codes created by Rezaul Hoque on April 12,2021 //please contact at jewelmrh@yahoo.com #include <iostream> #include <bits/stdc++.h> using namespace std; template <class T> class node{ public: T val; int p; int q; node<T>* next; node<T>* prev; void heapSort(node<T> a[], int n); void heapify(node<T> a[],int n, int i); int lins( node<T> a[], int n, T thing); void swap(node<T>& x, node<T>& y); }; template <class T> int node<T>::lins(node<T> a[], int n, T thing) { for(int i=0; i<n; i++){ if(a[i].val ==thing) { return i; } } return -1; } template <class T> void node<T>:: heapify(node<T> arr[], int n, int i) { int largest = i; int l = 2 * i + 1; int r = 2 * i + 2; if (l < n && arr[l].val> arr[largest].val) largest = l; if (r < n && arr[r].val> arr[largest].val) largest = r; if (largest != i) { swap(arr[i], arr[largest]); heapify(arr, n, largest); } } template <class T> void node<T>:: heapSort(node<T> arr[], int n) { for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); for (int i = n - 1; i >= 0; i--) { swap(arr[0], arr[i]); heapify(arr, i, 0); } } template <class T> void node<T>:: swap(node<T>& x, node<T>& y) { node<T> temp; temp = x; x=y; y=temp; } int main() { const int max=3; node<char>* a; a = new node<char>[max]; a[0].prev=NULL; a[2].next = NULL; for(int i=0; i<max; i++) { a[i].next = a[i+2].prev; } a[0].val={'E'}; a[1].val={'D'}; a[2].val={'C'}; a[0].p={4}; a[1].p={2}; a[2].p = {6}; a[0].q={5}; a[1].q={4}; a[2].q = {8}; cout<<"After heap sort:\n"; a->heapSort(a, 3); for(int i=0; i<max; i++) { cout<<a[i].val<<"\t"<<a[i].p<<"\t"<<a[i].q<<endl; cout<<"\n"; } char thing; cout<<"Enter the thing you look for:\n"; cin>>thing; if(a->lins(a,3,thing) != -1) { cout<<thing<<" is at a["<<a->lins(a, 3,thing)<<"].\n"; } else { cout <<thing<<" is not found.\n"; } return 0; }
run
|
edit
|
history
|
help
0
Test 15(2020)
Dar
Sieve Of Eratosthenes
VecHotel
20201123
Elevator
Dar
matrix calculator
kadane's algorithm 2
OverloadFunc