codeparrot/xlcost-text-to-code huggingface dataset:
Split the array and add the first part to the end | Function to spilt array and move first part to end ; make a temporary array with double the size ; copy array element in to new array twice ; Driver code
function SplitAndAdd ( A , Length , rotation ) { let tmp = new Array ( Length * 2 ) ; for ( let i = 0 ; i < tmp . length ; i ++ ) { tmp [ i ] = 0 ; } for ( let i = 0 ; i < Length ; i ++ ) { tmp [ i ] = A [ i ] tmp [ i + Length ] = A [ i ] } for ( let i = rotation ; i < rotation + Length ; i ++ ) { A [ i - rotation ] = tmp [ i ] ; } } let arr = [ 12 , 10 , 5 , 6 , 52 , 36 ] ; let n = arr . length ; let position = 2 ; SplitAndAdd ( arr , n , position ) ; for ( let i = 0 ; i < n ; ++ i ) document . write ( arr [ i ] + " " ) ;
Shell | Function to swap two elements ; Function to sort arr [ ] using Shell Metzner sort ; Declare variables ; Set initial step size to the size of the array ; Step size decreases by half each time ; k is the upper limit for j ; j is the starting point ; i equals to smaller value ; l equals to larger value ; Compare and swap arr [ i ] with arr [ l ] ; Decrease smaller value by step size ; Increment the lower limit of i ; Function to print the contents of an array ; Driver code ; Sort the array using Shell Metzner Sort ; Print the sorted array
function swap ( arr , i , j ) { let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } function sort_shell_metzner ( arr , n ) { let i , j , k , l , m , temp ; m = n ; while ( m > 0 ) { m = Math . floor ( m / 2 ) ; k = n - m ; j = 0 ; do { i = j ; do { l = i + m ; if ( l < n && arr [ i ] > arr [ l ] ) { swap ( arr , i , l ) ; i -= m ; } else { break ; } } while ( i >= 0 ) ; j ++ ; } while ( j <= k ) ; } } function printArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let arr = [ 0 , - 2 , 8 , 5 , 1 ] ; let n = arr . length ; sort_shell_metzner ( arr , n ) ; printArray ( arr , n ) ;
Rearrange an array such that arr [ i ] = i | Function to tranform the array ; Iterate over the array ; Check is any ar [ j ] exists such that ar [ j ] is equal to i ; Iterate over array ; If not present ; Print the output ; Driver Code ; Function Call
function fixArray ( ar , n ) { var i , j , temp ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( ar [ j ] == i ) { temp = ar [ j ] ; ar [ j ] = ar [ i ] ; ar [ i ] = temp ; break ; } } } for ( i = 0 ; i < n ; i ++ ) { if ( ar [ i ] != i ) { ar [ i ] = - 1 ; } } document . write ( " " ) ; document . write ( " " ) ; for ( i = 0 ; i < n ; i ++ ) { document . write ( ar [ i ] + " " ) ; } } var n , ar = [ - 1 , - 1 , 6 , 1 , 9 , 3 , 2 , - 1 , 4 , - 1 ] ; n = ar . length ; fixArray ( ar , n ) ;
Perform K of Q queries to maximize the sum of the array elements | Function to perform K queries out of Q to maximize the final sum ; Get the initial sum of the array ; Stores the contriution of every query ; Sort the contribution of queries in descending order ; Get the K most contributions ; Driver code
const getFinalSum = ( a , n , queries , q , k ) => { let answer = 0 ; for ( let i = 0 ; i < n ; i ++ ) answer += a [ i ] ; let contribution = [ ] ; for ( let i = 0 ; i < q ; i ++ ) { contribution . push ( queries [ i ] [ 1 ] - queries [ i ] [ 0 ] + 1 ) ; } contribution . sort ( ( a , b ) => b - a ) ; let i = 0 ; while ( i < k ) { answer += contribution [ i ] ; i ++ ; } return answer ; } const a = [ 1 , 1 , 2 , 2 , 2 , 3 ] ; const n = a . length ; const queries = [ [ 0 , 4 ] , [ 1 , 2 ] , [ 2 , 5 ] , [ 2 , 3 ] , [ 2 , 4 ] ] ; const q = queries . length ; let k = 3 ; document . write ( getFinalSum ( a , n , queries , q , k ) ) ;
Queries to return the absolute difference between L | Function to return the result for a particular query ; Get the difference between the indices of L - th and the R - th smallest element ; Return the answer ; Function that performs all the queries ; Store the array numbers and their indices ; Sort the array elements ; Answer all the queries ; Driver code
function answerQuery ( arr , l , r ) { let answer = Math . abs ( arr [ l - 1 ] [ 1 ] - arr [ r - 1 ] [ 1 ] ) ; return answer ; } function solveQueries ( a , n , q , m ) { let arr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = new Array ( n ) . fill ( 0 ) } for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] [ 0 ] = a [ i ] ; arr [ i ] [ 1 ] = i ; } arr . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ; for ( let i = 0 ; i < m ; i ++ ) document . write ( answerQuery ( arr , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) + " " ) ; } let a = [ 1 , 5 , 4 , 2 , 8 , 6 , 7 ] ; let n = a . length ; let q = [ [ 2 , 5 ] , [ 1 , 3 ] , [ 1 , 5 ] , [ 3 , 6 ] ] ; let m = q . length ; solveQueries ( a , n , q , m ) ;
Find the k largest numbers after deleting the given elements | Find k maximum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the largestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it largestElement ; Print top k elements in the heap ; Pop the top element ; Driver code
function findElementsAfterDel ( arr , m , del , n , k ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( mp . has ( del [ i ] ) ) { mp . set ( del [ i ] , mp . get ( del [ i ] ) + 1 ) ; } else { mp . set ( del [ i ] , 1 ) ; } } let heap = [ ] ; for ( let i = 0 ; i < m ; ++ i ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) - 1 ) ; if ( mp . get ( arr [ i ] ) == 0 ) { mp . delete ( arr [ i ] ) ; } } else { heap . push ( arr [ i ] ) ; } } heap . sort ( function ( a , b ) { return b - a ; } ) ; for ( let i = 0 ; i < k ; ++ i ) { document . write ( heap [ i ] + " " ) ; } } let array = [ 5 , 12 , 33 , 4 , 56 , 12 , 20 ] ; let m = array . length ; let del = [ 12 , 56 , 5 ] ; let n = del . length ; let k = 3 ; findElementsAfterDel ( array , m , del , n , k ) ;
K | JavaScript implementation of the above approach ; Set to store the unique substring ; String to create each substring ; Adding to set ; Converting set into a list ; Sorting the strings int the list into lexicographical order ; Printing kth substring ; Driver code
function kThLexString ( st , k , n ) { var z = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) { var pp = " " ; for ( var j = i ; j < i + k ; j ++ ) { if ( j >= n ) break ; pp += st [ j ] ; z . add ( pp ) ; } } var fin = [ ] ; z . forEach ( element => { fin . push ( element ) ; } ) ; fin . sort ( ) ; document . write ( fin [ k - 1 ] ) ; } var s = " " ; var k = 5 ; var n = s . length ; kThLexString ( s , k , n ) ;
Rearrange array such that arr [ i ] >= arr [ j ] if i is even and arr [ i ] <= arr [ j ] if i is odd and j < i | function to rearrange the array ; total even positions ; total odd positions ; copy original array in an auxiliary array ; sort the auxiliary array ; fill up odd position in original array ; fill up even positions in original array ; display array ; Driver code
function rearrangeArr ( arr , n ) { let evenPos = Math . floor ( n / 2 ) ; let oddPos = n - evenPos ; let tempArr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) tempArr [ i ] = arr [ i ] ; tempArr . sort ( ) ; let j = oddPos - 1 ; for ( let i = 0 ; i < n ; i += 2 ) { arr [ i ] = tempArr [ j ] ; j -- ; } j = oddPos ; for ( let i = 1 ; i < n ; i += 2 ) { arr [ i ] = tempArr [ j ] ; j ++ ; } for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; let size = arr . length ; rearrangeArr ( arr , size ) ;
Count triplets in a sorted doubly linked list whose product is equal to a given value x | javascript implementation to count triplets in a sorted doubly linked list whose sum is equal to a given value ' x ' Represents node of a doubly linked list ; function to count triplets in a sorted doubly linked list whose sum is equal to a given value ' x ' ; generate all possible triplets ; if elements in the current triplet sum up to ' x ' ; increment count ; required count of triplets ; A utility function to insert a new node at the beginning of doubly linked list ; allocate node ; start with an empty doubly linked list ; insert values in sorted order
class Node { constructor ( val ) { this . data = val ; this . prev = null ; this . next = null ; } } function countTriplets ( head , x ) { var ptr1 , ptr2 , ptr3 ; var count = 0 ; for ( ptr1 = head ; ptr1 != null ; ptr1 = ptr1 . next ) for ( ptr2 = ptr1 . next ; ptr2 != null ; ptr2 = ptr2 . next ) for ( ptr3 = ptr2 . next ; ptr3 != null ; ptr3 = ptr3 . next ) if ( ( ptr1 . data * ptr2 . data * ptr3 . data ) == x ) count ++ ; return count ; } function insert ( head , val ) { temp = new Node ( val ) ; if ( head == null ) head = temp ; else { temp . next = head ; head . prev = temp ; head = temp ; } return head ; } head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; var x = 8 ; document . write ( " " + countTriplets ( head , x ) ) ;
Sort only non | Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to print the array such that only non primes are sorted ; vector v will store all non prime numbers ; If not prime , insert into vector ; sorting vector of non primes ; print the required array ; Driver Code
prime = new Array ( 100005 ) ; function SieveOfEratosthenes ( n ) { prime . fill ( true ) ; prime [ 1 ] = false ; for ( var p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } } function sortedArray ( arr , n ) { SieveOfEratosthenes ( 100005 ) ; var v = [ ] ; for ( var i = 0 ; i < n ; ++ i ) { if ( prime [ arr [ i ] ] == 0 ) v . push ( arr [ i ] ) ; } v . sort ( ) ; var j = 0 ; for ( var i = 0 ; i < n ; ++ i ) { if ( prime [ arr [ i ] ] == true ) document . write ( arr [ i ] + " " ) ; else { document . write ( v [ j ] + " " ) ; j ++ ; } } } var n = 6 ; var arr = [ 100 , 11 , 500 , 2 , 17 , 1 ] ; sortedArray ( arr , n ) ;
Maximum sum of absolute difference of any permutation | Javascript implementation of above algorithm ; final sequence stored in the vector ; sort the original array so that we can retrieve the large elements from the end of array elements ; In this loop first we will insert one smallest element not entered till that time in final sequence and then enter a highest element ( not entered till that time ) in final sequence so that we have large difference value . This process is repeated till all array has completely entered in sequence . Here , we have loop till n / 2 because we are inserting two elements at a time in loop . ; If there are odd elements , push the middle element at the end . ; variable to store the maximum sum of absolute difference ; In this loop absolute difference of elements for the final sequence is calculated . ; absolute difference of last element and 1 st element ; return the value ; Driver Code
function MaxSumDifference ( a , n ) { let finalSequence = [ ] ; a . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < n / 2 ; ++ i ) { finalSequence . push ( a [ i ] ) ; finalSequence . push ( a [ n - i - 1 ] ) ; } if ( n % 2 != 0 ) finalSequence . push ( a [ Math . floor ( n / 2 ) ] ) ; let MaximumSum = 0 ; for ( let i = 0 ; i < n - 1 ; ++ i ) { MaximumSum = MaximumSum + Math . abs ( finalSequence [ i ] - finalSequence [ i + 1 ] ) ; } MaximumSum = MaximumSum + Math . abs ( finalSequence [ n - 1 ] - finalSequence [ 0 ] ) ; return MaximumSum ; } let a = [ 1 , 2 , 4 , 8 ] ; let n = a . length ; document . write ( MaxSumDifference ( a , n ) ) ;
Rearrange positive and negative numbers using inbuilt sort function | Javascript program to implement the above approach ; Rearrange the array with all negative integers on left and positive integers on right use recursion to split the array with first element as one half and the rest array as another and then merge it with head of the array in each step ; exit condition ; rearrange the array except the first element in each recursive call ; If the first element of the array is positive , then right - rotate the array by one place first and then reverse the merged array . ; Driver code
function printArray ( array , Length ) { document . write ( " " ) ; for ( let i = 0 ; i < Length ; i ++ ) { document . write ( array [ i ] ) ; if ( i < ( Length - 1 ) ) document . write ( " " ) ; else document . write ( " " ) ; } } function reverse ( array , start , end ) { while ( start < end ) { let temp = array [ start ] ; array [ start ] = array [ end ] ; array [ end ] = temp ; start ++ ; end -- ; } } function rearrange ( array , start , end ) { if ( start == end ) return ; rearrange ( array , ( start + 1 ) , end ) ; if ( array [ start ] >= 0 ) { reverse ( array , ( start + 1 ) , end ) ; reverse ( array , start , end ) ; } } let array = [ - 12 , - 11 , - 13 , - 5 , - 6 , 7 , 5 , 3 , 6 ] ; let length = array . length ; let countNegative = 0 ; for ( let i = 0 ; i < length ; i ++ ) { if ( array [ i ] < 0 ) countNegative ++ ; } document . write ( " " ) ; printArray ( array , length ) ; rearrange ( array , 0 , ( length - 1 ) ) ; reverse ( array , countNegative , ( length - 1 ) ) ; document . write ( " " ) ; printArray ( array , length ) ;
Efficiently merging two sorted arrays with O ( 1 ) extra space |  ; Now traverse the array1 and if arr2 first element is less than arr1 then swap ; Swap ; We will store the firstElement of array2 and left shift all the element and store the firstElement in arr2 [ k - 1 ] ; Read the arr1 ; Read the arr2 ; Driver Code
function mergeArray ( arr1 , arr2 , n , m ) { for ( let i = 0 ; i < n ; i ++ ) { if ( arr1 [ i ] > arr2 [ 0 ] ) { let temp = arr1 [ i ] ; arr1 [ i ] = arr2 [ 0 ] ; arr2 [ 0 ] = temp ; let firstElement = arr2 [ 0 ] ; let k ; for ( k = 1 ; k < m && arr2 [ k ] < firstElement ; k ++ ) { arr2 [ k - 1 ] = arr2 [ k ] ; } arr2 [ k - 1 ] = firstElement ; } } for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr1 [ i ] + " " ) ; } document . write ( " " ) ; for ( let i = 0 ; i < m ; i ++ ) { document . write ( arr2 [ i ] + " " ) ; } } let arr1 = [ 1 , 3 , 5 , 7 ] ; let arr2 = [ 0 , 2 , 6 , 8 , 9 ] ; let n = 4 , m = 5 ; mergeArray ( arr1 , arr2 , n , m ) ;
Maximum product of subsequence of size k | Function to find maximum possible product ; sorting given input array ; variable to store final product of all element of sub - sequence of size k ; CASE I If max element is 0 and k is odd then max product will be 0 ; CASE II If all elements are negative and k is odd then max product will be product of rightmost - subarray of size k ; else i is current left pointer index ; j is current right pointer index ; CASE III if k is odd and rightmost element in sorted array is positive then it must come in subsequence Multiplying A [ j ] with product and correspondingly changing j ; CASE IV Now k is even Now we deal with pairs Each time a pair is multiplied to product i . e . . two elements are added to subsequence each time Effectively k becomes half Hence , k >>= 1 means k /= 2 ; Now finding k corresponding pairs to get maximum possible value of product ; product from left pointers ; product from right pointers ; Taking the max product from two choices Correspondingly changing the pointer 's position ; Finally return product ; Driver Code
function maxProductSubarrayOfSizeK ( A , n , k ) { A . sort ( function ( a , b ) { return a - b } ) ; let product = 1 ; let i ; if ( A [ n - 1 ] == 0 && k % 2 != 0 ) return 0 ; if ( A [ n - 1 ] <= 0 && k % 2 != 0 ) { for ( i = n - 1 ; i >= n - k ; i -- ) product *= A [ i ] ; return product ; } i = 0 ; let j = n - 1 ; if ( k % 2 != 0 ) { product *= A [ j ] ; j -- ; k -- ; } k >>= 1 ; for ( let itr = 0 ; itr < k ; itr ++ ) { let left_product = A [ i ] * A [ i + 1 ] ; let right_product = A [ j ] * A [ j - 1 ] ; if ( left_product > right_product ) { product *= left_product ; i += 2 ; } else { product *= right_product ; j -= 2 ; } } return product ; } let A = [ 1 , 2 , - 1 , - 3 , - 6 , 4 ] ; let n = A . length ; let k = 4 ; document . write ( maxProductSubarrayOfSizeK ( A , n , k ) ) ;
Reorder an array according to given indexes | Function to reorder elements of arr [ ] according to index [ ] ; arr [ i ] should be present at index [ i ] index ; Copy temp [ ] to arr [ ] ; Driver program
function reorder ( arr , index , n ) { var temp = [ ... Array ( n ) ] ; for ( var i = 0 ; i < n ; i ++ ) temp [ index [ i ] ] = arr [ i ] ; for ( var i = 0 ; i < n ; i ++ ) { arr [ i ] = temp [ i ] ; index [ i ] = i ; } } var arr = [ 50 , 40 , 70 , 60 , 90 ] ; var index = [ 3 , 0 , 4 , 1 , 2 ] ; var n = arr . length ; reorder ( arr , index , n ) ; document . write ( " " ) ; document . write ( " " ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; document . write ( " " ) ; document . write ( " " ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( index [ i ] + " " ) ;
Program to sort an array of strings using Selection Sort | Sorts an array of strings ; One by one move boundary of unsorted subarray ; Find the minimum element in unsorted array ; If arr [ j ] is smaller than minStr ; Make arr [ j ] as minStr and update min_idx ; Swapping the minimum element found with the first element . ; Driver Code ; Printing the array before sorting ; Printing the array after sorting
function selectionSort ( arr , n ) { for ( let i = 0 ; i < n - 1 ; i ++ ) { let min_index = i ; let minStr = arr [ i ] ; for ( let j = i + 1 ; j < n ; j ++ ) { if ( ( arr [ j ] ) . localeCompare ( minStr ) === - 1 ) { minStr = arr [ j ] ; min_index = j ; } } if ( min_index != i ) { let temp = arr [ min_index ] ; arr [ min_index ] = arr [ i ] ; arr [ i ] = temp ; } } } let arr = [ " " , " " , " " ] ; let n = arr . length ; document . write ( " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( i + " " + arr [ i ] + " " ) ; } document . write ( " " ) ; selectionSort ( arr , n ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( i + " " + arr [ i ] + " " ) ; }
Reorder an array according to given indexes | Function to reorder elements of arr [ ] according to index [ ] ; Fix all elements one by one ; While index [ i ] and arr [ i ] are not fixed ; Store values of the target ( or correct ) position before placing arr [ i ] there ; Place arr [ i ] at its target ( or correct ) position . Also copy corrected index for new position ; Copy old target values to arr [ i ] and index [ i ] ; Driver program
function reorder ( arr , index , n ) { for ( let i = 0 ; i < n ; i ++ ) { while ( index [ i ] != i ) { let oldTargetI = index [ index [ i ] ] ; let oldTargetE = arr [ index [ i ] ] ; arr [ index [ i ] ] = arr [ i ] ; index [ index [ i ] ] = index [ i ] ; index [ i ] = oldTargetI ; arr [ i ] = oldTargetE ; } } } let arr = [ 50 , 40 , 70 , 60 , 90 ] ; let index = [ 3 , 0 , 4 , 1 , 2 ] ; let n = arr . length ; reorder ( arr , index , n ) ; document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( index [ i ] + " " ) ;
Stooge Sort | Function to implement stooge sort ; If first element is smaller than last , swap them ; If there are more than 2 elements in the array ; Recursively sort first 2 / 3 elements ; Recursively sort last 2 / 3 elements ; Recursively sort first 2 / 3 elements again to confirm ; Driver Code ; Calling Stooge Sort function to sort the array ; Display the sorted array
function stoogesort ( arr , l , h ) { if ( l >= h ) return ; if ( arr [ l ] > arr [ h ] ) { let t = arr [ l ] ; arr [ l ] = arr [ h ] ; arr [ h ] = t ; } if ( h - l + 1 > 2 ) { let t = parseInt ( ( h - l + 1 ) / 3 , 10 ) ; stoogesort ( arr , l , h - t ) ; stoogesort ( arr , l + t , h ) ; stoogesort ( arr , l , h - t ) ; } } let arr = [ 2 , 4 , 5 , 3 , 1 ] ; let n = arr . length ; stoogesort ( arr , 0 , n - 1 ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Count minimum number of subsets ( or subsequences ) with consecutive numbers | Returns count of subsets with consecutive numbers ; Sort the array so that elements which are consecutive in nature became consecutive in the array . ; Initialize result ; Check if there is beginning of another subset of consecutive number ; Driven Program
function numofsubset ( arr , n ) { arr . sort ( ( a , b ) => a - b ) ; var count = 1 ; for ( i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ; } return count ; } var arr = [ 100 , 56 , 5 , 6 , 102 , 58 , 101 , 57 , 7 , 103 , 59 ] ; var n = arr . length ; document . write ( numofsubset ( arr , n ) ) ;
Gnome Sort | A function to sort the algorithm using gnome sort ; Driver Code
function gnomeSort ( arr , n ) { let index = 0 ; while ( index < n ) { if ( index == 0 ) index ++ ; if ( arr [ index ] >= arr [ index - 1 ] ) index ++ ; else { let temp = 0 ; temp = arr [ index ] ; arr [ index ] = arr [ index - 1 ] ; arr [ index - 1 ] = temp ; index -- ; } } return ; } let arr = [ 34 , 2 , 10 , - 9 ] ; gnomeSort ( arr , arr . length ) ; document . write ( " " ) ; document . write ( arr . toString ( ) ) ;
Cocktail Sort | Sorts arrar a [ 0. . n - 1 ] using Cocktail sort ; reset the swapped flag on entering the loop , because it might be true from a previous iteration . ; loop from bottom to top same as the bubble sort ; if nothing moved , then array is sorted . ; otherwise , reset the swapped flag so that it can be used in the next stage ; move the end point back by one , because item at the end is in its rightful spot ; from top to bottom , doing the same comparison as in the previous stage ; increase the starting point , because the last stage would have moved the next smallest number to its rightful spot . ; Prints the array ; Driver code
function cocktailSort ( a ) { let swapped = true ; let start = 0 ; let end = a . length ; while ( swapped == true ) { swapped = false ; for ( let i = start ; i < end - 1 ; ++ i ) { if ( a [ i ] > a [ i + 1 ] ) { let temp = a [ i ] ; a [ i ] = a [ i + 1 ] ; a [ i + 1 ] = temp ; swapped = true ; } } if ( swapped == false ) break ; swapped = false ; end = end - 1 ; for ( let i = end - 1 ; i >= start ; i -- ) { if ( a [ i ] > a [ i + 1 ] ) { let temp = a [ i ] ; a [ i ] = a [ i + 1 ] ; a [ i + 1 ] = temp ; swapped = true ; } } start = start + 1 ; } } function printArray ( a ) { let n = a . length ; for ( let i = 0 ; i < n ; i ++ ) document . write ( a [ i ] + " " ) ; document . write ( " " ) ; } let a = [ 5 , 1 , 4 , 2 , 8 , 0 , 2 ] ; cocktailSort ( a ) ; document . write ( " " + " " ) ; printArray ( a ) ;
Find the point where maximum intervals overlap | Javascript Program to find maximum guest at any time in a party ; Sort arrival and exit arrays ; guests_in indicates number of guests at a time ; Similar to merge in merge sort to process all events in sorted order ; If next event in sorted order is arrival , increment count of guests ; Update max_guests if needed ; increment index of arrival array ; If event is exit , decrement count ; of guests . ; Driver Code
function findMaxGuests ( arrl , exit , n ) { arrl . sort ( function ( a , b ) { return a - b } ) ; exit . sort ( function ( a , b ) { return a - b } ) ; let guests_in = 1 , max_guests = 1 , time = arrl [ 0 ] ; let i = 1 , j = 0 ; while ( i < n && j < n ) { if ( arrl [ i ] <= exit [ j ] ) { guests_in ++ ; if ( guests_in > max_guests ) { max_guests = guests_in ; time = arrl [ i ] ; } i ++ ; } else { guests_in -- ; j ++ ; } } document . write ( " " + max_guests + " " + time ) ; } let arrl = [ 1 , 2 , 10 , 5 , 5 ] ; let exit = [ 4 , 5 , 12 , 9 , 12 ] ; let n = arrl . length ; findMaxGuests ( arrl , exit , n ) ;