def test_mixed_integers(self):
        my_list = InsertionSort(
            [5, 1, 10, 0, -2, 11, -3, 2, -100, 61, -176743, 163, -176742, 9])
        sorted_ll = list(
            sorted([
                5, 1, 10, 0, -2, 11, -3, 2, -100, 61, -176743, 163, -176742, 9
            ]))

        self.assertListEqual(sorted_ll, my_list.cleaned())
    def testInsertionSort(self):
        # randomly generate 1000 random numbers in the range -100 to 100
        A = [random.randint(-100,100) for c in range(1000)]

        print("before sort",A)
        InsertionSort.sort(A)
        print('After sort',A)
        for i in range(1, len(A)):
            if(A[i-1] > A[i]):
                self.fail('insertion sort method fails')
Esempio n. 3
0
def QuickInsertionSort(A, p=0, r=None):
    """
    p and r represent the list's border.
    if you want to sort the entire list just leave empty
    """
    if r is None:
        r = len(A)-1
    if p < r:
        if r-p < 20:
            print('a')
            InsertionSort(A, p, r)
        else:
            q = RandomizedPartition(A, p, r)
            QuickInsertionSort(A, p, q-1)
            QuickInsertionSort(A, q+1, r)
    def sort(self, arr: list) -> list:
        """
        Sorts an array using the bucket sort algorithm

        Parameters:
            arr (list): list to be sorted

        Returns:
            list: the sorted list
        """ 
        n = len(arr)

        buckets = []
        for i in range(n):
            buckets.append([])

        for x in arr:
            index = int(n*x)
            buckets[index].append(x)

        ins_sort = InsertionSort()
        for i in range(n):
            buckets[i] = ins_sort.sort(buckets[i])
        return list(chain.from_iterable(buckets))
Esempio n. 5
0
            ]

            # Perform MergeSort on our randomly-generated data
            m_sorting = MergeSort()

            start = time.time()
            result = m_sorting.mergesort_asc(merge_data)
            end = time.time()
            elapsed = end - start

            merge[array_index] = [
                k, n, m_sorting._compares, m_sorting._shifts, elapsed
            ]

            # Perform InsertionSort on our randomly-generated data
            i_sorting = InsertionSort()

            start = time.time()
            i_sorting.insertionsort_asc(insert_data)
            end = time.time()
            elapsed = end - start

            insert[array_index] = [
                k, n, i_sorting._compares, i_sorting._shifts, elapsed
            ]

            # Don't forget to increment the index
            array_index += 1

    # Uses Pandas' DataFrame to save the results in CSV files
    # This way the conversion will be much, much quicker, but will use up more memory.
def test_insertion_sort():
	sort = InsertionSort() 
	unsorted_numbers = [9,3,5,1,4,5,7]
	assert_equals(sort.sort(unsorted_numbers), sorted(unsorted_numbers))
 def test(self, args):
     InsertionSort.sort(args)
     assert InsertionSort.is_sorted(args)
     InsertionSort.show(args)
    def test_negative_integers(self):
        my_list = InsertionSort([-1, -2, -100, -66, -3, -1, -7, -200])
        sorted_ll = list(sorted([-1, -2, -100, -66, -3, -1, -7, -200]))

        self.assertListEqual(sorted_ll, my_list.cleaned())
Esempio n. 9
0
import random
from timeit import default_timer as timer
from insertion_sort import InsertionSort
from radix_sort import RadixSort

if __name__ == '__main__':

    # Insertion Sort caso random
    for i in [100, 1000, 10000, 100000]:

        print("InsertionSort su Array di {} elementi Random".format(i))

        arr = [random.randint(1, 100) for _ in range(0, i)]

        start = timer()
        InsertionSort().insertionsort(arr)
        end = timer()

        print("Tempo di esecuzione: " + str(end - start) + "s")
        print("\n")

    # Insertion Sort caso migliore
    for i in [100, 1000, 10000, 100000]:
        print("InsertionSort su Array di {} elementi array ordinato".format(i))

        arr = [0 for _ in range(0, i)]
        for i in range(1, i):
            arr[i] = arr[i - 1] + 1

        start = timer()
        InsertionSort().insertionsort(arr)
Esempio n. 10
0
 def insertion_sort(array):
     start = timeit.default_timer()
     copied_array = copy.copy(array)
     InsertionSort.sort(array=copied_array)
     end = timeit.default_timer()
     return format(end - start, '.5f')
