Skip to main content

Posts

a simple example of linked list

working sample code-> https://stackblitz.com/edit/typescript-mzgnay operation done:- add item is a linked list->at last add item at starting remove item by value remove item by index insert item before insert item after reverse linked list code:- //Linked list --------------------------------------------------- //create a class which holds data and address of next class //newNode class newNode { //data which can store any type of data but right now i am not handling array and object data type only string or number data : any ; next : newNode ; } class LinkedList { //Head store first node in linked list Head : newNode = null ; //last node in the linked list Tail : newNode = null ; //length of linked list len : number = 0 ; /* push a node at last of linked list */ public push ( data : any ) //push data { //create a new node---step1 var node = new newNode ();...

draw a 4*4 matrix in typescript and exchange two column values based on row index

working link https://stackblitz.com/edit/typescript-omoaq4 code => //draw a matrix and exchange two column values in the matrix based on given row index class matrix { static rm =[]; static rc =[]; static drawMatrix ( r : number , c : number ) { //n2 var k = 0 ; for ( let i = 0 , k = 0 ; i < r ; i ++) { this . rc =[]; for ( let j = 0 ; j < c ; j ++, k ++) { this . rc . push ( String . fromCharCode ( 64 +( k + 1 ))); } this . rm . push ( this . rc ); } } static exchangeRowData ( rowIndex , col1 , col2 ) { let currentRow = this . rm [ rowIndex ]; let temp = currentRow [ this . getIndex ( currentRow , col1 )]; let index = this . getIndex ( currentRow , col2 ); currentRow [ this . getIndex ( currentRow , col1 )]= currentRow [ index ]; currentRow [ index ]= t...

how to get min and max from array and return sum of all elements except min and max in typescript

working link https://stackblitz.com/edit/typescript-ewrnkd code=> //get minimum and maximum and then class Algo { static solve ( arr : Array < number >) { let max = 0 ; let min = 0 ; max = arr [ 0 ]; min = arr [ 0 ]; for ( var i = 0 ; i < arr . length ; i ++) { if ( arr [ i + 1 ]> max ) { max = arr [ i + 1 ] } if ( arr [ i + 1 ]< min ) { min = arr [ i + 1 ] } } return { "minSum" : this . sumWithMaxMin ( arr , max ), "maxSum" : this . sumWithMaxMin ( arr , min )} } static sumWithMaxMin ( arr , n ): number { let sum = 0 ; for ( let i = 0 ; i < arr . length ; i ++) { if ( arr [ i ]!= n ) { sum += arr [ i ]; } } return sum ; } } var o = Algo . solve ([ 1 , 2 , 3 , 4 , 5 ]); console . log ( o . max...

classic merge short in typescript

working example https://stackblitz.com/edit/typescript-lchwhz Please remember this is not the best way to implement merge short in javascript but its a classic way using without any inbuilt API method. more detail about merge sort is=> https://www.youtube.com/watch?v=TzeBrDU-JaY //merge sort in typescript using classic way you can easliy reduce this method to sorter one depends on your requirement class sortAlgo { static sort ( myArray : Array < number >) { //left bound let left = 0 ; //right bound just one less than total array length let right = myArray . length - 1 ; //divide array in to small part this . divideArray ( myArray , left , right ); } static divideArray ( myArray : Array < number >, left : number , right : number ) { //recursive call util left < right if ( left < right ) { //find mid to preventing stack overflow right-1 ...

quicksort in typescript a quick example how to implement quicksort

working link https://stackblitz.com/edit/typescript-vqzrsa //quicksort implementation in typescript //full detail please check https://www.youtube.com/watch?v=COk73cpQbFQ class Quicksort { //pass arry static sort ( arry : Array < number >) { //get start index let start = 0 ; //get end index which is one less tahn total length let end = arry . length - 1 ; //pass arry with start and end index to the function quickSort this . quickSort ( arry , start , end ); } static quickSort ( arry : Array < number >, start : number , end : number ) { //do sorting using divide and conquer if ( start < end ) { //get a pivot index let pindex = this . getPivotIndex ( arry , start , end ); this . quickSort ( arry , start , pindex - 1 ); this . quickSort ( arry , pindex + 1 , end ); } } //swap element static swap ( arry : Array < number >, start : number , index : number ) { let temp = arry [ s...

Insertion Sort in javascript with working link

https://stackblitz.com/edit/js-bbrsjn //insertionSort function insertionSort ( arry ) { var total = arry . length ; var value , index ; for ( var i = 1 ; i < total ; i ++) { value = arry [ i ];    index = i ; while ( index > 0 && value < arry [ index - 1 ]) { arry [ index ]= arry [ index - 1 ]; index = index - 1 ; } arry [ index ]= value ; } return arry ; } var arry =[ 90 , 222 , 998 , 117 , 2 ]; insertionSort ( arry ); console . log ( arry );

small example how to implement bubble short in javascript with time complexity O(n*n)

working example https://stackblitz.com/edit/js-faixur code :- function swap ( myArray , to , from ) { var temp = myArray [ to ]; myArray [ to ]= myArray [ from ]; myArray [ from ]= temp ; } function BubbleSort ( arr ) { for ( let i = 0 ; i < arr . length ; i ++) { for ( let x = 0 ; x < arr . length - 1 ; x ++) { if ( arr [ x ]< arr [ x + 1 ]) { swap ( myArray ,( x ), x + 1 ); } } } } var myArray =[]; for ( var i = 0 ; i < 20 ; i ++) { myArray . push ( Math . round ( Math . random ()* 1000 )); } console . time (); BubbleSort ( myArray ); console . log ( myArray ); console . timeEnd ();