Back to Practice Dashboard
Python BasicsEasy

Sort the elements of an array

Learn how to solve the 'Sort the elements of an array' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Write a function sort_array(arr) that takes a list of integers arr and returns a new list with all elements sorted in ascending order.

Constraints
  • 0 <= len(arr) <= 10^5
  • -10^9 <= arr[i] <= 10^9

Examples

Example 1
Input
arr = [5, 2, 8, 1, 9]
Output
[1, 2, 5, 8, 9]
Explanation

Elements rearranged from smallest to largest.

Example 2
Input
arr = [3, -1, 4, -1, 5]
Output
[-1, -1, 3, 4, 5]
Explanation

Negative numbers come first, duplicates are kept.

Example 3
Input
arr = [1]
Output
[1]
Explanation

Single element is already sorted.

Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor