Пример #1
0
 def test_sort(self):
     lists = []
     for l in self.lists:
         sort(l)
         lists.append(self.is_sorted(l))
     print("All {0} lists are sorted: {1}".format(self.nSorts, all(lists)))
     self.assertTrue(all(lists))
Пример #2
0
def sort(a):
  if(len(a) < 7):
    return insert.sort(a)

  mid = len(a)/2
  left = merge.sort(a[:mid])
  right = merge.sort(a[mid:])
  return merge.merge(left,right)
Пример #3
0
def test_sort():
    assert sort([1, 2, 3, 4, 5]) == [1, 2, 3, 4, 5]
    assert sort([1, 5, 2, 4, 3]) == [1, 2, 3, 4, 5]
    assert sort([4, 3, 2, 1]) == [1, 2, 3, 4]
    assert sort([3, 3, 3]) == [3, 3, 3]
    assert sort([2, 1]) == [1, 2]
    assert sort([1]) == [1]
    assert sort([]) == []
Пример #4
0
 def test_sort(self):
     tests = (
         (
             # kinda randomized numbers, as well as an odd number of elements
             [4, 1, 6, 5, 7, 8, 13, 12, 10, 11, 2, 3, 9],
             [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]),
         (
             # completely backwards list
             [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
             [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
         (
             # repeated elements
             [5, 5, 5, 5, 5],
             [5, 5, 5, 5, 5]),
         (
             # just a single element
             [1],
             [1]),
         (
             # no elements
             [],
             []))
     for notSorted, nowSorted in tests:
         self.assertEqual(sort(notSorted), nowSorted)
Пример #5
0
def test_sorting():
    '''Test if Array is being sorted'''
    array = [2, 5, 1]
    merge.sort(array)

    assert all(array[x] <= array[x + 1] for x in range(len(array) - 1))
Пример #6
0
                x = setupAsc(i)
            elif j == 4:
                x = setupA(i)
            #print(x) # tablica przed posortowaniem
            before = round(time.time_ns() / (10**9), 10)
            if s == 0:
                x = bubble.sort(x)
            elif s == 1:
                x = insertion.sort(x)
            elif s == 2:
                x = selection.sort(x)
            elif s == 3:
                x = counting.sort(x, i, maxNumber)
            elif s == 4:
                x = heap.sort(x)
            elif s == 5:
                x = quickRight.sort(x, 0, i - 1)
            elif s == 6:
                x = quickRandom.sort(x, 0, i - 1)
            elif s == 7:
                x = shell.sort(x)
            elif s == 8:
                x = merge.sort(x, 0, len(x) - 1)
            after = round(time.time_ns() / (10**9), 10)
            #print(x) # tablica po sortowaniu
            tmpTime += ';' + str(after - before)
            print(after - before)
        f.write(tmpTime + "\n")
        print(tmpTime)
    f.write('\n')
f.close()
Пример #7
0
def choose_order():
  while True:
    try:
      order = input('[0] ascend,  [1] descend -> ')
      if order == 0 or order == 1:
        return order
      else:
        print 'Invalid input!!'
    except ValueError:
      print 'ValueError! Please try again!'

def sort_order(order='ascend'):
  if order == 'ascend':
    return lambda x, y: x > y
  elif order == 'descend':
    return lambda x, y: x < y

if __name__ == '__main__':
  A = input_data()
  if choose_order() == 0:
    order = sort_order('ascend')
  else:
    order = sort_order('descend')

  print 'Selection: ' + str(selection.sort(copy.deepcopy(A), order))
  print 'Insertion: ' + str(insertion.sort(copy.deepcopy(A), order))
  print '    Shell: ' + str(shell.sort(copy.deepcopy(A), order))
  print '    Merge: ' + str(merge.sort(copy.deepcopy(A), 0, len(A)-1, order))
  print '     Bogo: ' + str(bogo.sort(copy.deepcopy(A), order))
Пример #8
0
from lecture_02_27.elapsedtime import ElapsedTime

import random

num = 100000

with ElapsedTime("built in"):
    array = [random.randint(0, 1000) for _ in range(num)]
    array.sort()

with ElapsedTime("merge"):
    import merge
    array = [random.randint(0, 1000) for _ in range(num)]
    merge.sort(array)
Пример #9
0
def test_single_entry():
    '''Handling 1 number'''
    array = [2]
    merge.sort(array)

    assert all(array[x] <= array[x + 1] for x in range(len(array) - 1))
Пример #10
0
    #bubble calls
    bubble_elapsed_time = time.time() - bubble_start

    data = [random.randint(0, size) for i in range(size)]
    insertion_start = time.time()
    InsertionSort.serialInsertionSort(data)
    insertion_elapsed_time = time.time() - insertion_start

    data = [random.randint(0, size) for i in range(size)]
    quick_start = time.time()
    quickSort.quickSort(data, 0, len(data) - 1)
    quick_elapsed_time = time.time() - quick_start

    data = [random.randint(0, size) for i in range(size)]
    merge_start = time.time()
    merge.sort(data, 0, size - 1)
    merge_elapsed_time = time.time() - merge_start

    selection_start = time.time()
    #selection calls
    selection_elapsed_time = time.time() - selection_start

    print("Time to for multiprocessed bubble sort: ", mp_bubble_elapsed_time)
    print("Time to for multiprocessed insertion sort: ",
          mp_insertion_elapsed_time)
    print("Time to for multiprocessed quick sort: ", mp_quick_elapsed_time)
    print("Time to for multiprocessed merge sort: ", mp_merge_elapsed_time)
    print("Time to for multiprocessed selection sort: ",
          mp_selection_elapsed_time)
    print("Time to for bubble sort: ", bubble_elapsed_time)
    print("Time to for insertion sort: ", insertion_elapsed_time)
Пример #11
0
 def test_sorted(self, first_value, second_value):
     self.assertEqual(sort(first_value), second_value)
Пример #12
0
    def test_reversed(self):
        self.input.reverse()

        actual = merge.sort(self.input)
        self.assertEqual(list(sorted(actual)), actual)
Пример #13
0
    def test_oneoff(self):
        self.input[0], self.input[1] = self.input[1], self.input[0]

        actual = merge.sort(self.input)
        self.assertEqual(list(sorted(actual)), actual)
Пример #14
0
 def test_sorted(self):
     actual = merge.sort(self.input)
     self.assertEqual(list(sorted(actual)), actual)
Пример #15
0
    def test_random(self):
        random.shuffle(self.input)

        actual = merge.sort(self.input)
        self.assertEqual(list(sorted(actual)), actual)
Пример #16
0
def test_negative():
    '''Handling of negative numbers'''
    array = [2, -5, 1]
    merge.sort(array)

    assert all(array[x] <= array[x + 1] for x in range(len(array) - 1))
Пример #17
0
def test_double_entry():
    '''Handling of repeating numbers'''
    array = [2, 5, 2]
    merge.sort(array)

    assert all(array[x] <= array[x + 1] for x in range(len(array) - 1))
Пример #18
0
            order = input('[0] ascend,  [1] descend -> ')
            if order == 0 or order == 1:
                return order
            else:
                print 'Invalid input!!'
        except ValueError:
            print 'ValueError! Please try again!'


def sort_order(order='ascend'):
    if order == 'ascend':
        return lambda x, y: x > y
    elif order == 'descend':
        return lambda x, y: x < y


if __name__ == '__main__':
    A = input_data()
    if choose_order() == 0:
        order = sort_order('ascend')
    else:
        order = sort_order('descend')

    print 'Selection: ' + str(selection.sort(copy.deepcopy(A), order))
    print 'Insertion: ' + str(insertion.sort(copy.deepcopy(A), order))
    print '    Shell: ' + str(shell.sort(copy.deepcopy(A), order))
    print '    Merge: ' + str(
        merge.sort(copy.deepcopy(A), 0,
                   len(A) - 1, order))
    print '     Bogo: ' + str(bogo.sort(copy.deepcopy(A), order))
Пример #19
0
def test_doubles():
    '''Handling of double values'''
    array = [3.1, 2.9, 2.4]
    merge.sort(array)

    assert all(array[x] <= array[x + 1] for x in range(len(array) - 1))
Пример #20
0
 def test_sort(self):
     list = generate_list(1000)
     x = merge.sort(list)
     self.assertTrue(x == sorted_list(list))
Пример #21
0
    print(next(g))

>> module is importable code
>> if __name__ != '__main__' then program is called form somewhere else as module


LISTS 

>> Arrays vs List :: hetro and h**o :: fixed and dynamic
>> list = [] for empty list
>> seq operation are applicable lst[2,20,2] :: len(lst) :: lst1+lst2 :: lst1*10 :: x in lst1 :: for i in lst1
>> lst = list[range[1,100,2]
>> lst.append(), del lst[1],lst.reverse(),
>> lst1 = lst2 will make both list to point to same memory address
>> to clone we should us lst1 = lst2[:] OR lst1=lst2.copy()
>> List Methods :: index(x), append(x), insert(i,x), list.copy(), extend(lst1), count(x), remove(x), pop(x), sort(reverse = True),reverse(), clear(), min(), max()
>> st1 = set(list) .. and set1.intersection(set2)
>> Nested list can be used as arrays or matrices 
>> List comprehension *******
          sqr = [x**2 for x in range(1,11) if x%2 == 0]
           lst = [i+j for i in x for j in y]
           lst = [w[0] for w in words]
          lst = [i for i in num1 if i not in num2]

sorted(my_list) will use TimSort. Timsort is a hybrid sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data

comparator >> data = sorted(data, cmp=Students.comparator)
where data is list of type class Student

TUPLES
>> tuples are similar to list except they are immutable
Пример #22
0
def m_sort(a):
  before = time.time()
  merge.sort(a)
  after = time.time()
  return (after - before)