We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
MERGE SORT IS still the fastest algorithm since o(n) complexity
defmerge(left,right):merged=[]i=j=0whilei<len(left)andj<len(right):ifcompare_numbers(left[i],right[j])<=0:merged.append(left[i])i+=1else:merged.append(right[j])j+=1merged.extend(left[i:])merged.extend(right[j:])returnmergeddefcompare_numbers(a,b):# First, compare by length.iflen(a)!=len(b):returnlen(a)-len(b)ifa<b:return-1elifa>b:return1else:return0defmerge_sort(arr):iflen(arr)<=1:returnarrmid=len(arr)// 2left=merge_sort(arr[:mid])right=merge_sort(arr[mid:])returnmerge(left,right)
this is insertion sort (0)^2
`
def compare(res, current):
if len(res) == 0:
res = [current]
return res
for idx, pip in enumerate(res):
if pip >= current:
rep = idx
if rep<0:
rep = 0
res.insert(rep, current)
return res
res.append(current)
print("final", res)
return res
def bigSorting(unsorted):
# Write your code here
res =[]
for datas in unsorted:
data = int(datas)
res = compare(res,data)
fix = []
for bub in res:
fix.append(str(bub))
print(bub)
return fix
`
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Big Sorting
You are viewing a single comment's thread. Return to all comments →
MERGE SORT IS still the fastest algorithm since o(n) complexity
this is insertion sort (0)^2 ` def compare(res, current): if len(res) == 0: res = [current] return res for idx, pip in enumerate(res): if pip >= current: rep = idx if rep<0: rep = 0 res.insert(rep, current) return res res.append(current) print("final", res) return res
def bigSorting(unsorted): # Write your code here res =[] for datas in unsorted: data = int(datas) res = compare(res,data) fix = [] for bub in res: fix.append(str(bub)) print(bub) return fix `