MCPcopy Create free account
hub / github.com/course-dasheng/fe-algorithm / bubbleSort

Function bubbleSort

sort/sort.js:8–23  ·  view source on GitHub ↗
(arr)

Source from the content-addressed store, hash-verified

6console.log(arrary)
7
8function bubbleSort(arr){
9 // 每个人和右边人比较,如果你比他高,就交换位置,否则就不动
10 let len = arr.length-1
11// O(n^2)
12 for(let j=0;j<len;j++){
13 for(let i=0;i<len-j;i++){
14 if(arr[i]>arr[i+1]){
15 // let tmp = arr[i]
16 // arr[i] = arr[i+1]
17 // arr[i+1] = tmp
18 [arr[i],arr[i+1]] = [arr[i+1],arr[i]]
19 }
20 }
21 }
22 return arr
23}
24
25// console.log('冒泡排序',bubbleSort(arrary))
26

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected