Example #1
0
def main():
    a = create_list()
    start = time.time()
    quicksort(a)
    elapsed = (time.time() - start)
    print_list(a)
    print("--Quick Sort--")
    print("Total Sort Time: ", elapsed)
    print()
    input("press enter to run next sort")
    
    a = create_list()
    start = time.time()
    bubblesort(a)
    elapsed = (time.time() - start)
    print_list(a)
    print("--Bubble Sort--")
    print("Total Sort Time: ", elapsed)
    print()
    input("press enter to run next sort")
    
    a = create_list()
    start = time.time()
    hi_low_sort(a)
    elapsed = (time.time() - start)
    print_list(a)
    print("--hi_low Sort--")
    print("Total Sort Time: ", elapsed)
    input("press enter to end")
Example #2
0
    def q3_quicksort(self):
        tests = 1500
        points = 0

        for i in xrange(tests):
            l = [random() for i in xrange(randint(20, 50))]
            if sorted(l) == bubblesort(l):
                points += 1
        return ('quicksort', points, tests)
Example #3
0
def graph(list_sizes=np.arange(1, 1000, 100)):
    bubble_time = []
    sorted_time = []
    quicksort_time = []
    insert_time = []

    for x in list_sizes:
        test_list = list(np.random.randint(10, size=x))

        bubble_time_start = time()
        bubblesort(test_list)
        bubble_time.append(time() - bubble_time_start)

        sort_time_start = time()
        sorted(test_list)
        sorted_time.append(time() - sort_time_start)

        quicksort_time_start = time()
        quicksort(test_list)
        quicksort_time.append(time() - quicksort_time_start)

        insert_time_start = time()
        insertion_sort(test_list)
        insert_time.append(time() - insert_time_start)

    plt.loglog(list_sizes, bubble_time, '-o')
    plt.loglog(list_sizes, sorted_time, '-s')
    plt.loglog(list_sizes, quicksort_time, '-v')
    plt.loglog(list_sizes, insert_time, '-x')

    plt.xlabel('List Length')
    plt.ylabel('Sort Time (s)')
    plt.title('List Sort Time for Various Methods')
    plt.legend(['Bubble Sort', 'Sorted', 'Quicksort', 'Insertion Sort'], loc=2)

    plt.show()
def go():
    global g_type
    global var
    global N
    global bar_plot
    global start_fig
    global ax
    lst = [x + 1 for x in range(N)]
    random.seed(123)
    random.shuffle(lst)

    if var.get() == "b":
        title = "Bubble sort"
        generator = sort.bubblesort(lst)
    elif var.get() == "i":
        title = "Insertion sort"
        generator = sort.insertionsort(lst)
    elif var.get() == "q":
        title = "Quicksort"
        generator = sort.quicksort(lst, 0, N - 1)
    else:
        title = "Selection sort"
        generator = sort.selectionsort(lst)

    ax.set_title(title)
    ax.set_xlim(0, N)
    ax.set_ylim(0, int(1.07 * N))

    text = ax.text(0.02, 0.95, "", transform=ax.transAxes)

    iteration = [0]

    def update_fig(lst, rects, iteration):
        for rect, val in zip(rects, lst):
            rect.set_height(val)
        iteration[0] += 1
        text.set_text("# of operations: {}".format(iteration[0]))

    anim = animation.FuncAnimation(start_fig,
                                   func=update_fig,
                                   fargs=(bar_plot, iteration),
                                   frames=generator,
                                   interval=1,
                                   repeat=False)
    start_fig.canvas.draw()
Example #5
0
#Random list generator
length_list = np.arange(100, 2100, 100)
sample_list = []

for i in range(len(length_list)):
    sample_list.append(np.random.random_sample(length_list[i]))

#tests bubblesort for each list
time_list_bubble = []
time_list_sorted = []
time_list_quick = []
for n in range(len(length_list)):
    #bubblesort time
    sortstart = timeit.time.time()
    bubblesort(sample_list[n])
    sortend = timeit.time.time()
    time_list_bubble.append(sortend - sortstart)
    #sorted time
    sortstart1 = timeit.time.time()
    sorted(sample_list[n])
    sortend1 = timeit.time.time()
    time_list_sorted.append(sortend1 - sortstart1)
    #quicksort time
    sortstart2 = timeit.time.time()
    sorting(sample_list[n], 0, len(sample_list[n]) - 1)
    sortend2 = timeit.time.time()
    time_list_quick.append(sortend2 - sortstart2)

plt.plot(length_list, time_list_bubble, 'r', length_list, time_list_sorted,
         'b', length_list, time_list_quick, 'g')
