Created
October 24, 2020 10:44
-
-
Save Alfex4936/af6a90d0a95c3120bb9292fe33d66558 to your computer and use it in GitHub Desktop.
Bubble Sort using Cython
This file contains hidden or 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
def bubbleSort(list array): | |
return bubbleSort_c(array) | |
cdef bubbleSort_c(list array): | |
cdef bint isSorted = False | |
cdef int count = 0 | |
cdef Py_ssize_t i | |
cdef Py_ssize_t n = len(array) - 1 | |
while not isSorted: | |
isSorted = True | |
for i from 0 <= i < n - count: | |
if array[i] > array[i + 1]: | |
array[i], array[i + 1] = array[i + 1], array[i] | |
isSorted = False | |
count += 1 | |
return array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment