Exemplo n.º 1
0
def tim_sort(arr, n):  
   
    # Sort individual subarrays of size RUN  
    for i in range(0, n, RUN):  
        sort(arr, i, min((i+31), (n-1)))  
    
    # start merging from size RUN (or 32). It will merge  
    # to form size 64, then 128, 256 and so on ....  
    size = RUN 
    while size < n:  
       
        # pick starting point of left sub array. We  
        # are going to merge arr[left..left+size-1]  
        # and arr[left+size, left+2*size-1]  
        # After every merge, we increase left by 2*size  
        for left in range(0, n, 2*size):  
           
            # find ending point of left sub array  
            # mid+1 is starting point of right sub array  
            mid = left + size - 1 
            right = min((left + 2*size - 1), (n-1))  
    
            # merge sub array arr[left.....mid] &  
            # arr[mid+1....right]  
            merge(arr, left, mid, right)  
          
        size = 2*size 
Exemplo n.º 2
0
def sort(array, bucketSize=DEFAULT_BUCKET_SIZE):
  if len(array) == 0:
    return array

  # Determine minimum and maximum values
  minValue = array[0]
  maxValue = array[0]
  for i in range(1, len(array)):
    if array[i] < minValue:
      minValue = array[i]
    elif array[i] > maxValue:
      maxValue = array[i]

  # Initialize buckets
  bucketCount = math.floor((maxValue - minValue) / bucketSize) + 1
  buckets = []
  for i in range(0, bucketCount):
    buckets.append([])

  # Distribute input array values into buckets
  for i in range(0, len(array)):
    buckets[math.floor((array[i] - minValue) / bucketSize)].append(array[i])

  # Sort buckets and place back into input array
  array = []
  for i in range(0, len(buckets)):
    insertion_sort.sort(buckets[i])
    for j in range(0, len(buckets[i])):
      array.append(buckets[i][j])

  return array
Exemplo n.º 3
0
def sort(array, bucketSize=DEFAULT_BUCKET_SIZE):
    if len(array) == 0:
        return array

    # Determine minimum and maximum values
    minValue = array[0]
    maxValue = array[0]
    for i in range(1, len(array)):
        if array[i] < minValue:
            minValue = array[i]
        elif array[i] > maxValue:
            maxValue = array[i]

    # Initialize buckets
    bucketCount = math.floor((maxValue - minValue) / bucketSize) + 1
    buckets = []
    for i in range(0, bucketCount):
        buckets.append([])

    # Distribute input array values into buckets
    for i in range(0, len(array)):
        buckets[math.floor(
            (array[i] - minValue) / bucketSize)].append(array[i])

    # Sort buckets and place back into input array
    array = []
    for i in range(0, len(buckets)):
        insertion_sort.sort(buckets[i])
        for j in range(0, len(buckets[i])):
            array.append(buckets[i][j])

    return array
Exemplo n.º 4
0
def main():
    unsorted = [random.randint(-999, 9999) for i in range(0, 25)]
    print("unsorted list:", unsorted)
    bubble_sort.sort(unsorted)
    merge_sort.sort(unsorted)
    insertion_sort.sort(unsorted)
    quick_sort.sort(unsorted)
Exemplo n.º 5
0
def sort(a, p, q, k=8):
    if q - p < k:
        return insertion_sort.sort(a, p, q)
    if p < q:
        r = (p + q) // 2
        sort(a, p, r, k)
        sort(a, r + 1, q, k)
        merge(a, p, r, q)
    return a
 def test_sort(self):
     """ Compare results of sorting a random list with insertion sort
     against sorting that same list using Python's built sorting. """
     test_list = []
     for x in range(10000):
         test_list.append(randrange(0, 100000))
     expected = sorted(test_list)
     result = insertion_sort.sort(test_list)
     self.assertEqual(expected, result)
Exemplo n.º 7
0
def sort(array, max_value):
    buckets = [[] for _ in range(len(array))]

    for i in range(len(array)):
        j = int(array[i] / (max_value / len(array)))
        if j != len(array):
            buckets[j].append(array[i])
        else:
            buckets[j - 1].append(array[i])

    for i in range(len(array)):
        insertion_sort.sort(buckets[i])

    result = []
    for i in range(len(array)):
        result.extend(buckets[i])

    return result
Exemplo n.º 8
0
def bucket_sort(A: list):
    C = []
    length = len(A)
    B = [None] * length
    for i in range(length):
        B[i] = [None]
    for j in A:
        if B[int((length * j) // 1)] == [None]:
            B[int((length * j) // 1)] = [j]
        else:
            B[int((length * j) // 1)].append(j)
    for k in B:
        if not k == [0]:
            insertion_sort.sort(k)
    for x in B:
        if x == [None]:
            continue
        C = C + x
    return C
Exemplo n.º 9
0
def quicker_sort(it):
    if len(it) < 10:
        return insertion_sort.sort(it)
    pivot = it.pop(get_pivot(it))
    less, greater = [], []
    for x in it:
        if x < pivot:
            less.append(x)
        else:
            greater.append(x)
    return sort(less) + [pivot] + sort(greater)
Exemplo n.º 10
0
 def test_dataset3(self):
     inputData = [90, -20, 8, 11, 3]
     expectedData = [-20, 3, 8, 11, 90]
     self.assertEqual(sort(inputData), expectedData)
Exemplo n.º 11
0
 def test_dataset2(self):
     inputData = [20, 2, 10, 6, 52, 31, 0, 45, 79, 40]
     expectedData = [0, 2, 6, 10, 20, 31, 40, 45, 52, 79]
     self.assertEqual(sort(inputData), expectedData)
 def test_sortrandorder(self):
     arr = [random.randint(0, 10000) for i in xrange(0, 10000)]
     sortarr = sort(arr)
     for i in xrange(1, 10000):
         self.assertGreaterEqual(sortarr[i], sortarr[i - 1])
def test_sorted_list():
    sorted_list = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6]
    in_sort.sort(sorted_list)
    assert sorted_list == [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6]
Exemplo n.º 14
0
 def test_sort(self):
     array = [0, 5, 1, 9, 4, 0, 2, 4]
     expected = [0, 0, 1, 2, 4, 4, 5, 9]
     self.assertEqual(expected, sort(array))
Exemplo n.º 15
0
def test_insertion_sort():
    list_of_items = [54, 26, 93, 17, 77, 31, 44, 55, 20]
    print(list_of_items)
    insertion_sort.sort(list_of_items)
    print(list_of_items)
Exemplo n.º 16
0
 def test_dataset1(self):
     inputData = [5, 3, 6, 9, 10, 45, 23, 12, 4, 2]
     expectedData = [2, 3, 4, 5, 6, 9, 10, 12, 23, 45]
     self.assertEqual(sort(inputData), expectedData)
Exemplo n.º 17
0
	def test_insertionsort_should_handle_edge_cases(self):
		for k,v in self.i_o:
			self.assertEqual(insertion_sort.sort(k), v)
Exemplo n.º 18
0
	def test_insertionsort_should_sort_lists_correctly(self):
		for k,v in self.i_o:
			self.assertEqual(insertion_sort.sort(k), v)
Exemplo n.º 19
0
import pattern_search
import perfect_number
import prime_factorisation
import prime_number
import quick_sort
import selection_sort
import sieve_of_eratosthenes
import transposition_cipher
""" SORTING """

bubble_sort.sort(array=[3, 2, 11, -1, 0])

array = [0.48, 0.27, 0.12, 0.21, 0.43, 0.25]
bucket_sort.sort(array, max_value=max(array))

insertion_sort.sort(array=[3, 2, 11, -1, 0])

merge_sort.sort(array=[3, 2, 11, -1, 0])

array = [3, 2, 11, -1, 0]
quick_sort.sort(array, left=0, right=len(array) - 1)

selection_sort.sort(array=[3, 2, 11, -1, 0])

lexicographic_order.order(words=['aab', 'aaa', 'aa', 'aaa'])
""" SEARCHING """

pattern_search.search(text=list('karykatura'), pattern=list('ka'))

bisection_root_finding.bisect(a=-4, b=4, error=0.001)
Exemplo n.º 20
0
 def test_insertion(self):
     for arr in self.test_arr:
         insertion_sort.sort(arr)
         self.assertEqual(arr, sorted(arr))
Exemplo n.º 21
0
 def test_dataset4(self):
     inputData = [33, 22, 11, 21, 55, 32, 3, 4]
     expectedData = [3, 4, 11, 21, 22, 32, 33, 55]
     self.assertEqual(sort(inputData), expectedData)
Exemplo n.º 22
0
 def test_dataset5(self):
     inputData = [11, 22, 33, 44, 55, 66, 77]
     expectedData = [77, 66, 55, 44, 33, 22, 11]
     self.assertEqual(sort(inputData, True), expectedData)
def test_empty_list():
    empty = []
    in_sort.sort(empty)
    assert empty == []
 def test_sort_inorder(self):
     arr = [i for i in xrange(0, 10000)]
     sortarr = sort(arr)
     for i in xrange(1, 10000):
         self.assertGreaterEqual(sortarr[i], sortarr[i-1])
Exemplo n.º 25
0
import sys

sys.path.insert(0, '../')

from Utils import *
from insertion_sort import sort

# MAIN
print("Unsorted list is: {0}".format(ARRAY_10))
sorted = sort(ARRAY_10)
print("Sorted list is:   {0}".format(sorted))
Exemplo n.º 26
0
 def test_insertion(self):
     for array in self.test_array:
         self.assertEqual(insertion_sort.sort(array), sorted(array))
Exemplo n.º 27
0
 def test_insertionsort_should_sort_lists_correctly(self):
     for k, v in self.i_o:
         self.assertEqual(insertion_sort.sort(k), v)
 def test_sortrandorder(self):
     arr = [random.randint(0, 10000) for i in xrange(0,10000)]
     sortarr = sort(arr)
     for i in xrange(1, 10000):
         self.assertGreaterEqual(sortarr[i], sortarr[i-1])
Exemplo n.º 29
0
 def test_insertionsort_should_handle_edge_cases(self):
     for k, v in self.i_o:
         self.assertEqual(insertion_sort.sort(k), v)
def test_unsorted_list():
    unsorted_list = [6, 2, 8, 4, 5, 3, 3, 4, 88, 2, 99, -43, 0]
    in_sort.sort(unsorted_list)
    assert unsorted_list == [-43, 0, 2, 2, 3, 3, 4, 4, 5, 6, 8, 88, 99]
 def test_sort_revorder(self):
     arr = [i for i in xrange(10000, 0, -1)]
     sortarr = sort(arr)
     for i in xrange(1, 10000):
         self.assertGreaterEqual(sortarr[i], sortarr[i - 1])