Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MergeDoubLinArr
//g++ 7.4.0 //Sorting and searching template array using merge sort and linear search //Merge sort,merge codes credit to JavaTPoint //template modifications,test driver codes created by Rezaul Hoque on April 09,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 mergeSort(node<T> a[], int start,int fin); void merge(node<T> a[],int start, int mid, int fin); int lins( node<T> a[], int n, T thing); }; 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>:: mergeSort(node<T> a[], int start, int fin) { int mid; if(start < fin) { mid = (start +fin)/2; mergeSort(a,start,mid); mergeSort(a,mid+1,fin); merge(a,start,mid,fin); } } template <class T> void node<T>::merge(node<T> a[], int start, int mid, int fin) { int i=start,j=mid+1,k,index = start; node<T> temp[10]; while(i<=mid && j<=fin) { if(a[i].val<a[j].val) { temp[index] = a[i]; i = i+1; } else { temp[index] = a[j]; j = j+1; } index++; } if(i>mid) { while(j<=fin) { temp[index]= a[j]; index++; j++; } } else { while(i<=mid) { temp[index]= a[i]; index++; i++; } } k = start; while(k<index) { a[k]=temp[k]; k++; } } 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 merge sort:\n"; a->mergeSort(a, 0,2); 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
Fungsi dan for
Policy based smart pointer
543
Two pointer - MUST DO
append
FAK MEN
1068 - Investigation
Right view of a tree
Kolokwium_2011_z7
3SUM problem