Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Go ahead and add a one line intro about you and add your favorite emoji (you can

- Hi, my name is Brandon and my favorite emoji is 🍔

- Hi!, I'm Haris and my favorite emoji is 👨‍💻

## Conclusion
Thank you for the overwhelming amount of contributions! I hope that everybody made their 4 pull requests for Hacktoberfest, and that your journey to open source doesn't end here. I am *slowly* getting through the pull requests. Check back if you don't see your changes in this repo! Best of luck :)

Expand Down
19 changes: 19 additions & 0 deletions sorting/counting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

"""Python implementation of in-place counting sort algorithm"""

def countingsort(array, maxval):
n = len(array)
m = maxval + 1
# init with zeros
count = [0] * m
for a in array:
# count occurences
count[a] += 1
i = 0
for a in range(m):
# make 'count[a]' copies of 'a'
for c in range(count[a]):
array[i] = a
i += 1
return array

81 changes: 81 additions & 0 deletions sorting/tim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@


def binary_search(the_array, item, start, end):
if start == end:
if the_array[start] > item:
return start
else:
return start + 1
if start > end:
return start

mid = round((start + end)/ 2)

if the_array[mid] < item:
return binary_search(the_array, item, mid + 1, end)

elif the_array[mid] > item:
return binary_search(the_array, item, start, mid - 1)

else:
return mid

# If array or size of run is small, use Insertion sort

def insertion_sort(the_array):
l = len(the_array)
for index in range(1, l):
value = the_array[index]
pos = binary_search(the_array, value, 0, index - 1)
the_array = the_array[:pos] + [value] + the_array[pos:index] + the_array[index+1:]

return the_array

# use merge function of mergesort

def merge(left, right):

if not left:
return right
if not right:
return left
if left[0] < right[0]:
return [left[0]] + merge(left[1:], right)
return [right[0]] + merge(left, right[1:])

def timsort(the_array):
runs, sorted_runs = [], []
length = len(the_array)
new_run = [the_array[0]]

# for every i in the range of 1 to length of array
for i in range(1, length):
# if i is at the end of the list
if i == length - 1:
new_run.append(the_array[i])
runs.append(new_run)
break
# if the i'th element of the array is less than the one before it
if the_array[i] < the_array[i-1]:
# if new_run is set to None (NULL)
if not new_run:
runs.append([the_array[i]])
new_run.append(the_array[i])
else:
runs.append(new_run)
new_run = [the_array[i]]
# else if its equal to or more than
else:
new_run.append(the_array[i])

# for every item in runs, append it using insertion sort
for item in runs:
sorted_runs.append(insertion_sort(item))

# for every run in sorted_runs, merge them
sorted_array = []
for run in sorted_runs:
sorted_array = merge(sorted_array, run)

return sorted_array

18 changes: 18 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,21 @@
print "Bucket Sort incorrect"
except:
print "Bucketsort function errored or is incomplete"

try:
from counting import countingsort
if(countingsort(list(nums), numpy.max(nums)) == sortedNums):
print "Counting Sort success!"
else:
print "Counting Sort incorrect."
except:
print "Countingsort function errored or is incomplete."

try:
from tim import timsort
if(timsort(list(nums)) == sortedNums):
print "Timsort success!"
else:
print "Timsort incorrect."
except:
print "Timsort function errored or is incomplete."