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
冒泡排序(英语:Bubble Sort)是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。
冒泡排序对n个项目需要O(n^2)的比较次数,且可以原地排序。所以它对于包含大量的元素的数列排序是很没有效率的。
每一次排序:
function bubble_sort (array) { var i, j; for(i from 0 to length-1){ for(j from 0 to length-1-i){ if (array[j] > array[j+1]) // 交换 array[j] 和 array[j + 1] 的值 } } }
function bubble_sort(arr) { let l = arr.length; for(let i=0;i<l-1;i++){ // 排序n-1次 for(let j=0;j<l-i-1;j++){ if(arr[j]>arr[j+1]){ //进行交换 var bf = arr[j]; arr[j] = arr[j+1]; arr[j+1] = bf; } } } return arr }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
冒泡排序
冒泡排序(英语:Bubble Sort)是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。
冒泡排序对n个项目需要O(n^2)的比较次数,且可以原地排序。所以它对于包含大量的元素的数列排序是很没有效率的。
算法步骤
每一次排序:
伪代码
JS实现
The text was updated successfully, but these errors were encountered: