Skip to main content

Posts

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) {                     ...

Just little basic about recursion dynamic programming and memorization using some simple example in javascript

working sample https://stackblitz.com/edit/js-h3abpu recursion  and memorization these three are the 2 important pillar of computer programming when you are dealing with big task and you want to break your task to smaller parts. let take a example a simple example you have a array and you want to find to maximum value form that array. so let try to solve this problem using recursion //Find a maximum value in a unsorted array //here how i can find max item from a array using recursion function findMax ( arr , len ) { //if length is 1 return first item if ( len == 1 ) { return arr [ 0 ]; } //else recursively search the next maximum item in the array return Math . max ( arr [ len - 1 ], findMax ( arr , len - 1 )) }   now find the minimum item in array function findMin ( arr , len = arr . length ) { if ( len == 1 ) { return arr [ 0 ]; } return Math . min ( arr [ len - 1 ], findMin ( arr , ...
working link  https://stackblitz.com/edit/js-wnzbgw rotate a array (right direction) k times //Right Rotation of a Array -----------------------using O(n) space and O(n) time complexity var myArray =[ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]; function RotationRightFirstMethod ( no_of_times ) { var newArray =[]; var t = myArray . length ; for ( var i = 0 ; i < t ; i ++) { newArray [( i + no_of_times )% t ]= myArray [ i ]; } return newArray ; } console . log ( "result 1" , RotationRightFirstMethod ( 4 )); //lets try using some my own method ---------- var myArray2 =[ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]; function RotationRightSecondMethod ( n ) { var firstArray =[]; var secondArray =[]; for ( var i = 0 ; i < myArray2 . length - n ; i ++) { firstArray . push ( myArray2 [ i ]) } for ( var i = myArray2 . length - n ; i < myArray2 . length ; i ++) { secondArray . push ( myArray2 [ i ]); } return secondArray . c...

polyfill for map and filter method in javascript

working link==> https://stackblitz.com/edit/js-wpw67a //check out filter map and reduce once //create your own map function or polyfill for map //map callback accept three parameter one value second index and third one is a current array //create a polyfill for map function Array . prototype . myMap = function ( callBack ) { if ( typeof callBack != "function" ) { throw new TypeError (); } var newArray =[]; for ( var i = 0 ; i < this . length ; i ++) { //this is current Array //i is index //this[i] is current item newArray . push ( callBack . apply ( null ,[ this [ i ], i , this ])); } return newArray ; } var myArray =[ 1 , 2 , 3 , 4 , 5 ]; var newArray = myArray . myMap (( value , index )=>{ return value * index ; }) console . log ( newArray ); //now polyfill for filter which accept three parameter one value second index and...