0

Query about python nested for loop and sorting

Greeting everyone. I'm practicing on sorting lists using python, and I wanted to implement two for loops; the first that keeps track of the minimal value of the list, and the second iterate over the remaining of the list expect the already iterated index, and then compare it to that minimum value, then it appends that value to the list and return it. class Solution: def sortColors(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ n = len(nums) for i in range(n): for j in range(0, n-i-1): if nums[j] > nums[j+1]: nums[j] , nums[j+1] = nums[j+1], nums[j] I've found this solution that is quite similar to my logic that i wanted to implement, but I didn't quite understand the the index of the nested loop. I'll be thankful for further explanation! The question is originally in leetcode as follows https://leetcode.com/problems/sort-colors/

9th Oct 2022, 7:25 AM
Fatimah Mohammad Emad EL Den
1 Réponse
0
Why are you overcomplicating yourself? Is there any reason you are not using the list.sort method?
19th Oct 2022, 10:33 PM
Lee