Advertisement
Guest User

Max Selection Sort

a guest
Apr 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. def swap(list, a_idx, b_idx):
  2.     temp = list[a_idx]
  3.     list[a_idx] = list[b_idx]
  4.     list[b_idx] = temp
  5.  
  6. def selection_sort(list):
  7.     for i in range(0, len(list)):
  8.         max = list[0]
  9.         max_idx = 0
  10.         for j in range(1, len(list) - i):
  11.             if(max < list[j]):
  12.                 max = list[j]
  13.                 max_idx = j
  14.         last_place_idx = len(list) - 1 - i
  15.         if(max_idx == last_place_idx):
  16.             print("Max already in the last place!")
  17.         else:
  18.             swap(list, last_place_idx, max_idx)
  19.             '''
  20.            temp = list[last_place_idx]
  21.            list[last_place_idx] = list[max_idx]
  22.            list[max_idx] = temp
  23.            '''
  24.  
  25. list = [3,4,1,2,6,5]
  26.  
  27. print("List at the start: " + str(list))
  28.  
  29. selection_sort(list)
  30.  
  31.  
  32. print("List at the end: " + str(list))
  33.  
  34. print("End")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement