js冒泡排序
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>JS冒泡排序</title> </head> <body> <script> var arr = [3,1,5,22,13,18,66,43,33,68]; var temp; function sortFn(arr){ for(var i = 0; i < arr.length - 1; i++){ for(var j = 0; j < arr.length - 1 - i; j++){ if(arr[j] > arr[j+1]){ temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } return arr; } console.log(sortFn(arr)); //[1, 3, 5, 13, 18, 22, 33, 43, 66, 68] </script> </body> </html>