Implement a function that performs a bubble sort. The function should take in an array of integers and return an array with the integers sorted in ascending order. The input array is modified.
bubbleSort([9, 3, 6, 2, 1, 11]); // [1, 2, 3, 6, 9, 11]bubbleSort([12, 16, 14, 1, 2, 3]); // [1, 2, 3, 12, 14, 16]
Bubble sort is a simple sorting algorithm which compares and swaps adjacent elements such that after every iteration over the array, one more element will be ordered/correctly placed, starting from the largest.
The algorithm starts by iterating over the array, comparing each adjacent pair of elements and swapping them if the larger one is on the left. This will ensure that after one whole iteration, the largest element in the array will be at the last index.
The algorithm then continues to iterate over the array and compares adjacent pairs, such that the 2nd largest element will be at the 2nd last index. The comparison of adjacent pairs can ignore the last element as it is already confirmed to be the largest element in the array through the first iteration.
For e.g., continuing from the algorithm above, the algorithm will move onto the 3rd iteration to compare adjacent pairs while ignoring the last and 2nd last element as they are already in the right order/place.
Implement a function that performs a bubble sort. The function should take in an array of integers and return an array with the integers sorted in ascending order. The input array is modified.
bubbleSort([9, 3, 6, 2, 1, 11]); // [1, 2, 3, 6, 9, 11]bubbleSort([12, 16, 14, 1, 2, 3]); // [1, 2, 3, 12, 14, 16]
Bubble sort is a simple sorting algorithm which compares and swaps adjacent elements such that after every iteration over the array, one more element will be ordered/correctly placed, starting from the largest.
The algorithm starts by iterating over the array, comparing each adjacent pair of elements and swapping them if the larger one is on the left. This will ensure that after one whole iteration, the largest element in the array will be at the last index.
The algorithm then continues to iterate over the array and compares adjacent pairs, such that the 2nd largest element will be at the 2nd last index. The comparison of adjacent pairs can ignore the last element as it is already confirmed to be the largest element in the array through the first iteration.
For e.g., continuing from the algorithm above, the algorithm will move onto the 3rd iteration to compare adjacent pairs while ignoring the last and 2nd last element as they are already in the right order/place.
Bubble sort is a stable, in-place, comparison-based algorithm that works well for small to medium-sized arrays as well as arrays that are partially sorted.
If unspecified:
Note: This question tackles in-place sorting for an output in ascending order. Refer to the 'Notes' section below on how to handle other cases.
/*** @param {Array<number>} arr The input integer array to be sorted.* @return {Array<number>}*/export default function bubbleSort(arr) {// Do multiple iterations over the array.for (let i = 0; i < arr.length; i++) {// For each iteration, compare every adjacent pairs while ignoring the last i elements that are already sorted.for (let j = 0; j < arr.length - i; j++) {// If the left element in the pair is larger than the right one, swap their positions to ensure that elements are sorted ascendingly.if (arr[j] > arr[j + 1]) {let temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}// Return the sorted array.return arr;}
<
instead of >
, as per below:if (arr[j] < arr[j + 1])
In the case of bubble sort, the time complexity is O(n2) as it requires a nested loop structure. The outer loop does multiple iterations over the array and the inner loop iterates over the array to compare each adjacent pairs and swap elements based on their correct order.
The space complexity for bubble sort is O(1), as it does in-place sorting and does not require additional storage proportional to input size.
console.log()
aparecerão aqui.