0

hi I don't understand how this code work. Explain me pls. Thank you.

#how this function finds the smallest element of the array.
def findSmallest(arr):
  smallest = arr[0] 
  smallest_index = 0
  for i in range(1, len(arr)):
    if arr[i] < smallest: #--> why this is here and what does it mean?
      smallest_index = i
      smallest = arr[i]      
  return smallest_index

def selectionSort(arr):
  newArr = []
  for i in range(len(arr)):
      smallest = findSmallest(arr)
      newArr.append(arr.pop(smallest))
  return newArr

print(selectionSort([5, 3, 6, 2, 10]))

how "findSmallest" function finds the smallest element of the array?

2
  • 2
    why this is here and what does it mean? That line of code compares the current element to the smallest element seen so far. Commented May 27, 2020 at 19:46
  • In (if arr[i] < smallest:) smallest is a variable which stores the values of smallest of an array, and this condition is checking, if any element is an array is small then "smallest" variable. Commented May 27, 2020 at 19:54

2 Answers 2

3

The line you questioned specifically is comparing values. If the value at the current index (calculated by the for-loop) is less than the value stored in smallest (originally the first value in the list), then store this value as the new smallest value. I added more comments to the code to explain the rest of it.

#how this function finds the smallest element of the array.
def findSmallest(arr): #function with a parameter to hold an array
  smallest = arr[0]  #index 0 of passed array
  smallest_index = 0 
  for i in range(1, len(arr)): 
    if arr[i] < smallest: #if the value in the array at the index of i is smaller than the value at the index of 0
      smallest_index = i #the index of the smallest number is now at the position of i
      smallest = arr[i] #the smallest number is now the number at the index of i     
  return smallest_index #return the index position of the smallest number

def selectionSort(arr): #function passed an array
  newArr = [] 
  for i in range(len(arr)): #loop through passed array
      smallest = findSmallest(arr) #call the findSmallest function
      newArr.append(arr.pop(smallest)) #remove the smallest number from the passed array and add it to the new one. Repeat until passed array is empty
  return newArr #return the new array

print(selectionSort([5, 3, 6, 2, 10]))
Sign up to request clarification or add additional context in comments.

Comments

1

By reading it, looks like it orders given elements and puts them into an array from smallest to largest based on value. To answer your question about line 5, it compares the stored integer value i to the other ones in selectionSort so that the following code can print the sorted array. def selectionSort(arr) has a variable smallest that uses findSmallest to append the list of elements in numerical order.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.