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
35 changes: 35 additions & 0 deletions Sorting/Iterative_Merge_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def merge(left, right):
if not len(left) or not len(right):
return left or right

result = []
i, j = 0, 0
while (len(result) < len(left) + len(right)):
if left[i] < right[j]:
result.append(left[i])
i+= 1
else:
result.append(right[j])
j+= 1
if i == len(left) or j == len(right):
result.extend(left[i:] or right[j:])
break

return result

def mergesort(list):
if len(list) < 2:
return list

middle = len(list)/2
left = mergesort(list[:middle])
right = mergesort(list[middle:])

return merge(left, right)

seq = [12, 11, 13, 5, 6, 7]
print("Given array is")
print(seq);
print("\n")
print("Sorted array is")
print(mergesort(seq))
23 changes: 23 additions & 0 deletions Sorting/Selection_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Python program for implementation of Selection
# Sort
import sys
A = [64, 25, 12, 22, 11]

# Traverse through all array elements
for i in range(len(A)):

# Find the minimum element in remaining
# unsorted array
min_idx = i
for j in range(i+1, len(A)):
if A[min_idx] > A[j]:
min_idx = j

# Swap the found minimum element with
# the first element
A[i], A[min_idx] = A[min_idx], A[i]

# Driver code to test above
print ("Sorted array")
for i in range(len(A)):
print("%d" %A[i]),
64 changes: 64 additions & 0 deletions Sorting/radix_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Python program for implementation of Radix Sort
# A function to do counting sort of arr[] according to
# the digit represented by exp.

def countingSort(arr, exp1):

n = len(arr)

# The output array elements that will have sorted arr
output = [0] * (n)

# initialize count array as 0
count = [0] * (10)

# Store count of occurrences in count[]
for i in range(0, n):
index = (arr[i] / exp1)
count[int(index % 10)] += 1

# Change count[i] so that count[i] now contains actual
# position of this digit in output array
for i in range(1, 10):
count[i] += count[i - 1]

# Build the output array
i = n - 1
while i >= 0:
index = (arr[i] / exp1)
output[count[int(index % 10)] - 1] = arr[i]
count[int(index % 10)] -= 1
i -= 1

# Copying the output array to arr[],
# so that arr now contains sorted numbers
i = 0
for i in range(0, len(arr)):
arr[i] = output[i]

# Method to do Radix Sort
def radixSort(arr):

# Find the maximum number to know number of digits
max1 = max(arr)

# Do counting sort for every digit. Note that instead
# of passing digit number, exp is passed. exp is 10^i
# where i is current digit number
exp = 1
while max1 / exp > 0:
countingSort(arr, exp)
exp *= 10


# Driver code
arr = [170, 45, 75, 90, 802, 24, 2, 66]

# Function Call
radixSort(arr)

for i in range(len(arr)):
print(arr[i])

# This code is contributed by Mohit Kumra
# Edited by Patrick Gallagher