Guest User

Untitled

a guest
May 23rd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. def selectionsort(L):
  2.     for x in range(len(L)-1):
  3.         minpos = x
  4.         for y in range(x, len(L)):
  5.             if L[y] < L[minpos]:
  6.                 minpos = y
  7.         L[x], L[minpos] = L[minpos], L[x]
  8.     return L
  9.  
  10.  
  11. def insertionsort(list):
  12.     for index in range(1, len(list)):
  13.         value = list[index]
  14.         i = index - 1
  15.  
  16.         while i>=0:
  17.             if value < list[i]:
  18.                 list[i+1] = list[i]
  19.                 list[i] = value
  20.                 i = i - 1
  21.             else:
  22.                 break
  23.     return list
  24.  
  25.  
  26.  
  27.  
  28.  
  29. def bubblesort(sequenz):
  30.     for x in range(len(sequenz)):
  31.         for k in range(len(sequenz) - 1):
  32.             if sequenz[k] > sequenz[k+1]:
  33.                 sequenz[k],sequenz[k+1] = sequenz[k+1],sequenz[k]
  34.     return sequenz
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. def quicksort(links,rechts,liste):
  42.         G=links
  43.         k=rechts
  44.         pivot=int((rechts+links)/2)
  45.         while (not ((G==k)and(G==pivot)) ):
  46.             while ((not liste[G]>liste[pivot]) and (G<pivot)):
  47.                 G+=1
  48.             while ((not liste[k]<liste[pivot]) and (k>pivot)):
  49.                 k-=1
  50.             if (G==pivot):
  51.                 pivot=k
  52.             elif (k==pivot):
  53.                 pivot=G
  54.             (liste[G],liste[k])=(liste[k],liste[G])
  55.  
  56.         if (pivot-links > 1):
  57.             quicksort(links,pivot-1,liste)
  58.         if (rechts-pivot > 1):
  59.             quicksort(pivot+1,rechts,liste)
  60.         return L
  61.  
  62.  
  63.  
  64.  
  65. # Test
  66. print("Selectionsort:")
  67. L = [25, 17, 32, 56, 25, 19, 8, 66, 29, 6, 20, 29]
  68. print(L)
  69. L = selectionsort(L)
  70. print(L)
  71.  
  72.  
  73. print("Insertionsort:")
  74. L = [25, 17, 32, 56, 25, 19, 8, 66, 29, 6, 20, 29]
  75. print(L)
  76. L = insertionsort(L)
  77. print(L)
  78.  
  79.  
  80. print("Bubblesort:")
  81. L = [25, 17, 32, 56, 25, 19, 8, 66, 29, 6, 20, 29]
  82. print(L)
  83. L = bubblesort(L)
  84. print(L)
  85.  
  86.  
  87. print("Quicksort:")
  88. L = [25, 17, 32, 56, 25, 19, 8, 66, 29, 6, 20, 29]
  89. print(L)
  90. L = quicksort(0,len(L)-1, L)
  91. print(L)
Add Comment
Please, Sign In to add comment