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
class TestQuickSort(unittest.TestCase):

    @classmethod
    def setUpClass(self):

        self.items = [38, 27, 43, 50, 9, 82, 10]
        self.sort = QuickSort()

        
    def test_quick_sort(self):
        print '---- quicksort ----'
        print self.sort.quick_sort(self.items, 0, 6)
Exemple #3
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
 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
class TestQuicksort(unittest.TestCase):

    def setUp(self):
        self.q = QuickSort(None)

    def test_quick_sort_ordered_on_regular_list(self):
        l = [9, 6, 7, 3, 0, 1, 5, 2, 8, 4]
        r = self.q.quick_sort(l)

        self.assertEquals(r, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

    def test_quick_sort_empty_list(self):
        l = []
        r = self.q.quick_sort(l)

        self.assertEquals(r, [])
class TestQuickSort(unittest.TestCase):
    """ quicksort 的测试用例
    """

    def setUp(self):
        self.qs = QuickSort()

    def tearDown(self):
        self.qs = None

    def testCommon(self):
        assert self.qs.sort([2, 4, 1, 3]) == [1, 2, 3, 4]

    def testBoundary(self):
        assert self.qs.sort([]) == []

    def testSingle(self):
        assert self.qs.sort([1,]) == [1,]

    def testEqu(self):
        assert self.qs.sort([1, 1]) == [1, 1]

    def testNeg(self):
        assert self.qs.sort([1, -1]) == [-1, 1]

    def testRandom(self):
        data = [random.randint(1,1000) for i in range(1,1000)]

        qdata = data[:]
        data.sort()

        assert self.qs.sort(qdata) == data
Exemple #7
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'))
Exemple #8
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 #9
0
 def test_empty_Array(self):
     arr = []
     result = QuickSort().sort(arr)
     self.assertEqual([], result)
Exemple #10
0
 def test_array_all_same(self):
     arr = [1, 1, 1]
     result = QuickSort().sort(arr)
     self.assertEqual([1, 1, 1], result)
Exemple #11
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 #12
0
 def get_algorytm(self):
     self.list_agloritm = [BubbleSort(), QuickSort()]
Exemple #13
0
from bubblesort import BubbleSort
from insertionsort import InsertionSort
from selectionsort import SelectionSort
from mergesort import MergeSort
from quicksort import QuickSort


def isSorted(x, key=lambda x: x):
    return all([key(x[i]) <= key(x[i + 1]) for i in xrange(len(x) - 1)])


if __name__ == "__main__":
    to_sort = [613, 55, 8721, 472, 94, 72, 74, 8, 61, 356]
    _sorted = [8, 55, 61, 72, 74, 94, 356, 472, 613, 8721]

    #BubbleSort.sort(to_sort)
    #InsertionSort.sort(to_sort)
    #SelectionSort.sort(to_sort)
    #MergeSort.sort(to_sort)
    QuickSort.sort(to_sort)
    print to_sort
    assert isSorted(to_sort)
    def test_quicksort(self):
        self.assertEqual(QuickSort()(L1), L1s)

        for i in range(1):
            self.assertEqual(QuickSort()(L2), L2s)
 def test_quicksort_class_singleton(self):
     a = QuickSort()
     b = QuickSort()
     self.assertEqual(id(a), id(b))
 def setUp(self):
     self.q = QuickSort(None)
Exemple #17
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 #18
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 #19
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 #20
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 #22
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 #23
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 #24
0
    def setUpClass(self):

        self.items = [38, 27, 43, 50, 9, 82, 10]
        self.sort = QuickSort()
Exemple #25
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))