Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add Tim Sort implementation #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2020
Merged
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
90 changes: 90 additions & 0 deletions 90 algorithms/sorting/tim_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from random import randint


class TimSort:
""" A class to demonstrate Tim Sort """

def __init__(self, array):
self.array = array
self.arrayLength = len(array)
self.__RUN = 32

def insertionSort(self, arr):
""" Sorts the given array from given starting index to ending index """

for i in range(1, len(arr)):
currentElement = arr[i]
j = i - 1
while j >= 0 and arr[j] > currentElement:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = currentElement

return arr

def mergeRuns(self, arr1, arr2):
""" Merges the given two arrays: arr1 and arr2 """

newArray = list()
lengthOfArr1 = len(arr1)
lengthOfArr2 = len(arr2)

# The variable i is used to keep track of the indices of the first array
# The variable j is used to keep track of the indices of the second array
# The variable k is used to keep track of the indices of the newArray array which is to be returned
i, j, k = 0, 0, 0

while i < lengthOfArr1 and j < lengthOfArr2:
if arr1[i] <= arr2[j]:
newArray[k] = arr1[i]
k += 1
i += 1
elif arr1[i] >= arr2[j]:
newArray[k] = arr2[j]
k += 1
j += 1

# The below two loops will append any remaining elements left in any of the two arrays.
while i < lengthOfArr1:
newArray.append(arr1[i])
i += 1

while j < lengthOfArr2:
newArray.append(arr2[j])
j += 1

return newArray

def changeRun(self, newRun):
self.__RUN = newRun

def algorithm(self):
""" This function will perfom Tim Sort on the given array """

# Breaking the array into chunks of subarray(RUNS) of size RUN and perfomring insertionSort on them.
for i in range(0, self.arrayLength, self.__RUN):
currentRunElements = self.array[i: i + self.__RUN]

self.array[i: i +
self.__RUN] = self.insertionSort(currentRunElements)

temp_runner = self.__RUN
while temp_runner < self.arrayLength:
for idx in range(0, self.arrayLength, temp_runner * 2):
firstArray = self.array[idx: idx + temp_runner]
secondArray = self.array[idx +
temp_runner: idx + temp_runner * 2]
self.array[idx: idx + temp_runner *
2] = self.mergeRuns(firstArray, secondArray)
temp_runner = self.__RUN * 2

print(f"The sorted array is : {self.array}")

def __repr__(self):
return f"Array: {self.array}\nRUN: {self.__RUN}"


myArray = [randint(1, 100) for i in range(15)]
demo = TimSort(myArray)
print(demo)
demo.algorithm()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.