Ejemplo n.º 1
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)
Ejemplo n.º 2
0
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, [])
Ejemplo n.º 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
Ejemplo n.º 4
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
Ejemplo n.º 5
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