Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. a = [2, 3, 4, 1]
  2. b = [4, 2, 5, 1, 2, 7, 6, 0]
  3.  
  4. def bubble_sort(A):
  5.   n = len(A)
  6.   while n > 1:
  7.     for i in range(0, n - 1):
  8.       if A[i] > A[i + 1]:
  9.         A[i], A[i + 1] = A[i + 1], A[i]
  10.     n -= 1
  11.   return A
  12.  
  13.  
  14. def bubble_sort2(A):
  15.   for j in range(len(A)):
  16.     for i in range(len(A) - 1):
  17.       if A[i] > A[i + 1]:
  18.          A[i], A[i + 1] = A[i + 1], A[i]
  19.   return A
  20.  
  21.  
  22. # print('Przed sortowaniem', a)
  23. # print('Po sortowaniu', bubble_sort2(a))
  24.  
  25. # print('Przed sortowaniem', b)
  26. # print('Po sortowaniu', bubble_sort2(b))
  27.  
  28. def counting_sort(array):
  29.     maxval = max(array)
  30.     n = len(array)
  31.     m = maxval + 1
  32.     count = [0] * m               # init with zeros
  33.     for a in array:
  34.         count[a] += 1             # count occurences
  35.     new_array = []
  36.     for liczba in range(m):
  37.       for ilosc_wystapien in range(count[liczba]):
  38.         new_array.append(liczba)
  39.     return new_array
  40.  
  41. # print('Przed sortowaniem', a)
  42. # print('Po sortowaniu', counting_sort(a))
  43.  
  44. # print('Przed sortowaniem', b)
  45. # print('Po sortowaniu', counting_sort(b))
  46.  
  47.  
  48. def bucket_sort(alist):
  49.     largest = max(alist)
  50.     length = len(alist)
  51.     size = largest/length
  52.  
  53.     buckets = [[] for _ in range(length)]
  54.     for i in range(length):
  55.         j = int(alist[i]/size)
  56.         if j != length:
  57.             buckets[j].append(alist[i])
  58.         else:
  59.             buckets[length - 1].append(alist[i])
  60.  
  61.     for i in range(length):
  62.         insertion_sort(buckets[i])
  63.     result = []
  64.     for i in range(length):
  65.         result = result + buckets[i]
  66.  
  67.     return result
  68.  
  69. def insertion_sort(alist):
  70.     for i in range(1, len(alist)):
  71.         temp = alist[i]
  72.         j = i - 1
  73.         while (j >= 0 and temp < alist[j]):
  74.             alist[j + 1] = alist[j]
  75.             j = j - 1
  76.         alist[j + 1] = temp
  77.  
  78. a = [7, 10, 2, 8, 4, 5, 27]
  79. print('Sorting list', a)
  80. print('Bucket sort', bucket_sort(a))
  81.  
  82.  
  83.  
  84.  
  85. def selecting_sort(alist):
  86.   for i in range(len(alist)):
  87.     min_ = alist[i] # 10
  88.     idx = i # 1
  89.     for j in range(i + 1, len(alist)):
  90.       if alist[j] < min_:
  91.         min_ = alist[j]
  92.         idx = j
  93.     alist[i], alist[idx] = alist[idx], alist[i]
  94.   return alist
  95.  
  96. a = [7, 10, 2, 8, 4, 5, 27]
  97. print('Sorting list', a)
  98. print('Selection sort', bucket_sort(a))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement