Skip to content

Commit

Permalink
Merge pull request #93 from sid-am-ahd935/dev
Browse files Browse the repository at this point in the history
Created Bubble Sort Using Cython
  • Loading branch information
sid-am-ahd935 authored Oct 28, 2022
2 parents 7462f71 + 5f12c6b commit 87d7e83
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cython/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.c
*.so

venv/
Empty file.
23 changes: 23 additions & 0 deletions Cython/Bubble Sort/cython_sorting/cython_bubble_sort.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# cython: language_level=3

from array import array
from cpython cimport array


cpdef int[:] c_sort(int[:] arr, int n):
cdef int i, j

for i in range(n):
for j in range(n - i - 1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]

return arr


cpdef list sort(list arr):
cdef int[:] c_arr = array("i", arr)
cdef int[:] s_arr

s_arr = c_sort(c_arr, len(arr))
return list(s_arr)
38 changes: 38 additions & 0 deletions Cython/Bubble Sort/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import time
import random


def timeit(func= None, *args, **kwargs):
a = time.time()
returned_items = func(*args, **kwargs)
b = time.time()
return (returned_items, b - a)

generate_values = lambda n : [random.randint(-n, n) for i in range(n)]

# __________________________________________________________ #
#### ________ Change Only From Below Section ________ ####

def main():
from cython_sorting import cython_bubble_sort
from python_sorting import python_bubble_sort

values = generate_values(n= 10000)
array1, t1 = timeit(
cython_bubble_sort.sort,
values.copy()
)
array2, t2 = timeit(
python_bubble_sort.sort,
values.copy()
)

print(f"Cython takes: {t1:0.3f} seconds")
print(f"Python takes: {t2:0.3f} seconds")
print(f"Cython is {t2/t1:0.2f}x faster than Python!")
print(f"Same returned value: {array1 == array2}")
# print(array2)


if __name__ == "__main__":
main()
Empty file.
10 changes: 10 additions & 0 deletions Cython/Bubble Sort/python_sorting/python_bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

def sort(arr):
n = len(arr)

for i in range(n):
for j in range(n - i - 1):
if(arr[j] > arr[j+1]):
arr[j], arr[j+1] = arr[j+1], arr[j]

return arr
7 changes: 7 additions & 0 deletions Cython/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Contribution Guidelines for this folder

Create new issues if not available and fill this folder with code snippets written in Python. No spamming is allowed, the code must be genuinely solving a problem or might contain usefuls insights for this language.

- Create a new folder with your code snippet and add a README.md to this new folder.
- Add possible explanations along with screenshots and inner working of this snippets
- The snippet should be more than 50 lines of code.

0 comments on commit 87d7e83

Please sign in to comment.