Skip to content
CodeKruPro

Sort a list without a library sort (bubble, selection or insertion)

Coding problemEasyAsked in: Capgemini

Given a list of integers, return it sorted from smallest to largest. Write the sorting loop yourself: bubble sort, selection sort or insertion sort are all fine. Do not call a library sort; that is the point of the question.

The list has at most 2,000 values, so a simple quadratic sort (one that takes about n × n steps) is fast enough here. Duplicates and negative values are allowed.

Examples

Example 1
Input: nums = [5,2,9,1,5,6]
Output: [1,2,5,5,6,9]
Example 2
Input: nums = [3,-1,0]
Output: [-1,0,3]
Example 3
Input: nums = []
Output: []

Constraints

  • The list holds between 0 and 2,000 integers
  • Each value is a 32-bit integer

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

to answer and keep your progress. Reading is free for everyone.