Skip to content

Instantly share code, notes, and snippets.

View PradipShrestha's full-sized avatar

CIsSharp PradipShrestha

View GitHub Profile
@PradipShrestha
PradipShrestha / wordwrap.js
Created September 15, 2021 02:12
Word wrapping of given input string such that every line cannot exceed max length and breaking word is not allowed.
const wrap = (text, length) => {
const words = text.split(' ')
let line = ''
let lastWord = ''
const result = []
for (let i = 0; i < words.length; i++) {
if (length > (words[i].length + line.length)) {
line += words[i] + ' '
continue
}
@PradipShrestha
PradipShrestha / quicksort.js
Created August 17, 2021 21:00
Quicksort with middle pivot implementation in javascript
const partition = (l, r) => {
let p = Math.floor(l + (r - l) / 2)
let pivot = nums[p]
let i = l
let j = r
while (i <= j) {
while (pivot > nums[i]) {
i++
}
while (pivot < nums[j]) {
@PradipShrestha
PradipShrestha / heap.js
Created August 17, 2021 04:15
Heapify javascript
class Heap {
buildHeaps(nums) {
for (let i = Math.ceil(nums.length / 2) - 1; i >= 0; i--) {
this.heapify(nums, i)
}
}
heapify(nums, i) {
let l = i * 2 + 1
let r = l + 1