Nero Sort Love at first index.
The idea is simple:
- Neo sees the first number in the list.
- Neo falls in love with it immediately.
- From that moment on, every number Neo sees is turned into that first number.
The process is extremely decisive.
Given a list:
- If the list is empty, return the empty list.
- Save the first value as
favorite. - Replace every element in the list with
favorite. - Return the modified list.
NeroSort(list):
if list is empty:
return list
favorite = list[0]
for i from 0 to length(list) - 1:
list[i] = favorite
return list
Input:
[7, 2, 9, 4, 1]
Neo sees 7, falls in love, and decides every number deserves to be 7.
Output:
[7, 7, 7, 7, 7]
Once the first value is chosen, the rest of the list aligns with it.
- Time:
O(n) - Space:
O(1)if done in place
Nero Sort produces a highly consistent final list.
