forked from Winner-exe/GroupProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.py
More file actions
41 lines (41 loc) · 2.57 KB
/
sort.py
File metadata and controls
41 lines (41 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def sort(artist_list):
"""Sorts a list of strings alphabetitcally using merge sort"""
if len(artist_list) > 1: # If the length of the list is greater than 1 run this
middleIndex = len(artist_list) // 2 # Middle Index is halfway point of the list using int division
leftList = artist_list[:middleIndex]
rightList = artist_list[middleIndex:] # left and right list created by splitting list by that middle index
sort(leftList)
sort(
rightList) # recursive call of the left and right list to further break down the list till it gets to
# length 1 where it will no longer run the if statement
indexMain = 0
indexLeft = 0
indexRight = 0 # creation of indexes for each list
while indexLeft < len(leftList) and indexRight < len(
rightList): # Runs through the left and right list at the same time while comparing them until one
# of the lists reaches the end
leftString = leftList[indexLeft].lower()
leftString = leftString.replace(" ", "")
rightString = rightList[indexRight].lower()
rightString = rightString.replace(" ",
"") # Right and Left string created by standarizing each string to all
# lowercase and no spaces
if leftString <= rightString:
artist_list[indexMain] = leftList[indexLeft]
indexLeft += 1 # If the leftString is alphabetically before rightString i.e. <= it will set the main
# string at indexMain to the leftList at indexLeft
else:
artist_list[indexMain] = rightList[indexRight]
indexRight += 1 # If the leftString is alphabetically after rightString i.e. <= it will set the main
# string at indexMain to the rightList at indexRight
indexMain += 1 # Index of main increased by 1 to go to next element
while indexLeft < len(leftList):
artist_list[indexMain] = leftList[indexLeft]
indexLeft += 1
indexMain += 1 # All remaining parts of the left list if left is greater than right at that time,
# run through here to set remaining values of index main to leftList
while indexRight < len(rightList):
artist_list[indexMain] = rightList[indexRight]
indexRight += 1
indexMain += 1 # All remaining parts of the right list if right is greater than left at that time,
# run through here to set remaining values of index main to rightList