From 8a2406d30b8955b62dd56128ebf95f2af6e96d1f Mon Sep 17 00:00:00 2001 From: Nizamuddin Mondal Date: Sun, 24 Oct 2021 11:28:07 +0530 Subject: [PATCH 1/4] cycle sort added --- cycle_sort.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 7 +++++++ 2 files changed, 61 insertions(+) create mode 100644 cycle_sort.py diff --git a/cycle_sort.py b/cycle_sort.py new file mode 100644 index 0000000..7ba7cae --- /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): + 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..253f356 100644 --- a/main.py +++ b/main.py @@ -14,6 +14,9 @@ from cocktail_sort import sort as CS +from cycle_sort import sort as CY + + 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: CY()) + 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 From 54b69a291c6811d9c53cf179cc8f84b491a10b62 Mon Sep 17 00:00:00 2001 From: Nizamuddin Mondal <89808462+Nizam420@users.noreply.github.com> Date: Sun, 24 Oct 2021 11:52:23 +0530 Subject: [PATCH 2/4] Cycle_sort alias changed from CY to CyS Co-authored-by: Azanul Haque <42029519+Azanul@users.noreply.github.com> --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 253f356..7d80131 100644 --- a/main.py +++ b/main.py @@ -14,7 +14,7 @@ from cocktail_sort import sort as CS -from cycle_sort import sort as CY +from cycle_sort import sort as CyS from OFA import * From c9417fd24313918d5deb40fcb966eb8194a6b2f3 Mon Sep 17 00:00:00 2001 From: Nizamuddin Mondal <89808462+Nizam420@users.noreply.github.com> Date: Sun, 24 Oct 2021 11:53:03 +0530 Subject: [PATCH 3/4] Cycle_sort alias changed from CY to CyS Co-authored-by: Azanul Haque <42029519+Azanul@users.noreply.github.com> --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 7d80131..66da637 100644 --- a/main.py +++ b/main.py @@ -46,7 +46,7 @@ btn9 = Button(tk, text = 'Cocktail Sort', bd = '5', command = lambda: CS()) -btnnizam = Button(tk, text = 'Cycle Sort', bd = '5', command = lambda: CY()) +btnnizam = Button(tk, text = 'Cycle Sort', bd = '5', command = lambda: CyS()) btn1.place(x = 710, y = 5) From 491eadeb775b31233fccbbbfcb63e5216e8f78ae Mon Sep 17 00:00:00 2001 From: Nizamuddin Mondal <89808462+Nizam420@users.noreply.github.com> Date: Sun, 24 Oct 2021 11:55:10 +0530 Subject: [PATCH 4/4] Range decrease by 1 Co-authored-by: Azanul Haque <42029519+Azanul@users.noreply.github.com> --- cycle_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cycle_sort.py b/cycle_sort.py index 7ba7cae..a0c784b 100644 --- a/cycle_sort.py +++ b/cycle_sort.py @@ -8,7 +8,7 @@ def sort(list = blocks): writes = 0 LS = len(list) - for i in range(LS): + for i in range(LS-1): item = list[i] pos = i for j in range(i + 1, LS):