def get_comparisons(pivot_func):
    with open('QuickSort.txt') as f:
        array = [int(l) for l in f.readlines()]

    qsort = QuickSort(pivot_func=pivot_func)
    comparisons, sorted_array = qsort.sort(array)

    # Validate sort result
    assert all(sorted_array[i] == el for i, el in enumerate(sorted(array)))

    return comparisons
Exemple #2
0
 def test_quicksort(self):
     self.is_ordenado(QuickSort(self.ordenado[:], 'QuickSort Ordenado'))
     self.is_ordenado(QuickSort(self.reverso[:], 'QuickSort Reverso'))
     self.is_ordenado(QuickSort(self.aleatorio1[:], 'QuickSort Aleatorio1'))
     self.is_ordenado(QuickSort(self.aleatorio2[:], 'QuickSort Aleatorio2'))
     self.is_ordenado(QuickSort(self.aleatorio3[:], 'QuickSort Aleatorio3'))
     self.is_ordenado(QuickSort(self.aleatorio4[:], 'QuickSort Aleatorio4'))
 def handle_quick_sort(self):
     print "Please enter in your values one by one, when you are done, insert x"
     x = False
     quick_sort_list = []
     while not x:
         val = raw_input(">")
         if val == "x":
             x = True
         else:
             quick_sort_list.append(val)
     q = QuickSort()
     print "You entered ", quick_sort_list
     q.quicksort(quick_sort_list, 0, len(quick_sort_list) - 1)
     print "Sorted is ", quick_sort_list
Exemple #4
0
def rank_sort(records, N):
    # COMPUTE AND APPEND THE RANK
    counter = 0
    for record in records:
        counter += 1
        rank, operations = gray_rank(record, N)
        counter += operations
        record.append(rank)

    # QUICKSORT ON THE RECORDS
    qs = QuickSort(comparator_function='b')
    records = qs.quick_sort(records, 0, len(records) - 1)
    iterations = qs.total_iterations + counter
    return records, iterations
Exemple #5
0
def test_gray_order_sort():
    num_fields = 3

    # GENERATE GREY CODE AND SHUFFLE
    grey_code = generate_grey_code(fields=num_fields, start=0, end=3)
    randomized_grey_code = grey_code.copy()
    random.shuffle(randomized_grey_code)

    qs = QuickSort()
    sorted = qs.quick_sort(randomized_grey_code, 0,
                           len(randomized_grey_code) - 1, grayorder)
    is_different = False

    for grey, sort in zip(grey_code, sorted):

        if grey == sort:
            is_same = True
        else:
            is_different = True
            is_same = False
        print("{} | {} | {}".format(grey, sort, is_same))
    return is_different
Exemple #6
0
 def test_array_all_same(self):
     arr = [1, 1, 1]
     result = QuickSort().sort(arr)
     self.assertEqual([1, 1, 1], result)
Exemple #7
0
 def test_sorted_array(self):
     arr = [-8, -5, 1, 2, 2, 3, 8, 10]
     result = QuickSort().sort(arr)
     self.assertEqual(result, [-8, -5, 1, 2, 2, 3, 8, 10])
Exemple #8
0
 def get_algorytm(self):
     self.list_agloritm = [BubbleSort(), QuickSort()]
    def test_quicksort(self):
        self.assertEqual(QuickSort()(L1), L1s)

        for i in range(1):
            self.assertEqual(QuickSort()(L2), L2s)
Exemple #10
0
def gray_order_sort(records):
    qs = QuickSort(comparator_function='a')
    records = qs.quick_sort(records, 0, len(records) - 1)
    iterations = qs.total_iterations
    return records, iterations
Exemple #11
0
 def test_random_array(self):
     arr = [8, 1, -5, 3, 2, 2, -8, 10]
     result = QuickSort().sort(arr)
     self.assertEqual(result, [-8, -5, 1, 2, 2, 3, 8, 10])
Exemple #12
0
from compare import compare_dates, compare_ids
from filehandler import parse_file, write_file
from quicksort import QuickSort

(header, data) = parse_file('random_data.csv')
sorted_data = QuickSort(data, compare_ids, compare_dates).sort()
write_file('random_data_sorted.csv', header, sorted_data)
Exemple #13
0
# Ryan Dawkins
# October 9th, 2014
# Programming Assignment 1

from quicksort import QuickSort

if __name__ == "__main__":
    sorter = QuickSort()

    a0 = [200,133,41,34,2,11]
    a1 = a0[:] # Creates a copy of a0

    # Example showing the recursive method
    print "Recursive:"
    print a0
    sorter.recursive(a0, 0, len(a0)-1)
    print a0

    print

    # Example showing the iterative method
    print "Iterative:"
    print a1
    sorter.iterative(a1, 0, len(a1)-1)
    print a1
Exemple #14
0
from mergesort import MergeSort
from quicksort import QuickSort
from insertionsort import InsertionSort
from bubblesort import BubbleSort

MergeSort = MergeSort()
QuickSort = QuickSort()
InsertionSort = InsertionSort()
BubbleSort = BubbleSort()
 def setUp(self):
     self.qs = QuickSort()
Exemple #16
0
 def test_empty_Array(self):
     arr = []
     result = QuickSort().sort(arr)
     self.assertEqual([], result)
Exemple #17
0
 def test_reverse_sorted_array(self):
     arr = [5, 3, 1, -4, -6]
     result = QuickSort().sort(arr)
     self.assertEqual([-6, -4, 1, 3, 5], result)
Exemple #18
0
from bubble_sort import BubbleSort
from quicksort import QuickSort
from sort_time_complexity import SortTimeComplexity


if __name__ == '__main__':
    array = [1, 5, 63, 72, 9, 34]
    bubble_sorts = BubbleSort()
    bubble_sorts.get_data(array)
    print(bubble_sorts.sort(array))
    quick_sort = QuickSort()
    # quick_sort.get_data()
    sort_machine = SortTimeComplexity()
    print(sort_machine.measure_time(bubble_sorts, array))


Exemple #19
0
    def setUpClass(self):

        self.items = [38, 27, 43, 50, 9, 82, 10]
        self.sort = QuickSort()
 def test_quicksort_class_singleton(self):
     a = QuickSort()
     b = QuickSort()
     self.assertEqual(id(a), id(b))