I'm new to Python and this is the quick sort code I wrote:
def Quick(List):
if len(List) <= 1:
return List
pivot = List[0]
l_idx = 0
r_idx = len(List) - 1
while l_idx != r_idx:
while List[l_idx] < pivot and l_idx < r_idx:
l_idx += 1
while List[r_idx] > pivot and l_idx < r_idx:
r_idx -= 1
if l_idx < r_idx:
List[l_idx], List[r_idx] = List[r_idx], List[l_idx]
List = Quick(List[0: (l_idx)]) + [List[l_idx]] + Quick(List[(l_idx + 1):])
return List
The list I'm trying to sort is [598, 862, 950, 953, 373, 981, 201, 258, 427, 669].
If I run the following code, I'll get
xxx = [598, 862, 950, 953, 373, 981, 201, 258, 427, 669]
print(xxx)
# Gives me: [598, 862, 950, 953, 373, 981, 201, 258, 427, 669]
print(Quick(xxx))
# Gives me:[201, 258, 373, 427, 598, 669, 862, 950, 953, 981], which is the correct result.
print(xxx)
# Gives me: [427, 258, 201, 373, 598, 981, 953, 950, 862, 669], which is not the correct result.
I'm wondering why I get a completely different result than the one I returned when I print the list "xxx" the second time. Thanks!!