I'm trying to utilize mergesort + divide & conquer by taking a user's input of random words. I'm taking the users input and dividing the words given into two arrays:
import math
inputted_sentence = input("Enter your sentence here: \n")
separated_inputs = [word.lower() for word in inputted_sentence.split()]
inputs_length = int(len(separated_inputs))
#separate the arrays
array_one = (separated_inputs[0:math.floor(int(inputs_length/2))])
array_two = (separated_inputs[math.floor(int(inputs_length/2)):inputs_length])
#grab length of the array
length_array_one = len(array_one)
length_array_two = len(array_two)
after that, I'm sorting them (alphabetical order)
#first array being sorted and stored
for a in range(length_array_one-1):
for b in range(length_array_one-a-1):
if array_one[b] > array_one[b+1]:
array_one[b], array_one[b+1] = array_one[b+1], array_one[b]
sorted_array_one = []
for words in array_one:
sorted_array_one.append(words)
#second array being sorted and stored
for a in range(length_array_two-1):
for b in range(length_array_two-a-1):
if array_two[b] > array_two[b+1]:
array_two[b], array_two[b+1] = array_two[b+1], array_two[b]
sorted_array_two = []
for words in array_two:
sorted_array_two.append(words)
This image shows the two arrays: https://i.stack.imgur.com/9ZrHb.png
Now I need to compare blue to aaple, see it's less, compare blue to apple, see its less, blue to cat, see it is greater and it takes index[2] in the final array.
after that, rabbit compares with cat, its less, dog, it's less, takes the array spot after dog.
Edit: my version one (below) does this but this doesn't utilize the sorted arrays as it just sorts the words all over again.
unsorted_final = sorted_array_one + sorted_array_two
length_unsorted_final = len(unsorted_final)
sorted_array_final = []
#Final array sorted and stored
for a in range(length_unsorted_final-1):
for b in range(length_unsorted_final-a-1):
if unsorted_final[b] > unsorted_final[b+1]:
unsorted_final[b], unsorted_final[b+1] = unsorted_final[b+1], unsorted_final[b]
for words in unsorted_final:
sorted_array_final.append(words)
print(sorted_array_final)