20 lines
601 B
JavaScript
20 lines
601 B
JavaScript
/* Prompt:
|
|
Generate a JavaScript function that implements bubble sort.
|
|
The function should be named 'bubbleSort' and take one parameter 'arr' (an array of numbers).
|
|
The function should return the sorted array.
|
|
Return only the function code, no explanation.
|
|
*/
|
|
|
|
function bubbleSort(arr) {
|
|
let n = arr.length;
|
|
for (let i = 0; i < n - 1; i++) {
|
|
for (let j = 0; j < n - 1 - i; j++) {
|
|
if (arr[j] > arr[j + 1]) {
|
|
let temp = arr[j];
|
|
arr[j] = arr[j + 1];
|
|
arr[j + 1] = temp;
|
|
}
|
|
}
|
|
}
|
|
return arr;
|
|
} |