Bubble Sort
Updated
Problem statement
Implement bubbleSort(nums) using bubble sort to return the integers of nums from smallest to largest.
On each left-to-right pass, compare neighboring elements and swap them when the left value is greater. Continue until the array is sorted. Do not replace bubble sort with a built-in sorting routine or a different sorting algorithm.
You may modify nums in place. Return the resulting sorted array, preserving every occurrence of each input value.
Function
bubbleSort(nums: int[]) → int[]Examples
Example 1
nums = [5,1,4,2,8]return = [1,2,4,5,8]Repeated neighboring swaps place the smaller values before the larger values.
Example 2
nums = [3,-1,3,0]return = [-1,0,3,3]Negative values sort first, and both occurrences of 3 remain.
Example 3
nums = [7]return = [7]A one-element array is already sorted.
Constraints
1 <= nums.length <= 2000.-10^9 <= nums[i] <= 10^9.
More Hartford Financial Services interview questions
Similar questions at other companies
- Group Transactions by HourAffirm · ONSITE INTERVIEWAffirm interview questions
- Merge Sorted ArrayDP World · ONSITE INTERVIEW
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Meeting RoomsGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Sort Products by Aisle With Frozen Items LastInstacart · ONSITE INTERVIEWInstacart interview questions
- Merge Two Arrays Into Sorted OrderNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Compress Consecutive Integer RangesDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Filter Anagrams of a TargetMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Rank Transform an ArrayAgoda · ONSITE INTERVIEWAgoda interview questions
- Sort Documents Left to RightFigma · ONSITE INTERVIEWFigma interview questions