diff --git a/cycle_sort.py b/cycle_sort.py new file mode 100644 index 0000000..a0c784b --- /dev/null +++ b/cycle_sort.py @@ -0,0 +1,54 @@ + +from OFA import * + +def sort(list = blocks): + for b in list: + print(b.h, end = " ") + print() + writes = 0 + LS = len(list) + + for i in range(LS-1): + item = list[i] + pos = i + for j in range(i + 1, LS): + if list[j].h < item.h: + pos += 1 + if pos == i: + continue + + while item == list[pos]: + pos += 1 + + + swap(pos, i, list) + writes += 1 + + + while pos != i: + + pos = i + + for j in range(i + 1, LS): + if list[j].h < item.h: + pos += 1 + + while item == list[pos]: + pos += 1 + + # list[pos], item = item, list[pos] + swap(pos, i, list) + writes += 1 + for b in list: + print(b.h, end = " ") + print() + +if __name__ == "__main__": + tk.title("Cycle Sort") + generate() + btn1 = Button(tk, text='Shuffle', bd='5', command=lambda: shuffle()) + btn2 = Button(tk, text='Sort', bd='5', command=lambda: sort()) + btn1.pack(side='left') + btn2.pack(side='left') + tk.mainloop() + diff --git a/main.py b/main.py index 0793ee6..66da637 100644 --- a/main.py +++ b/main.py @@ -14,6 +14,9 @@ from cocktail_sort import sort as CS +from cycle_sort import sort as CyS + + from OFA import * @@ -43,6 +46,8 @@ btn9 = Button(tk, text = 'Cocktail Sort', bd = '5', command = lambda: CS()) +btnnizam = Button(tk, text = 'Cycle Sort', bd = '5', command = lambda: CyS()) + btn1.place(x = 710, y = 5) btn2.place(x = 710, y = 65) @@ -61,4 +66,6 @@ btn9.place(x = 710, y = 275) +btnnizam.place(x = 710, y = 305) + tk.mainloop() \ No newline at end of file