Esempio n. 11
0
from counting_sort import CountingSort
from radix_sort import RadixSort
from heap_sort import HeapSort
from merge_sort import MergeSort
from quick_sort import QuickSort
from bubble_sort import BubbleSort
from selection_sort import SelectionSort
from insertion_sort import InsertionSort
from time import time

import random

options = {
    1: ("Bubble Sort", BubbleSort()),
    2: ("Selection Sort", SelectionSort()),
    3: ("Insertion Sort", InsertionSort()),
    4: ("Quick Sort", QuickSort()),
    5: ("Merge Sort", MergeSort()),
    6: ("Radix Sort", RadixSort()),
    7: ("Counting Sort", CountingSort()),
    8: ("Heap Sort", HeapSort())
}
print("Sorting Options: ")
for i in options.keys():
    print("Option {} : {}".format(i, options[i][0]))
while True:
    option = int(input("Enter sorting option: "))
    sort_alg = options[option][1]
    start = time()
    arr = [random.randint(-100000, 100000) for _ in range(10)]
    print(arr)
Esempio n. 12
0
class TestInsertionSort(SortTestSuite):

    sort = InsertionSort().sort
Esempio n. 13
0
    def test_positive_integers(self):
        my_list = InsertionSort([4, 10, 2, 3, 8, 10, 100, 5, 8, 0, 1, 101, 17])
        sorted_ll = list(sorted([4, 10, 2, 3, 8, 10, 100, 5, 8, 0, 1, 101,
                                 17]))

        self.assertListEqual(sorted_ll, my_list.cleaned())
Esempio n. 14
0
    def test_float_numbers(self):
        my_list = InsertionSort([0.01, 0.1, 1.21, 1.20, -1.1])
        sorted_ll = list(sorted([0.01, 0.1, 1.21, 1.20, -1.1]))

        self.assertListEqual(sorted_ll, my_list.cleaned())
Esempio n. 15
0
for z in range(0, m):
        # repetition for different orders of array size
        for i in range(1,n):    
                
                l = 10**i                                               # order of array size
                arr = [int(random.random()*100) for j in range(l)]      # create an array of random numbers with incremental order
                alist = [0]*len(arr)                                    # initialize an empty array of same size
                times[i-1][0] = l                                       # add the order as the first column in the matrix

                # a loop to go through all the sorting algorithms
                for g in range(1, 6):                                   
                        start = time.clock()                            # get a CPU clock tick
                        if g == 1:
                                temp = SelectionSort.sort(dup(alist, arr))
                        elif g == 2:
                                temp = InsertionSort.sort(dup(alist, arr))
                        elif g == 3:
                                temp = MergeSort.sort(dup(alist, arr))
                        elif g == 4:
                                temp = QuickSort.sort(dup(alist, arr))
                        elif g == 5:
                                temp = CountSort.sort(dup(alist, arr))
                        end = time.clock() - start                       # get a CPU clock tick and estimate algorithm r
                        setin(i, end, g)

endit = time.clock() - begin                                             # estimate overal calculation time
print ('Total time Elapsed:', endit, 'seconds.') 

# show the benchmark matrix
for i in range(0, n-1):
        print (times[i])
Esempio n. 16
0
# -*- coding: utf-8 -*-
import random
from timeit import default_timer as timer

from insertion_sort import InsertionSort

print "-" * 10 + "sorting numbers" + "-" * 10
items = []
for i in range(0, 10):
    items.append(random.randint(2, 999))
print "original items: %r" % items
items = items_worse = range(10 - 1, -1, -1)
ssort = InsertionSort(items)

# calculate execution time for our selection sort method
start = timer()
ssort.sort()
end = timer()
duration1 = end - start
# calculate execution time for python built-in sort method
start = timer()
items.sort()
end = timer()
duration2 = end - start

assert ssort.items == items
print "sorted items: %r" % ssort.items
print "Duration: our selection sort method - %ds, python builtin sort - %ds" % (
    duration1, duration2)
 def test_InsertionSort(self):
     for i in range(NumberOfTests):
         l = [random.randint(0, 10000) for j in range(MaxSize)]
         self.assertEqual(InsertionSort(l), sorted(l))
 def test_insertion_sort(self):
     algo = SortingTestWrapper(InsertionSort(), self.n, self.seed)
     self.assertListEqual(algo.integer_sort(), self.verification.integer_sort())