Home

Published

- 1 min read

js comb sort

img of js comb sort

The solution for this is noted below

js comb sort

Solution

   function combSort(array) {
	let inc = array.length
	let swap = true

	while (!inc == 1 || swap) {
		// recalibrate inc
		inc = Math.floor(inc / 1.3)
		if (inc < 1) {
			inc = 1
		}

		swap = false
		for (let i = 0; i < array.length - inc; i++) {
			if (array[i] > array[i + inc]) {
				// swap position
				let tmp = array[i]
				array[i] = array[i + inc]
				array[i + inc] = tmp
				swap = true
			}
		}
	}
	return array
}

Try other methods by searching on the site. That is if this doesn’t work