Skip to main content

Posts

Showing posts from November, 2019

Merge sort algorithm in a classic way example contain c++ code using vector

Merge sort  one of the highly used algorithm for sorting data check out this video for more detail https://www.youtube.com/watch?v=TzeBrDU-JaY Benefits over other sorting algorithms for  faster than bubble sort, insertion sort and selection sort. always used when time complexity is your first priority over space complexity. when data is huge 1000+ records but space complexity is not a problem. huge unsorted data means lots of records is unsorted. don't use for small range where data is already sorted or only a few records needed to sort use selection sort in this case. take's O(n) space because its a divide a conquer algorithm. running code just copy and paste and run it. // #include <iostream> #include <vector> #include <list> using namespace std ; //@why merge sort //faster than bubble sort //faster than insertion sort //KeyNote //divide recursively array/vector into smaller part...

how to create a hashtable in typescript which can store both number and string using generic code

running sample https://stackblitz.com/edit/typescript-2zlayw code=> //hash node class   HashNode < T , U > {     key : T     value : U      constructor ( _key : T , _value : U )      {          if ( typeof  _key === "object" )            throw   new   Error ( "object as key not allowed" )          this . key = _key ;          this . value = _value ;      }      //get key      public  getKey ()      {         return   this . key ;      }      //get value   ...

Different methods to copy in C++ STL and its uses with code

// //   main.cpp //   Exp // //   Created by Manish on 07/11/19. //   Copyright © 2019 Manish. All rights reserved. // #include <iostream> #include <vector> #include <algorithm> using namespace std ; int main( int argc, const char * argv[]) {     //1     //copy**************************************************     vector < string > sourceA={ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" };     vector < string > dest={ "A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" };     //copy a to d and replace A to D in dest     copy (sourceA. begin (), sourceA. begin ()+ 4 , dest. begin ());          for ( auto x:dest)     {         cout <<x<< endl ;     }               cout << "-------------------...

INSERTION SORT IN C++ ( PART1 OF SORTING ALGORITHMS)

// //   main.cpp //   Exp // //   Created by Manish on 07/11/19. //   Copyright © 2019 Manish. All rights reserved. // #include <iostream> #include <vector> using namespace std ; vector < int > insertionSort( vector < int > &myVector) {    //left index     int leftIndex;     //key element     int key;     //loop from first index to length-1     for ( int i= 1 ;i!=myVector. size ();i++)     {         //store first element in inside key         key=myVector[ i ];         leftIndex=i- 1 ;         //swap element until left is greator than right and leftindex >=0         while (leftIndex>= 0 && myVector[ leftIndex ]>key) {                     ...