I made an inefficient sorting algorithm

Поделиться
HTML-код
  • Опубликовано: 7 мар 2023
  • youtube doesn't allow angled brackts so the were replaced with the words less than or greater than also this is python code its noame is hydra sort and its suc also the code actually broken and i dont care to fix it so thats that
    import random
    import time
    class Head:
    def __init__(self):
    self.UpperHead=None
    self.parent=None#unused #it is now used
    self.LowerHead=None
    self.number=None
    self.inserted=False
    self.count=0
    start = time.time()
    array = []
    Hydra = []
    for i in range(1,100001):
    array.append(random.randint(1,100))
    Hydra.append(Head())
    average=sum(array)/len(array)
    for i in range(len(array)):
    index=round((array[i]*len(array)/(average*2))-1)
    if (index greater than or equal to len(array)):
    index=len(array)-1
    elif (index less than 0):
    index=0
    if ((Hydra[index]).number==None):#array slot is open simple slop it in
    (Hydra[index]).number=array[i]
    (Hydra[index]).count+=1
    #(Hydra[index]).inserted=True
    elif ((Hydra[index]).number==array[i]):#they are the same number and we can double up horay
    (Hydra[index]).count+=1
    else:#uh oh array already has somone living there continue loop till you find a new home gamer
    WhichHead=Hydra[index]
    while(True):
    if(array[i] less than WhichHead.number):#go down
    if(WhichHead.LowerHead==None):#there is an empty spot on lower head
    WhichHead.LowerHead=Head()
    WhichHead.LowerHead.parent=WhichHead#.LowerHead#maybe???? probably doesnt work but we'll see
    WhichHead.LowerHead.number=array[i]#count
    WhichHead.LowerHead.count+=1
    break
    elif(WhichHead.number==array[i]):#lower head is filled but values are the same
    WhichHead.count+=1
    break
    else:#lower head is filled move which pointer to lower head
    WhichHead=WhichHead.LowerHead
    else:#array[i] greater than WhichHead.number #go up
    if(WhichHead.UpperHead==None):#there is an empty spot on upper head
    WhichHead.UpperHead=Head()
    WhichHead.UpperHead.parent=WhichHead#.UpperHead#maybe???? probably doesnt work but we'll see
    WhichHead.UpperHead.number=array[i]#count
    WhichHead.UpperHead.count+=1
    break
    elif(WhichHead.number==array[i]):#upper head is filled but values are the same
    WhichHead.count+=1
    break
    else:#upper head is filled move which pointer to lower head
    WhichHead=WhichHead.UpperHead
    '''
    print("array")
    for i in range(len(array)):
    print(array[i])
    print("")
    print("Hydra")
    for i in range(len(Hydra)):
    print(Hydra[i].number)
    print("")
    '''
    ArrayIndex=0
    HydraIndex=0
    didInsert=False
    while(ArrayIndex less than len(array)):
    CurrentHead=Hydra[HydraIndex]
    if(CurrentHead.number==None):
    #ArrayIndex+=1
    HydraIndex+=1
    else:
    while(True):#while we are not at head and head has been checked but while loop will always run once
    while(CurrentHead.LowerHead!=None):#go to lowest head
    if(CurrentHead.LowerHead.inserted):# if the node below has already been inserted you are lowest if statement is seperated to avoid accessing None
    break
    CurrentHead=CurrentHead.LowerHead
    if(CurrentHead.inserted==False): # if current head hasn't been inserted
    for i in range(CurrentHead.count):#insert into array count times
    #print("butthole")
    array[ArrayIndex]=CurrentHead.number
    ArrayIndex+=1 #move array over to next open slot
    CurrentHead.inserted=True #mark as inserted
    didInsert=True
    if(CurrentHead.parent!=None):
    if(CurrentHead.UpperHead!=None):
    if(CurrentHead.UpperHead.inserted==False):
    CurrentHead=CurrentHead.UpperHead
    else:
    CurrentHead=CurrentHead.parent
    else:
    CurrentHead=CurrentHead.parent
    if(CurrentHead.parent==None and CurrentHead.inserted==True):
    break
    if(didInsert):
    HydraIndex+=1
    didInsert=False
    end = time.time()
    print(end - start)
    #for i in range(len(array)):
    print(array[i])
    #print("")

Комментарии •