SHARE
TWEET

Test

a guest Sep 16th, 2019 121 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Ascending Order Insertion Sort
  2. def insertionSortAsc(my_list):
  3.     for index in range(1,len(my_list)):
  4.         current_element = my_list[index]
  5.         pos = index
  6.         while current_element < my_list[pos-1] and pos>0:
  7.             my_list[pos] = my_list[pos-1]
  8.             pos = pos-1
  9.         my_list[pos] = current_element
  10.  
  11. #Descending Order Insertion Sort
  12. def insertionSortDesc(my_list):
  13.     for index in range(1,len(my_list)):
  14.         current_element = my_list[index]
  15.         pos = index
  16.         while current_element > my_list[pos-1] and pos>0:#Change < to > in first condition
  17.             my_list[pos] = my_list[pos-1]
  18.             pos = pos-1
  19.         my_list[pos] = current_element
  20.  
  21. #Getting keyboard inputs
  22. arr = list()
  23. num = input("Enter the number of elements you want:")
  24. for i in range(int(num)):
  25.     n = input("number :")
  26.     arr. append(int(n))
  27. insertionSortAsc(arr)
  28. print("Ascending :",arr)
  29. insertionSortDesc(arr)
  30. print("Descending :",arr)
  31.  
  32. #Ascending Order Bubble Sort
  33.  
  34. def BubbleSortAsc(my_list):
  35.     for j in range(len(my_list)-1):
  36.         for i in range(len(my_list)-1):
  37.             if my_list[i] > my_list[i+1]:
  38.                 my_list[i],my_list[i+1]=my_list[i+1],my_list[i]
  39.  
  40. #Descending Order Bubble Sort
  41.  
  42. def BubbleSortDesc(my_list):
  43.     for j in range(len(my_list)-1):
  44.         for i in range(len(my_list)-1):
  45.             if my_list[i] < my_list[i+1]:#Change > to < in first condition
  46.                 my_list[i],my_list[i+1]=my_list[i+1],my_list[i]
  47.  
  48. #Ascending Order Bubble Sort with all iterations
  49.  
  50. def BubbleSortAscAll(my_list):
  51.     for j in range(len(my_list)-1):
  52.         for i in range(len(my_list)-1-j):
  53.             if my_list[i] > my_list[i+1]:
  54.                 my_list[i],my_list[i+1]=my_list[i+1],my_list[i]
  55.                 print(my_list)
  56.             else:
  57.                 prin(my_list)
  58.         print()
  59.        
  60. #Descending Order Bubble Sort with all iterations
  61.  
  62. def BubbleSortDescAll(my_list):
  63.     for j in range(len(my_list)-1):
  64.         for i in range(len(my_list)-1-j):
  65.             if my_list[i] < my_list[i+1]:
  66.                 my_list[i],my_list[i+1]=my_list[i+1],my_list[i]
  67.                 print(my_list)
  68.             else:
  69.                 prin(my_list)
  70.         print()
  71.  
  72. #Ascending Order Selection Sort
  73. def SelectionSortAsc(arr):
  74.     for i in range (len(arr)):
  75.         min_val = min(arr[i:])#min
  76.         min_ind = arr.index(min_val)
  77.         arr[i],arr[min_ind] = arr[min_ind],arr[i]
  78.  
  79. #Descending Order Selection Sort
  80. def SelectionSortDesc(arr):
  81.     for i in range (len(arr)):
  82.         max_val = max(arr[i:])#max
  83.         max_ind = arr.index(max_val)
  84.         arr[i],arr[max_ind] = arr[max_ind],arr[i]
  85.  
  86. #Selection Sort function
  87. def selectionsort(arr):
  88.     n=len(arr)
  89.     for j in range(0,n):
  90.         smallest=j
  91.         for i in range(j+1,n-1):
  92.             if(arr[i]<arr[smallest]):
  93.                smallest=i
  94.         arr[j],arr[smallest]=arr[smallest],arr[j]
  95.     return arr
  96.  
  97. #Getting Output
  98. number_array = [50,60,10,40,20,30,90]
  99. print(selectionsort(number_array))
  100.  
  101. #Bubble Sort function
  102. def bubblesort(arr):
  103.     for i in range(len(arr)):
  104.         for j in range(0,len(arr)-i-1):
  105.             if arr[j]<arr[j+1]:
  106.                 arr[j],arr[j+1] = arr[j+1],arr[j]
  107.  
  108.  
  109. #Insertion sort function
  110. def insertionsort(arr):
  111.  
  112.     for i in range(1, len(arr)):
  113.         key = arr[i]
  114.         j=i-1
  115.         while j>=0 and key < arr[j]:
  116.             arr[j+1] = arr[j]
  117.             j-=1
  118.         arr[j+1] = key
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top