Example #6
0
        czas_timsort = timeit.timeit(lambda: lista(N).sort(),
                                     number=ILOSC_PROBEK) / ILOSC_PROBEK
        wyniki.append({"name": "quicksort", "N": N, "time": czas_quicksort})
        wyniki.append({"name": "mergesort", "N": N, "time": czas_mergesort})
        wyniki.append({"name": "timsort", "N": N, "time": czas_timsort})

    # Selectsort, Insertsort, Bubblesort, Shakersort i Shellsort
    for N in [10**2, 10**3, 10**4]:
        czas_selectsort = timeit.timeit(
            lambda: sort.selectsort(lista(N), 0, N - 1),
            number=ILOSC_PROBEK) / ILOSC_PROBEK
        czas_insertsort = timeit.timeit(
            lambda: sort.insertsort(lista(N), 0, N - 1),
            number=ILOSC_PROBEK) / ILOSC_PROBEK
        czas_bubblesort = timeit.timeit(
            lambda: sort.bubblesort(lista(N), 0, N - 1),
            number=ILOSC_PROBEK) / ILOSC_PROBEK
        czas_shakersort = timeit.timeit(
            lambda: sort.shakersort(lista(N), 0, N - 1),
            number=ILOSC_PROBEK) / ILOSC_PROBEK
        czas_shellsort = timeit.timeit(
            lambda: sort.shellsort(lista(N), 0, N - 1),
            number=ILOSC_PROBEK) / ILOSC_PROBEK

        wyniki.append({"name": "selectsort", "N": N, "time": czas_selectsort})
        wyniki.append({"name": "insertsort", "N": N, "time": czas_insertsort})
        wyniki.append({"name": "bubblesort", "N": N, "time": czas_bubblesort})
        wyniki.append({"name": "shakersort", "N": N, "time": czas_shakersort})
        wyniki.append({"name": "shellsort", "N": N, "time": czas_shellsort})

    print_results(wyniki)
Example #7
0
	def testBubble(self):
		test_vec = self.test_vec()
		sort.bubblesort(test_vec)
		self.assertEqual(test_vec, sorted(self.test_vec()))
Example #8
0
def test_bubblesort(seq):
    assert_unsorted(seq)
    assert_sorted(bubblesort(seq))
Example #9
0
def test_bubblesort(seq):
    assert_unsorted(seq)
    assert_sorted(bubblesort(seq))
Example #10
0
import random
import argparse
import sort

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(nargs='?', type=int, default=1000, dest='number')
    args = parser.parse_args()
    lst = []
    for i in range(args.number):
        lst.append(random.randint(1,100))

    ans = sort.samplesort(list(lst))
    qs  = sort.quicksort(list(lst))
    ms  = sort.mergesort(list(lst))
    bs  = sort.bubblesort(list(lst))
    ins = sort.insertionsort(list(lst))
    sel = sort.selectionsort(list(lst))  
    
    print "Sample sort returns {} in {}".format(ans[0], ans[1])
    print "Quicksort matches sample: {} in {}".format(ans[0] == qs[0], qs[1])
    print "Mergesort matches sample: {} in {}".format(ans[0] == ms[0], ms[1])
    print "Bubblesort matches sample: {} in {}".format(ans[0] == bs[0], bs[1])
    print "Insertionsort matches sample: {} in {}".format(ans[0] == ins[0], ins[1])
    print "Selectionsort matches sample: {} in {}".format(ans[0] == sel[0], sel[1])
Example #11
0
from sort import bubblesort, quicksort, quicksort_used_in_quicksort
from timeit import timeit
import random


sorted_list = [x for x in range(1,500)]
not_sorted_list = [x for x in range(500,0,-1)]
equal_list = 500*[1]

rnd = random.sample(range(1, 1001), 1000)

t1_bubble = timeit ( str(bubblesort(sorted_list)) , number =1 , globals = globals ())
t2_bubble = timeit ( str(bubblesort(not_sorted_list)) , number =1 , globals = globals ())
t3_bubble = timeit ( str(bubblesort(equal_list)) , number =1 , globals = globals ())
t4_bubble = timeit ( str(bubblesort(rnd)) , number =1 , globals = globals ())


t1_quicksort = timeit ( str(quicksort(sorted_list)) , number =1 , globals = globals ())
t2_quicksort = timeit ( str(quicksort(not_sorted_list)) , number =1 , globals = globals ())
t3_quicksort = timeit ( str(quicksort(equal_list)) , number =1 , globals = globals ())
t4_quicksort = timeit ( str(quicksort(rnd)) , number =1 , globals = globals ())

print("lista uporzadkowana")
print("bubble: ",t1_bubble)
print("quick: ",t1_quicksort)

print("lista malejÄ…ca")
print("bubble: ",t2_bubble)
print("quick: ",t2_quicksort)

print("lista stalych elementow")
Example #12
0
 def test_Bubblesort(self):
     lst = [10, 3, 4, 1, 6, 8, 9, 2, 5, 7]
     result = ([x for x in range(1, 11)], 42)
     self.assertEqual(sort.bubblesort(lst), result)