Advertisement
AgungAlfiansyah

Untitled

Apr 10th, 2018
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.78 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Apr  9 13:37:48 2018
  4.  
  5. @author: Agung Alfiansyah
  6. """
  7. from random import randint
  8. """
  9. def binarySearch(Lin, key):
  10.    print(Lin)
  11.    if len(Lin) == 0:
  12.        return -1
  13.    else:
  14.        c = len(Lin)//2
  15.        if Lin[c]==key:
  16.            return c
  17.        else:
  18.            if key<Lin[c]:
  19.                print(c," KIRI")
  20.                return binarySearch(Lin[:c],key)
  21.            else:
  22.                print(c," KANAN")
  23.                return binarySearch(Lin[c+1:],key)
  24. """    
  25.  
  26. def binarySearch(Lin,l,r,key):
  27.     if l==r-1:
  28.         return -1
  29.     else:
  30.         c=(l+r)//2
  31.         if Lin[c]==key:
  32.             return c
  33.         else:
  34.             if key<Lin[c]:
  35.                 return binarySearch(Lin,l,c,key)
  36.             else:
  37.                 return binarySearch(Lin,c,r,key)
  38. """            
  39. testlist = [i*2 for i in range(0,256)]
  40. print(binarySearch(testlist,0,len(testlist),154))
  41. """
  42.  
  43. def binarySearchIterative(Lin,l,r,key):
  44.     while l!=r-1:
  45.         c=(l+r)//2
  46.         print(l,c,r)
  47.         if Lin[c]==key:
  48.             return c
  49.         elif Lin[c]>key:
  50.             r=c
  51.         else:
  52.             l=c
  53.     return -1
  54.  
  55. """
  56. testlist = [i*2 for i in range(0,256*256*256)]
  57. print(binarySearchIterative(testlist,0,len(testlist),254*256))
  58. """
  59.  
  60. def LinearSearch(Lin,key):
  61.     for i in range (0,len(Lin)):
  62.         if Lin[i]==key:
  63.             return i
  64.     return -1
  65.  
  66. """
  67. testlist = [i*2 for i in range(0,256*2)]
  68. print(LinearSearch(testlist,254*2-1))
  69.  
  70. """
  71.  
  72. def Min(Lin):
  73.     print(Lin)
  74.     if len(Lin) == 1:
  75.         #print(Lin[0])
  76.         return Lin[0]
  77.     elif len(Lin) == 2:
  78.         if Lin[0]<=Lin[1]:
  79.             #print(Lin[0])
  80.             return Lin[0]
  81.         else:
  82.             #print(Lin[1])
  83.             return Lin[1]
  84.     else:
  85.         c=len(Lin)//2
  86.         L1=Min(Lin[:c])
  87.         L2=Min(Lin[c:])
  88.         return Min([L1,L2])
  89.  
  90. #testlist = [randint(0,256) for i in range(0,12)]
  91. #print(Min(testlist))
  92.  
  93. def CountingSort(Lin,maxval):
  94.     count=[0]*(maxval+1)
  95.    
  96.     for i in Lin:count[i]=count[i]+1
  97.     Lsort=[]
  98.     for i in range (0,maxval+1):
  99.         for j in range (0,count[i]):
  100.             Lsort.append(i)
  101.     return Lsort
  102.  
  103.  
  104.  
  105. def partition(Lin, start, end):
  106.     pivot = Lin[(start)]
  107.     left = start+1
  108.     right = end
  109.     done = False
  110.     print (left,right)
  111.     while not done:
  112.         while left <= right and Lin[left] <= pivot:
  113.             left = left + 1
  114.         while Lin[right] >= pivot and right >=left:
  115.             right = right -1
  116.         if right < left:
  117.             done= True
  118.         else:
  119.             temp=Lin[left]
  120.             Lin[left]=Lin[right]
  121.             Lin[right]=temp
  122.     # swap
  123.     temp=Lin[start]
  124.     Lin[start]=Lin[right]
  125.     Lin[right]=temp
  126.     return right
  127.  
  128. def QuickSort(Lin, start, end):
  129.     if start < end:
  130.         # partition the list
  131.         pivot = partition(Lin, start, end)
  132.         # sort both halves
  133.         QuickSort(Lin, start, pivot-1)
  134.         QuickSort(Lin, pivot+1, end)
  135.     return Lin
  136.  
  137.  
  138. #testlist = [randint(0,256) for i in range(0,16)]
  139. #print(QuickSort(testlist,0,len(testlist)-1))
  140.  
  141.  
  142.  
  143. def merge(left, right):
  144.     if not len(left) or not len(right):
  145.         return left or right
  146.  
  147.     result = []
  148.     i, j = 0, 0
  149.     while (len(result) < len(left) + len(right)):
  150.         if left[i] < right[j]:
  151.             result.append(left[i])
  152.             i+= 1
  153.         else:
  154.             result.append(right[j])
  155.             j+= 1
  156.         if i == len(left) or j == len(right):
  157.             result.extend(left[i:] or right[j:])
  158.             break
  159.     return result
  160.  
  161. def MergeSort(list):
  162.     if len(list) < 2:
  163.         return list
  164.  
  165.     middle = len(list)//2
  166.     left = MergeSort(list[:middle])
  167.     right = MergeSort(list[middle:])
  168.  
  169.     return merge(left, right)
  170.  
  171. testlist = [randint(0,256*256) for i in range(0,16*256*256)]
  172. print(MergeSort(testlist))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement