-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from sid-am-ahd935/dev
Created Bubble Sort Using Cython
- Loading branch information
Showing
7 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.c | ||
*.so | ||
|
||
venv/ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |