MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / combSort

Function combSort

Sorts/CombSort.js:25–50  ·  view source on GitHub ↗

* combSort returns an array of numbers sorted in increasing order. * * @param {number[]} list The array of numbers to sort. * @return {number[]} The array of numbers sorted in increasing order.

(list)

Source from the content-addressed store, hash-verified

23 * @return {number[]} The array of numbers sorted in increasing order.
24 */
25function combSort(list) {
26 if (list.length === 0) {
27 return list
28 }
29 const shrink = 1.3
30 let gap = list.length
31 let isSwapped = true
32 let i = 0
33
34 while (gap > 1 || isSwapped) {
35 // Update the gap value for a next comb
36 gap = parseInt(parseFloat(gap) / shrink, 10)
37
38 isSwapped = false
39 i = 0
40
41 while (gap + i < list.length) {
42 if (list[i] > list[i + gap]) {
43 ;[list[i], list[i + gap]] = [list[i + gap], list[i]]
44 isSwapped = true
45 }
46 i += 1
47 }
48 }
49 return list
50}
51
52export { combSort }

Callers 1

CombSort.test.jsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected