Exemplo n.º 1
0
	def closest_to_zero(self):
		self._map_negatives()
		abs_array = [abs(self._array[x]) for x in range(len(self._array))]
		sa = sort(abs_array)

		min_dist = sys.maxint
		a1 = 0
		a2 = 0

		for i in range(len(sa)):
			if (sa[i] in self._neg.keys()):
				self._neg[sa[i]] = self._neg[sa[i]] - 1
				sa[i] = -1 * sa[i]

			if ((i + 1) < len(sa) and abs(sa[i] + sa[i + 1]) <= min_dist):
				if (abs(sa[i]) in self._neg.keys() and self._neg[abs(sa[i])] == 0):
					min_dist = abs(sa[i] + sa[i + 1])
					a1 = sa[i]
					a2 = sa[i + 1]

			if ((i - 1) >= 0 and abs(sa[i] + sa[i - 1]) <= min_dist):
				min_dist = abs(sa[i] + sa[i - 1])
				a1 = sa[i]
				a2 = sa[i - 1]

		return (a1, a2)
Exemplo n.º 2
0
 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 = merge_sort.sort(test_list)
     self.assertEqual(expected, result)
Exemplo n.º 3
0
 def test_sorting(self):
     sorted_list = sort([5, 2, 4, 7, 8, 1, 3, 2, 6])
     self.assertEqual(sorted_list, [1, 2, 2, 3, 4, 5, 6, 7, 8])
Exemplo n.º 4
0
import insertion_sort
import merge_sort
import numpy as np
import time

if __name__ == '__main__':
    # generate data
    np.random.seed(233)
    array = np.random.randint(10000, size=10000)

    # calculate execution time
    prev_time = time.time()
    sorted_array = merge_sort.sort(array)
    exec_time = time.time() - prev_time

    # assertion
    assert (sorted_array == sorted(array))
    print(exec_time)
Exemplo n.º 5
0
jon = []
#ran = 9000000000000000000000000000000000000000000000000000000000000000000
ran = 2 ** 140
length = [1000, 10000, 30000, 50000]
for lent in length:
    for i in range(0,lent):
        num = random.randint(100000,ran)
        A.append(num)
        #B.append(num)
        C.append(num)

    start2 = time.clock()
    radix_sort.radixsort(C)
    radix_time = time.clock() - start2
    print ("tong use time: ", time.clock() - start2)


    start = time.clock()
    merge_sort.sort(A, 0, len(A))
    merge_time = time.clock() - start
    print ("merge sort use: ", time.clock() - start )
    jon.append({"x": lent, "mst": merge_time, "rst": radix_time})

f = open('result.json','w')

f.write(json.dumps(jon))

#start1 = time.clock()
#maopao.mp(B)
#print ("maopao use time:", time.clock() - start1)
Exemplo n.º 6
0
 def test_mergesort_should_handle_edge_cases(self):
     for k, v in self.i_o:
         self.assertEqual(merge_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.º 8
0
 def test_sort_on_sample_list(self):
     data = [2, -2, 1, 3, 0, 2, -3]
     self.assertEqual(merge_sort.sort(data), sorted(data))
     self.assertEqual(merge_sort.sort_efficiently(data), sorted(data))
Exemplo n.º 9
0
from random import randint
import merge_sort
import selection
import statistics as s
import os
rand_array = x = [randint(0, 10000) for p in range(0, 100000)]
out = merge_sort.sort(rand_array)
array = [4, 2, 3, 1]
sorted, operations = merge_sort.sort(array, out_ops=True)
print(sorted)
print(operations)

medsEst, operations = selection.select(rand_array, out_ops=True)
medsReal = s.median_low(rand_array)
print(medsEst)
print(medsReal)
print(operations)
print(len(rand_array))
Exemplo n.º 10
0
 def testThreeElementArray(self):
     arr = [1, 2, 3]
     arr_sorted = arr
     self.assertEqual(merge_sort.sort(arr), arr_sorted)
     arr = [3, 1, 2]
     self.assertEqual(merge_sort.sort(arr), arr_sorted)
Exemplo n.º 11
0
 def testLargeArray(self):
     arr = [random.randint(0, 100000) for i in range(10000)]
     self.assertEqual(merge_sort.sort(arr), sorted(arr))
Exemplo n.º 12
0
 def testSingleElementArray(self):
     arr = [1]
     self.assertEqual(merge_sort.sort(arr), arr)
Exemplo n.º 13
0
 def testTwoElementArray(self):
     arr = [1, 2]
     self.assertEqual(merge_sort.sort(arr), arr)
     arr = [2, 1]
     self.assertEqual(merge_sort.sort(arr), [1, 2])
    def test_sort_results_are_in_order(self):
        array_to_sort = [7, 1, 11, 3, 5, 2, 9, 10, 8, 4, 6]
        sorted_array = merge_sort.sort(array_to_sort)
        expected_sort_results = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

        self.assertEqual(expected_sort_results, sorted_array)
Exemplo n.º 15
0
	def test_mergesort_should_handle_edge_cases(self):
		for k,v in self.i_o:
			self.assertEqual(merge_sort.sort(k), v)
Exemplo n.º 16
0
	def test_mergesort_should_sort_lists_correctly(self):
		for k,v in self.i_o:
			self.assertEqual(merge_sort.sort(k), v)
Exemplo n.º 17
0
 def test_merge(self):
     for arr in self.test_arr:
         self.assertEqual(merge_sort.sort(arr), sorted(arr))
Exemplo n.º 18
0
 def testEmptyArray(self):
     self.assertIsNone(merge_sort.sort([]))
Exemplo n.º 19
0
 def test_sort_on_negative_elements(self):
     data = [-2, -1, 0, -3]
     self.assertEqual(merge_sort.sort(data), sorted(data))
     self.assertEqual(merge_sort.sort_efficiently(data), sorted(data))
Exemplo n.º 20
0
def test_unsorted_list():
    unsorted_list = [6, 2, 8, 4, 5, 3, 3, 4, 88, 2, 99, -43, 0]
    m_sort.sort(unsorted_list)
    assert unsorted_list == [-43, 0, 2, 2, 3, 3, 4, 4, 5, 6, 8, 88, 99]
Exemplo n.º 21
0
 def test_sort_on_empty_list(self):
     data = []
     self.assertEqual(merge_sort.sort(data), sorted(data))
     self.assertEqual(merge_sort.sort_efficiently(data), sorted(data))
Exemplo n.º 22
0
def test_empty_list():
    empty = []
    m_sort.sort(empty)
    assert empty == []
Exemplo n.º 23
0
 def test_mergesort_should_sort_lists_correctly(self):
     for k, v in self.i_o:
         self.assertEqual(merge_sort.sort(k), v)
Exemplo n.º 24
0
def test_sorted_list():
    sorted_list = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6]
    m_sort.sort(sorted_list)
    assert sorted_list == [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6]
Exemplo n.º 25
0
from merge_sort import sort
import random

n = 100

array = list()

while n > 0:
    array.append(random.randint(1, 10000))
    n -= 1

array = sort(array)
print(array)
Exemplo n.º 26
0
 def test_if_sorted_for_different_list(self):
     list = [8, 7, 6, 5, 4, 3, 2, 1]
     sorted_list = list.copy()
     sorted_list.sort()
     self.assertEqual(sort(list), sorted_list)
Exemplo n.º 27
0
 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.º 28
0
 def test_single_element_list(self):
     list = [1]
     sorted_list = list.copy()
     sorted_list.sort()
     self.assertEqual(sort(list), sorted_list)
Exemplo n.º 29
0
 def test_sort_on_single_element(self):
     data = [0]
     self.assertEqual(merge_sort.sort(data), sorted(data))
     self.assertEqual(merge_sort.sort(data), sorted(data))
Exemplo n.º 30
0
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)

bisection_search.search(array=[-1, 0, 2, 3, 11], target=3)
""" STRING OPS """
Exemplo n.º 31
0
 def test_two_element_list(self):
     list = [2, 1]
     sorted_list = list.copy()
     sorted_list.sort()
     self.assertEqual(sort(list), sorted_list)
Exemplo n.º 32
0
 def test_sort_on_two_elements_out_of_order(self):
     data = [2, 1]
     self.assertEqual(merge_sort.sort(data), sorted(data))
     self.assertEqual(merge_sort.sort_efficiently(data), sorted(data))
Exemplo n.º 33
0
 def test_sort_on_odd_number_of_elements(self):
     data = [2, 1, 3]
     self.assertEqual(merge_sort.sort(data), sorted(data))
     self.assertEqual(merge_sort.sort_efficiently(data), sorted(data))
Exemplo n.º 34
0
 def test_sort_on_duplicate_elements(self):
     data = [2, 1, 2, 3, 1, 3]
     self.assertEqual(merge_sort.sort(data), sorted(data))
     self.assertEqual(merge_sort.sort_efficiently(data), sorted(data))
Exemplo n.º 35
0
import sys
import globalsed
if not "/home/zh/git/python/sort/" in sys.path:
    sys.path.append("/home/zh/git/python/sort/")
import merge_sort

def open_txt():
    try:
        infile = file('interArray.txt', 'r')
        for x in infile.readlines():
            passlist.append(x)
        infile.close()
    except:
        return 0
globalsed.init()
passlist = []
open_txt()
#print len(passlist)
#passlist = [3,6,2,4,8]
merge_sort.sort(passlist, 0, len(passlist))
print("chang", globalsed.inter)
 def test_sort(self):
     array = [1, 5, 6, 3, 2, 1, 0]
     merge_sort.sort(array)
     self.assertEqual(array, [0, 1, 1, 2, 3, 5, 6])