Selection sort कोडिंग
पिछले वीडियो में, आपने selection sort एल्गोरिदम का अध्ययन किया था.
इस अभ्यास में, आपको selection_sort() फंक्शन को पूरा करके इसे इम्प्लीमेंट करना है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Data Structures और Algorithms
अभ्यास निर्देश
lowestको सूची के उस एलिमेंट पर सेट करें जो इंडेक्सiपर है.iवैरिएबल की अगली पोज़िशन से शुरू करते हुए सूची पर दोबारा इटरेट करें.- जाँचें कि इंडेक्स
jपर स्थित सूची का एलिमेंटlowestसे छोटा है या नहीं.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
def selection_sort(my_list):
list_length = len(my_list)
for i in range(list_length - 1):
# Set lowest to the element of the list located at index i
lowest = ____
index = i
# Iterate again over the list starting on the next position of the i variable
____ j in range(____, list_length):
# Compare whether the element of the list located at index j is smaller than lowest
if _____:
index = j
lowest = my_list[j]
my_list[i] , my_list[index] = my_list[index] , my_list[i]
return my_list
my_list = [6, 2, 9, 7, 4, 8]
selection_sort(my_list)
print(my_list)