Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Sort an Array Without Built-ins
00:00
5 left

Sort an Array Without Built-ins

MediumPython

Problem

How would you sort an array without using an inbuilt function?

Implement sort_array(nums) to return the elements in ascending order without calling Python's sorting functions or other built-in sorting utilities. The input is a list of integers, and the function should return a new sorted list without modifying the original.

Input: nums, a list of integers. Output: a list containing the same values in ascending order.

Examples:

  • [5, 2, 8, 1] returns [1, 2, 5, 8].
  • [3, 3, -1, 0] returns [-1, 0, 3, 3].

Constraints: 0 <= len(nums) <= 5,000; values are between -10^9 and 10^9.

Constraints

  • 0 <= len(nums) <= 5,000
  • -10^9 <= nums[i] <= 10^9
  • Do not call sorted(), list.sort(), or another built-in sorting utility
  • Return a new list and do not modify nums

Function Signature

def sort_array(nums):
Interviewer

Your question is Sort an Array Without Built-ins. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.