- var arr = [6,8,1,5,7,0,9]
- function bubbleSort(arr){
- var len = arr.length;
- for (var i = 0; i < len; i++){
- for (var j = 0; j < len - 1 - i; j++) {
- if (arr[j] > arr[j + 1]){
- var temp = arr[j + 1];
- arr[j + 1] = arr[j];
- arr[j] = temp;
- }
- }
- }
- return arr;
- }
- bubbleSort(arr)
- console.log(arr)
- //------------上面一种方法,下面一种分开看--------------------------------------
- var a = [4,5,6,7,8,9,0,1,2,3]
- for(var i = 0;i < a.length;i++){
- for(var j = i + 1;j<a.length;j++){
- if(a[i]>a[j]){
- var c = a[j]
- a[j] = a[i]
- a[i] = c
- }
- }
- }
- console.log(a)
暂无评论