Sort servers by response time
Problem statement
Sorting puts a list in order so the slowest or fastest values are easy to spot. You do not need to write a sorting method yourself, every language has one built in.
Right now you only have the response times, not the server names attached to them. Keeping each name paired with its time needs dictionaries, which come later in this track. For now, sort the times.
Common mistake: assigning the result of .sort() to a variable (Python).
Wrong: sorted_times = times.sort().sort() rearranges the list in place and returns nothing, so sorted_times ends up as None. Use sorted(times) when you want a new sorted list back.
Your task: given response times in milliseconds, [340, 120, 95, 210, 180], sort them from fastest (smallest) to slowest (largest) and print each one on its own line.
Expected output:
95120180210340Approach
Use each language's built-in sort:
- Python:
sorted(times)returns a new sorted list and leaves the original alone. (times.sort()sorts in place instead.) - Java:
Arrays.sort(times)sorts the array in place. - C++:
std::sort(times.begin(), times.end())sorts the vector in place.
DSA connection: sorting first, then working on ordered data, is a pattern you will reuse often in DSA.
times = [340, 120, 95, 210, 180]
sorted_times = sorted(times)
for t in sorted_times:
print(t)