codeparrot/xlcost-text-to-code huggingface dataset:
Minimize cost to convert all characters of a binary string to 0 s | Function to get the minimum Cost to convert all characters of given string to 0 s ; Stores the range of indexes of characters that need to be flipped ; Stores the number of times current character is flipped ; Stores minimum cost to get the required string ; Traverse the given string ; Remove all value from pq whose value is less than i ; Update flip ; If current character is flipped odd times ; If current character contains non - zero value ; Update flip ; Update cost ; Append R [ i ] into pq ; Driver Code ; Function call
function minCost ( str , N , R , C ) { str = str . split ( ' ' ) ; var pq = [ ] ; var flip = 0 ; var cost = 0 ; for ( var i = 0 ; i < N ; i ++ ) { while ( pq . length > 0 && pq [ pq . length - 1 ] < i ) { pq . pop ( ) ; flip -- ; } if ( flip % 2 == 1 ) { str [ i ] = String . fromCharCode ( ' ' . charCodeAt ( 0 ) - str [ i ] . charCodeAt ( 0 ) + ' ' . charCodeAt ( 0 ) ) ; } if ( str [ i ] == ' ' ) { flip ++ ; cost += C [ i ] ; pq . push ( R [ i ] ) ; } pq . sort ( ( a , b ) => b - a ) ; } return cost ; } var str = " " ; var R = [ 1 , 2 , 2 , 3 ] ; var C = [ 3 , 1 , 2 , 3 ] ; var N = str . length ; document . write ( minCost ( str , N , R , C ) ) ;
Maximum distance between two points in coordinate plane using Rotating Caliper 's Method | Function calculates distance between two points ; Function to find the maximum distance between any two points ; Iterate over all possible pairs ; Update maxm ; Return actual distance ; Driver Code Number of points ; Given points ; Function Call
function dist ( p1 , p2 ) { var x0 = p1 [ 0 ] - p2 [ 0 ] ; var y0 = p1 [ 1 ] - p2 [ 1 ] ; return x0 * x0 + y0 * y0 ; } function maxDist ( p ) { var n = p . length ; var maxm = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { maxm = Math . max ( maxm , dist ( p [ i ] , p [ j ] ) ) ; } } return Math . sqrt ( maxm ) ; } var n = 5 ; var p = [ ] ; p . push ( [ 4 , 0 ] ) ; p . push ( [ 0 , 2 ] ) ; p . push ( [ - 1 , - 7 ] ) ; p . push ( [ 1 , 10 ] ) ; p . push ( [ 2 , - 3 ] ) ; document . write ( maxDist ( p ) ) ;
Reorder an array such that sum of left half is not equal to sum of right half | Function to print the required reordering of array if possible ; Sort the array in increasing order ; If all elements are equal , then it is not possible ; Else print the sorted array arr ; Given array ; Function call
function printArr ( arr , n ) { arr . sort ( ) ; if ( arr [ 0 ] == arr [ n - 1 ] ) { document . write ( " " ) ; } else { document . write ( " " ) ; for ( i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } } var arr = [ 1 , 2 , 2 , 1 , 3 , 1 ] ; var N = arr . length ; printArr ( arr , N ) ;
Maximize Array sum by swapping at most K elements with another array | Function to find the maximum sum ; If element of array a is smaller than that of array b , swap them . ; Find sum of resultant array ; Driver Code
function maximumSum ( a , b , k , n ) { let i , j ; a . sort ( ) ; b . sort ( ) ; for ( i = 0 , j = n - 1 ; i < k ; i ++ , j -- ) { if ( a [ i ] < b [ j ] ) { let temp = a [ i ] ; a [ i ] = b [ j ] ; b [ j ] = temp ; } else break ; } let sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += a [ i ] ; document . write ( sum ) ; } let K = 1 ; let A = [ 2 , 3 , 4 ] ; let B = [ 6 , 8 , 5 ] ; let N = A . length ; maximumSum ( A , B , K , N ) ;
Find if array can be sorted by swaps limited to multiples of k | CheckSort function To check if array can be sorted ; sortarr is sorted array of arr ; If k = 1 then ( always possible to sort ) swapping can easily give sorted array ; Comparing sortarray with array ; Element at index j must be in j = i + l * k form where i = 0 , 1 , 2 , 3. . . where l = 0 , 1 , 2 , 3 , . . n - 1 ; If element is present then swapped ; If element of sorted array does not found in its sequence then flag remain zero that means arr can not be sort after swapping ; If flag is 0 Not possible else Possible ; Size of step ; Array initialized ; Length of arr ; Calling function
function CheckSort ( arr , k , n ) { var sortarr = arr . sort ( ( a , b ) => a - b ) ; if ( k === 1 ) document . write ( " " ) ; else { var flag = 0 ; for ( var i = 0 ; i < n ; i ++ ) { flag = 0 ; for ( var j = i ; j < n ; j += k ) { if ( sortarr [ i ] === arr [ j ] ) { var tmp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = tmp ; flag = 1 ; break ; } if ( j + k >= n ) { break ; } } if ( flag === 0 ) { break ; } } if ( flag === 0 ) document . write ( " " ) ; else document . write ( " " ) ; } } var k = 3 ; var arr = [ 1 , 5 , 6 , 9 , 2 , 3 , 5 , 9 ] ; var n = arr . length ; CheckSort ( arr , k , n ) ;
Check whether an array can be made strictly decreasing by modifying at most one element | Function that returns true if the array can be made strictly decreasing with at most one change ; To store the number of modifications required to make the array strictly decreasing ; Check whether the last element needs to be modify or not ; Check whether the first element needs to be modify or not ; Loop from 2 nd element to the 2 nd last element ; Check whether arr [ i ] needs to be modified ; Modifying arr [ i ] ; Check if arr [ i ] is equal to any of arr [ i - 1 ] or arr [ i + 1 ] ; If more than 1 modification is required ; Driver Code
function check ( arr , n ) { let modify = 0 ; if ( arr [ n - 1 ] >= arr [ n - 2 ] ) { arr [ n - 1 ] = arr [ n - 2 ] - 1 ; modify ++ ; } if ( arr [ 0 ] <= arr [ 1 ] ) { arr [ 0 ] = arr [ 1 ] + 1 ; modify ++ ; } for ( let i = n - 2 ; i > 0 ; i -- ) { if ( ( arr [ i - 1 ] <= arr [ i ] && arr [ i + 1 ] <= arr [ i ] ) || ( arr [ i - 1 ] >= arr [ i ] && arr [ i + 1 ] >= arr [ i ] ) ) { arr [ i ] = parseInt ( ( arr [ i - 1 ] + arr [ i + 1 ] ) / 2 , 10 ) ; modify ++ ; if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i + 1 ] ) return false ; } } if ( modify > 1 ) return false ; return true ; } let arr = [ 10 , 5 , 11 , 3 ] ; let n = arr . length ; if ( check ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximal Disjoint Intervals | Function to find maximal disjoint set ; Sort the list of intervals ; First Interval will always be included in set ; End point of first interval ; Check if given interval overlap with previously included interval , if not then include this interval and update the end point of last added interval ; Driver code
function maxDisjointIntervals ( list ) { list . sort ( ( a , b ) => a [ 1 ] - b [ 1 ] ) ; document . write ( " " + list [ 0 ] [ 0 ] + " " + list [ 0 ] [ 1 ] + " " + " " ) ; var r1 = list [ 0 ] [ 1 ] ; for ( var i = 1 ; i < list . length ; i ++ ) { var l1 = list [ i ] [ 0 ] ; var r2 = list [ i ] [ 1 ] ; if ( l1 > r1 ) { document . write ( " " + l1 + " " + r2 + " " + " " ) ; r1 = r2 ; } } } var N = 4 ; var intervals = [ [ 1 , 4 ] , [ 2 , 3 ] , [ 4 , 6 ] , [ 8 , 9 ] ] ; maxDisjointIntervals ( intervals ) ;
Print k different sorted permutations of a given array | Javascript implementation of the approach ; Utility function to print the original indices of the elements of the array ; Function to print the required permutations ; To keep track of original indices ; Sort the array ; Count the number of swaps that can be made ; Cannot generate 3 permutations ; Print the first permutation ; Find an index to swap and create second permutation ; Print the last permutation ; Driver code ; Function call
var next_pos = 1 ; function printIndices ( n , a ) { for ( var i = 0 ; i < n ; i ++ ) document . write ( a [ i ] [ 1 ] + " " ) ; document . write ( " " ) ; } function printPermutations ( n , a , k ) { var arr = Array . from ( Array ( n ) , ( ) => Array ( 2 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { arr [ i ] [ 0 ] = a [ i ] ; arr [ i ] [ 1 ] = i ; } arr . sort ( ) ; var count = 1 ; for ( var i = 1 ; i < n ; i ++ ) if ( arr [ i ] [ 0 ] == arr [ i - 1 ] [ 0 ] ) count ++ ; if ( count < k ) { document . write ( " " ) ; return ; } for ( var i = 0 ; i < k - 1 ; i ++ ) { printIndices ( n , arr ) ; for ( var j = next_pos ; j < n ; j ++ ) { if ( arr [ j ] [ 0 ] == arr [ j - 1 ] [ 0 ] ) { [ arr [ j ] , arr [ j - 1 ] ] = [ arr [ j - 1 ] , arr [ j ] ] ; next_pos = j + 1 ; break ; } } } printIndices ( n , arr ) ; } var a = [ 1 , 3 , 3 , 1 ] ; var n = a . length ; var k = 3 ; printPermutations ( n , a , k ) ;
Count distinct elements in an array | This method returns count of Unique elements ; Creates and empty hashset ; Traverse the input array ; If not present , then put it in hashtable and increment result ; Driver code
function countDistinct ( arr , n ) { let hs = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) { hs . add ( arr [ i ] ) ; } return hs . size ; } let arr = [ 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 ] ; document . write ( countDistinct ( arr , arr . length ) ) ;
In | Calculating next gap ; Function for swapping ; Merging the subarrays using shell sorting Time Complexity : O ( nlog n ) Space Complexity : O ( 1 ) ; merge sort makes log n recursive calls and each time calls merge ( ) which takes nlog n steps Time Complexity : O ( n * log n + 2 ( ( n / 2 ) * log ( n / 2 ) ) + 4 ( ( n / 4 ) * log ( n / 4 ) ) + ... . . + 1 ) Time Complexity : O ( logn * ( n * log n ) ) i . e . O ( n * ( logn ) ^ 2 ) Space Complexity : O ( 1 ) ; Calculating mid to slice the array in two halves ; Recursive calls to sort left and right subarrays ; Driver Code
function nextGap ( gap ) { if ( gap <= 1 ) return 0 ; return Math . floor ( Math . ceil ( gap / 2.0 ) ) ; } function swap ( nums , i , j ) { let temp = nums [ i ] ; nums [ i ] = nums [ j ] ; nums [ j ] = temp ; } function inPlaceMerge ( nums , start , end ) { let gap = end - start + 1 ; for ( gap = nextGap ( gap ) ; gap > 0 ; gap = nextGap ( gap ) ) { for ( let i = start ; i + gap <= end ; i ++ ) { let j = i + gap ; if ( nums [ i ] > nums [ j ] ) swap ( nums , i , j ) ; } } } function mergeSort ( nums , s , e ) { if ( s == e ) return ; let mid = Math . floor ( ( s + e ) / 2 ) ; mergeSort ( nums , s , mid ) ; mergeSort ( nums , mid + 1 , e ) ; inPlaceMerge ( nums , s , e ) ; } let nums = [ 12 , 11 , 13 , 5 , 6 , 7 ] ; mergeSort ( nums , 0 , nums . length - 1 ) ; document . write ( ( nums ) . join ( " " ) ) ;
Rearrange an array to maximize i * arr [ i ] | Function to calculate the maximum points earned by making an optimal selection on the given array ; Sorting the array ; Variable to store the total points earned ; Driver Code
function findOptimalSolution ( a , N ) { a . sort ( function ( a , b ) { return a - b } ) ; let points = 0 ; for ( let i = 0 ; i < N ; i ++ ) { points += a [ i ] * i ; } return points ; } let a = [ 1 , 4 , 2 , 3 , 9 ] ; let N = a . length ; document . write ( findOptimalSolution ( a , N ) ) ;
Minimum number of towers required such that every house is in the range of at least one tower | Function to count the number of tower ; first we sort the house numbers ; for count number of towers ; for iterate all houses ; count number of towers ; find find the middle location ; traverse till middle location ; this is point to middle house where we insert the tower ; now find the last location ; traverse till last house of the range ; return the number of tower ; given elements ; print number of towers
function number_of_tower ( house , range , n ) { house . sort ( function ( a , b ) { return a - b ; } ) ; let numOfTower = 0 ; let i = 0 ; while ( i < n ) { numOfTower ++ ; let loc = house [ i ] + range ; while ( i < n && house [ i ] <= loc ) i ++ ; -- i ; loc = house [ i ] + range ; while ( i < n && house [ i ] <= loc ) i ++ ; } return numOfTower ; } let house = [ 7 , 2 , 4 , 6 , 5 , 9 , 12 , 11 ] ; let range = 2 ; let n = house . length ; document . write ( number_of_tower ( house , range , n ) ) ;
Check if the characters of a given string are in alphabetical order | Function that checks whether the string is in alphabetical order or not ; length of the string ; create a character array of the length of the string ; assign the string to character array ; sort the character array ; check if the character array is equal to the string or not ; Driver code ; check whether the string is in alphabetical order or not
function isAlphabaticOrder ( s ) { var n = s . length ; var c = new Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { c [ i ] = s [ i ] ; } c . sort ( ) ; for ( var i = 0 ; i < n ; i ++ ) if ( c [ i ] != s [ i ] ) return false ; return true ; } s = " " ; if ( isAlphabaticOrder ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sort first k values in ascending order and remaining n | function to sort the array ; Sort first k elements in ascending order ; Sort remaining n - k elements in descending order ; Our arr contains 8 elements
function printOrder ( arr , k ) { let n = arr . length ; arr = arr . slice ( 0 , k ) . sort ( function ( a , b ) { return a - b ; } ) . concat ( arr . slice ( k , n ) . sort ( function ( a , b ) { return b - a ; } ) ) ; return arr ; } let arr = [ 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , - 1 ] ; let k = 4 ; arr = printOrder ( arr , k ) ; document . write ( arr . join ( " " ) ) ;
Merging and Sorting Two Unsorted Stacks | This is the temporary stack ; Sorts input stack and returns sorted stack . ; pop out the first element ; while temporary stack is not empty and top of stack is greater than temp ; pop from temporary stack and push it to the input stack ; push temp in temporary of stack ; Push contents of both stacks in result ; Sort the result stack . ; main function
let res = [ ] ; let tmpStack = [ ] ; function sortStack ( input ) { while ( input . length != 0 ) { let tmp = input [ input . length - 1 ] ; input . pop ( ) ; while ( tmpStack . length != 0 && tmpStack [ tmpStack . length - 1 ] > tmp ) { input . push ( tmpStack [ tmpStack . length - 1 ] ) ; tmpStack . pop ( ) ; } tmpStack . push ( tmp ) ; } } function sortedMerge ( s1 , s2 ) { while ( s1 . length != 0 ) { res . push ( s1 [ s1 . length - 1 ] ) ; s1 . pop ( ) ; } while ( s2 . length != 0 ) { res . push ( s2 [ s2 . length - 1 ] ) ; s2 . pop ( ) ; } sortStack ( res ) ; } let s1 = [ ] ; let s2 = [ ] ; s1 . push ( 34 ) ; s1 . push ( 3 ) ; s1 . push ( 31 ) ; s2 . push ( 1 ) ; s2 . push ( 12 ) ; s2 . push ( 23 ) ; sortedMerge ( s1 , s2 ) ; document . write ( " " + " " ) ; while ( tmpStack . length != 0 ) { document . write ( tmpStack [ tmpStack . length - 1 ] + " " ) ; tmpStack . pop ( ) ; }
Lexicographical concatenation of all substrings of a string | Javascript Program to create concatenation of all substrings in lexicographic order . ; Creating an array to store substrings ; finding all substrings of string ; Sort all substrings in lexicographic order ; Concatenating all substrings ; Driver code
function lexicographicSubConcat ( s ) { var n = s . length ; var sub_count = n * parseInt ( ( n + 1 ) / 2 ) ; var arr = Array ( sub_count ) ; var index = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var len = 1 ; len <= n - i ; len ++ ) arr [ index ++ ] = s . substring ( i , i + len ) ; arr . sort ( ) ; var res = " " ; for ( var i = 0 ; i < sub_count ; i ++ ) res += arr [ i ] ; return res ; } var s = " " ; document . write ( lexicographicSubConcat ( s ) ) ;
Insertion Sort by Swapping Elements | Utility function to print a Vector ; Function performs insertion sort on vector V ; Insert V [ i ] into list 0. . i - 1 ; Swap V [ j ] and V [ j - 1 ] ; Decrement j by 1 ; Driver Code
function printVector ( V ) { for ( let e of V ) { document . write ( e + " " ) ; } document . write ( " " ) ; } function insertionSort ( V ) { let N = V . length ; let i , j , key ; for ( i = 1 ; i < N ; i ++ ) { j = i ; while ( j > 0 && V [ j ] < V [ j - 1 ] ) { let temp = V [ j ] ; V [ j ] = V [ j - 1 ] ; V [ j - 1 ] = temp ; j -= 1 ; } } } let A = [ 9 , 8 , 7 , 5 , 2 , 1 , 2 , 3 ] ; document . write ( " " + " " ) ; printVector ( A ) ; document . write ( " " + " " ) ; insertionSort ( A ) ; printVector ( A ) ;
Program to sort string in descending order | JavaScript program to sort a string in descending order using library function ; Driver code ; function call
function descOrder ( s ) { s . sort ( ) . reverse ( ) ; str1 = s . join ( " " ) ; document . write ( str1 ) ; } var s = " " ; s = s . split ( " " ) ; descOrder ( s ) ;
Sort string of characters | JavaScript program to sort a string of characters ; function to print string in sorted order ; Hash array to keep count of characters . Initially count of all charters is initialized to zero . . ; Traverse string and increment count of characters ; ' a ' - ' a ' will be 0 , ' b ' - ' a ' will be 1 , so for location of character in count array we will do str [ i ] - ' a ' . ; Traverse the hash array and print characters ; Driver program to test above function
let MAX_CHAR = 26 ; function sortString ( str ) { let letters = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { letters [ i ] = 0 ; } for ( let x = 0 ; x < str . length ; x ++ ) { letters [ str [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { for ( let j = 0 ; j < letters [ i ] ; j ++ ) { document . write ( String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ) ; } } } sortString ( " " ) ;
Smallest element in an array that is repeated exactly ' k ' times . | javascript program to find smallest number in array that is repeated exactly ' k ' times . ; finds the smallest number in arr that is repeated k times ; Computing frequencies of all elements ; Finding the smallest element with frequency as k ; If frequency of any of the number is equal to k starting from 0 then return the number ; Driver code
var MAX = 1000 ; function findDuplicate ( arr , n , k ) { var freq = Array . from ( { length : MAX } , ( _ , i ) => 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 1 && arr [ i ] > MAX ) { document . write ( " " ) ; return - 1 ; } freq [ arr [ i ] ] += 1 ; } for ( var i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] == k ) return i ; } return - 1 ; } var arr = [ 2 , 2 , 1 , 3 , 1 ] ; var k = 2 ; var n = arr . length ; document . write ( findDuplicate ( arr , n , k ) ) ;
Find the Sub | Javascript program to find subarray with sum closest to 0 ; Returns subarray with sum closest to 0. ; To consider the case of subarray starting from beginning of the array ; Store prefix sum with index ; Sort on the basis of sum ; Find two consecutive elements with minimum difference ; Update minimum difference and starting and ending indexes ; Return starting and ending indexes ; Driver code
class Prefix { constructor ( ) { this . sum = 0 ; this . index = 0 ; } } class Pair { constructor ( a , b ) { this . first = a ; this . second = b ; } } function findSubArray ( arr , n ) { let start = - 1 , end = - 1 , min_diff = Number . MAX_VALUE ; let pre_sum = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) pre_sum [ i ] = new Prefix ( ) ; pre_sum [ 0 ] . sum = 0 ; pre_sum [ 0 ] . index = - 1 ; for ( let i = 1 ; i <= n ; i ++ ) { pre_sum [ i ] . sum = pre_sum [ i - 1 ] . sum + arr [ i - 1 ] ; pre_sum [ i ] . index = i - 1 ; } pre_sum . sort ( function ( a , b ) { return a . sum - b . sum } ) ; for ( let i = 1 ; i <= n ; i ++ ) { let diff = pre_sum [ i ] . sum - pre_sum [ i - 1 ] . sum ; if ( min_diff > diff ) { min_diff = diff ; start = pre_sum [ i - 1 ] . index ; end = pre_sum [ i ] . index ; } } let p = new Pair ( start + 1 , end ) ; return p ; } let arr = [ 2 , 3 , - 4 , - 1 , 6 ] ; let n = arr . length ; let point = findSubArray ( arr , n ) ; document . write ( " " ) ; document . write ( point . first + " " + point . second ) ;
Check whether a given array is a k sorted array or not | function to find index of element ' x ' in sorted ' arr ' uses binary search technique ; function to check whether the given array is a ' k ' sorted array or not ; auxiliary array ' aux ' ; copy elements of ' arr ' to ' aux ' ; sort ' aux ' ; for every element of ' arr ' at index ' i ' , find its index ' j ' in ' aux ' ; index of arr [ i ] in sorted array ' aux ' ; if abs ( i - j ) > k , then that element is not at - most k distance away from its target position . Thus , ' arr ' is not a k sorted array ; ' arr ' is a k sorted array ; Driver program to test above
function binarySearch ( arr , low , high , x ) { while ( low <= high ) { var mid = parseInt ( ( low + high ) / 2 ) ; if ( arr [ mid ] == x ) return mid ; else if ( arr [ mid ] > x ) high = mid - 1 ; else low = mid + 1 ; } } function isKSortedArray ( arr , n , k ) { var aux = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) aux [ i ] = arr [ i ] ; aux . sort ( ( a , b ) => a - b ) for ( var i = 0 ; i < n ; i ++ ) { var j = binarySearch ( aux , 0 , n - 1 , arr [ i ] ) ; if ( Math . abs ( i - j ) > k ) return " " ; } return " " ; } var arr = [ 3 , 2 , 1 , 5 , 6 , 4 ] ; var n = arr . length ; var k = 2 ; document . write ( " " + isKSortedArray ( arr , n , k ) ) ;
TimSort | Javascript program to perform TimSort . ; Becomes 1 if any 1 bits are shifted off ; This function sorts array from left index to to right index which is of size atmost RUN ; Merge function merges the sorted runs ; Original array is broken in two parts left and right array ; After comparing , we merge those two array in larger sub array ; Copy remaining elements of left , if any ; Copy remaining element of right , if any ; Iterative Timsort function to sort the array [ 0. . . n - 1 ] ( similar to merge sort ) ; Sort individual subarrays of size RUN ; Start merging from size RUN ( or 32 ) . It will merge to form size 64 , then 128 , 256 and so on ... . ; Pick starting point of left sub array . We are going to merge arr [ left . . left + size - 1 ] and arr [ left + size , left + 2 * size - 1 ] After every merge , we increase left by 2 * size ; Find ending point of left sub array mid + 1 is starting point of right sub array ; Merge sub array arr [ left ... . . mid ] & arr [ mid + 1. ... right ] ; Utility function to print the Array ; Driver code ; Function Call
let MIN_MERGE = 32 ; function minRunLength ( n ) { let r = 0 ; while ( n >= MIN_MERGE ) { r |= ( n & 1 ) ; n >>= 1 ; } return n + r ; } function insertionSort ( arr , left , right ) { for ( let i = left + 1 ; i <= right ; i ++ ) { let temp = arr [ i ] ; let j = i - 1 ; while ( j >= left && arr [ j ] > temp ) { arr [ j + 1 ] = arr [ j ] ; j -- ; } arr [ j + 1 ] = temp ; } } function merge ( arr , l , m , r ) { let len1 = m - l + 1 , len2 = r - m ; let left = new Array ( len1 ) ; let right = new Array ( len2 ) ; for ( let x = 0 ; x < len1 ; x ++ ) { left [ x ] = arr [ l + x ] ; } for ( let x = 0 ; x < len2 ; x ++ ) { right [ x ] = arr [ m + 1 + x ] ; } let i = 0 ; let j = 0 ; let k = l ; while ( i < len1 && j < len2 ) { if ( left [ i ] <= right [ j ] ) { arr [ k ] = left [ i ] ; i ++ ; } else { arr [ k ] = right [ j ] ; j ++ ; } k ++ ; } while ( i < len1 ) { arr [ k ] = left [ i ] ; k ++ ; i ++ ; } while ( j < len2 ) { arr [ k ] = right [ j ] ; k ++ ; j ++ ; } } function timSort ( arr , n ) { let minRun = minRunLength ( MIN_MERGE ) ; for ( let i = 0 ; i < n ; i += minRun ) { insertionSort ( arr , i , Math . min ( ( i + MIN_MERGE - 1 ) , ( n - 1 ) ) ) ; } for ( let size = minRun ; size < n ; size = 2 * size ) { for ( let left = 0 ; left < n ; left += 2 * size ) { let mid = left + size - 1 ; let right = Math . min ( ( left + 2 * size - 1 ) , ( n - 1 ) ) ; if ( mid < right ) merge ( arr , left , mid , right ) ; } } } function printArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } let arr = [ - 2 , 7 , 15 , - 14 , 0 , 15 , 0 , 7 , - 7 , - 4 , - 13 , 5 , 8 , - 14 , 12 ] ; let n = arr . length ; document . write ( " " ) ; printArray ( arr , n ) ; timSort ( arr , n ) ; document . write ( " " ) ; printArray ( arr , n ) ;