constminIndex = (numbers) => { var index = 0 for (let i = 1; i < numbers.length; i++) { if (numbers[i] < numbers[index]) { index = i } } return index }
constswap = (array, i, j) => { let temp = array[i] array[i] = array[j] array[j] = temp }
constsort = (numbers) => { for (let i = 0; i < numbers.length - 1; i++) { console.log(`----`) console.log(`i: ${i}`) let index = minIndex(numbers.slice(i)) + i console.log(`index: ${index}`) console.log(`min: ${numbers[index]}`) if (index !== i) { swap(numbers, index, i) console.log(`swap ${index}: ${i}`) console.log(numbers) } } return numbers }
constcountingSort = arr => { let hashTable = {}, max = 0, result = [] for (let i = 0; i < arr.length; i++) { if (!(arr[i] in hashTable)) { hashTable[arr[i]] = 1 } else { hashTable[arr[i]] += 1 } if (arr[i] > max) { max = arr[i] } } for (let j = 0; j <= max; j++) { if (j in hashTable) { for (let i = 0; i < hashTable[j]; i++) { result.push(j)