We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
计数排序适合整数排序,计数排序的思想就是两点:
function countingSort(array) { const min = Math.min.apply(Math, array) const counts = [] let sortedIndex = 0 // 相当于把值做为下标,这样在顺序上就排好了,同时 // 计数每个元素出现的次数,因为可能有重复的数字 // 减去min是为了防止有负数,同时这样做可以节省空间比如排序1000-1100,只需要100个空间 array.forEach(item => { counts[item - min] = (counts[item - min] || 0) + 1 }) counts.forEach((count, index) => { while (count > 0) { array[sortedIndex] = index + min sortedIndex++ count-- } }) return array } let array = [-10, 1, 1, 2, 3, -10, -90] console.log(countingSort(array))
The text was updated successfully, but these errors were encountered:
No branches or pull requests
计数排序适合整数排序,计数排序的思想就是两点:
The text was updated successfully, but these errors were encountered: