Arranging a collection into some sorted order (ascending/descending, numerical/alphabetical/lexicographical) is a very common problem in computer science
student_grades = [88.3, 53.2, 76.6, 92.2, 81.9, 79.7, 62.1, 84.8, 83.3]
student_grades.sort()
print(student_grades)
Well-studied algorithmic problem
Many different algorithms (bubble sort, quicksort, merge sort, heap sort, selection sort, insertion sort, bucket sort, etc.)
Later computer science courses cover how to analyze and compare these algorithms.
Insertion Sort: is a simple, intuitive sorting algorithm
Works by splitting the list into two parts - a sorted part and an unsorted part
student_grades = [88.3, 53.2, 76.6, 92.2, 81.9, 79.7, 62.1, 84.8, 83.3]
#loop through all indices in the list, starting with index 1 (first element is already "sorted")
for current_grade_index in range(1,len(student_grades)):
print("popping from index",current_grade_index,", length of list:",len(student_grades))
#pop item at current index
curr_grade = student_grades.pop(current_grade_index)
print("item popped",curr_grade)
#loop from the beginning of the list up until the index we popped from
position_to_check = 0
while position_to_check < current_grade_index:
#compare popped item with each
#keep going until we run into one that our item isn't greater than
#insert the item at that spot
if student_grades[position_to_check] > curr_grade:
student_grades.insert(position_to_check, curr_grade)
break
position_to_check += 1
# if we got back to the current spot without finding a place
# to put this item, then put it back in its original spot
if position_to_check == current_grade_index:
student_grades.insert(position_to_check, curr_grade)
print(student_grades)
Because both pop and insert require Python to scan through the list, we end up doing more iterating through the list than we need to, which takes extra time.
Here's another approach that eliminates the need to use pop and insert by scanning from right-to-left starting with the item being inserted into the correct place. As we scan, we move the items over one spot at a time until we find the place where this one goes.
student_grades = [88.3, 53.2, 76.6, 92.2, 81.9, 79.7, 62.1, 84.8, 83.3]
for current_grade_index in range(1,len(student_grades)):
current_grade = student_grades[current_grade_index]
position = current_grade_index
#shift grades down the list until we find where this one goes
while position>0 and student_grades[position-1]>current_grade:
student_grades[position]=student_grades[position-1]
position = position-1
student_grades[position]=current_grade
print(student_grades)
Let's improve this code. Do the following: