def CocktailShakerSort(MyArray, Win, Font): for i in range(len(MyArray.Array) - 1, 0, -1): Swapped = False for j in range(i, 0, -1): if MyArray.Array[j] < MyArray.Array[j - 1]: MyArray.Array[j], MyArray.Array[j - 1] = MyArray.Array[j - 1], MyArray.Array[j] MyArray.Moving_Elements = [MyArray.Array[j], MyArray.Array[j - 1]] KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: MyArray.Draw(Win, Font, extraAcceses=2) # Since we are moving two elements on the array, we have to add 2 acceses instead of one. Swapped = True for j in range(i): if MyArray.Array[j] > MyArray.Array[j + 1]: MyArray.Array[j], MyArray.Array[j + 1] = MyArray.Array[j + 1], MyArray.Array[j] MyArray.Moving_Elements = [MyArray.Array[j], MyArray.Array[j + 1]] KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: MyArray.Draw(Win, Font, extraAcceses=2) # Since we are moving two elements on the array, we have to add 2 acceses instead of one. Swapped = True if not Swapped: MyArray.isSorted = True return MyArray
def Merge(MyArray, l, m, r, Win, Font, DrawSort): LEN_1, LEN_2 = m - l + 1, r - m Left, Right = [], [] for i in range(0, LEN_1): Left.append(MyArray.Array[l + i]) for i in range(0, LEN_2): Right.append(MyArray.Array[m + 1 + i]) i, j, k = 0, 0, l while i < LEN_1 and j < LEN_2: if Left[i] <= Right[j]: MyArray.Array[k] = Left[i] MyArray.Moving_Elements = [MyArray.Array[k]] i += 1 KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: MyArray.Draw(Win, Font) else: MyArray.Array[k] = Right[j] MyArray.Moving_Elements = [MyArray.Array[k]] j += 1 KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: if DrawSort: MyArray.Draw(Win, Font) k += 1 while i < LEN_1: MyArray.Array[k] = Left[i] MyArray.Moving_Elements = [MyArray.Array[k]] k += 1 i += 1 KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: if DrawSort: MyArray.Draw(Win, Font) while j < LEN_2: MyArray.Array[k] = Right[j] MyArray.Moving_Elements = [MyArray.Array[k]] k += 1 j += 1 KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: if DrawSort: MyArray.Draw(Win, Font)
def BubbleSort(MyArray, Win, Font, DrawSort=True): Swapped = True while Swapped: Swapped = False for i in range(len(MyArray.Array) - 1): if MyArray.Array[i] > MyArray.Array[i + 1]: MyArray.Array[i], MyArray.Array[i + 1] = MyArray.Array[ i + 1], MyArray.Array[i] MyArray.Moving_Elements = [ MyArray.Array[i], MyArray.Array[i + 1] ] if DrawSort: KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: MyArray.Draw( Win, Font, extraAcceses=2 ) # Since we are moving two elements on the array, we have to add 2 acceses instead of one. Swapped = True if not MyArray.isSorted: MyArray.Draw_Check_Sorted(Win, Font) MyArray.isSorted = True return MyArray
def TimSort(MyArray, Win, Font, DrawSort=True): if not MyArray.isSorted: N = len(MyArray.Array) for i in range(0, N, RUN): Tim_InsertionSort(MyArray, i, min((i + 31), (N - 1)), Win, Font, DrawSort) Size = RUN while Size < N: for Left in range(0, N, 2 * Size): Mid = Left + Size - 1 Right = min((Left + 2 * Size - 1), (N - 1)) KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: if DrawSort: MyArray.Draw(Win, Font) Merge(MyArray, Left, Mid, Right, Win, Font, DrawSort) Size = 2 * Size if not MyArray.isSorted: MyArray.Draw_Check_Sorted(Win, Font) MyArray.isSorted = True return MyArray
def Draw_Check_Sorted(self, Win, Font): Win.fill((0, 0, 0)) # Cleaning everything on the screen. Accesses_Text = Font.render(f"Array Accesses: {self.Current_Accesses}", True, (255, 255, 255)) # Drawing the text with the accesses to the array. Win.blit(Accesses_Text, (10, 10)) Rendered_1 = Font.render(self.Information_Text[0], True, (255, 255, 255)) # Array size. Rendered_2 = Font.render(self.Information_Text[1], True, (255, 255, 255)) # Current algorithm. Win.blit(Rendered_1, (400, 10)) Win.blit(Rendered_2, (720, 10)) self.Normalized_Array = [i / max(self.Array) for i in self.Array] # Creating an array with values between [0, 1]. for i in range(self.Size): Current_Element = self.Normalized_Array[i] # Getting the value of the element. Rectangle = pygame.Rect(self.x + i*self.Width, self.y, self.Width, -self.Height*Current_Element) # Drawing the rectangle, x position increases at every iteration and Height depends on the value of the element. # Height is negative to draw in up direction. Element_Colour = (48, 235, 92) # Getting the colour value depending on our value, here we don't use the normalized value, since we want to acces to a position of an array and we can't do Array[0.7]. pygame.draw.rect(Win, Element_Colour, Rectangle) # Drawing on the surface, with the colour, and the generated rectangle. if self.Sound: # If Sound is enabled, we make the corresponding sound. # BeepSound.Beep(Sound_Frequency_Range[self.Array[i]*self.Jumps%len(Sound_Frequency_Range)], Sound_Duration) Generator.play(Sound_Frequency_Range[self.Array[i]*self.Audio_Jumps%len(Sound_Frequency_Range)], Sound_Duration, Amplitude) while Generator.is_playing(): None KEY = KEY_PRESSED() if KEY == "QUIT": break pygame.quit() sys.exit() else: pygame.display.update() # Updating screen.
def CycleSort(MyArray, Win, Font): if not MyArray.isSorted: Writes = 0 for cycleStart in range(0, len(MyArray) - 1): Item = MyArray.Array[cycleStart] Position = cycleStart for i in range(cycleStart + 1, len(MyArray)): if MyArray.Array[i] < Item: Position += 1 if Position == cycleStart: continue while Item == MyArray.Array[Position]: Position += 1 MyArray.Array[Position], Item = Item, MyArray.Array[Position] KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: MyArray.Draw(Win, Font, extraAcceses=2) Writes += 1 while Position != cycleStart: Position = cycleStart for i in range(cycleStart + 1, len(MyArray)): if MyArray.Array[i] < Item: Position += 1 while Item == MyArray.Array[Position]: Position += 1 MyArray.Array[Position], Item = Item, MyArray.Array[Position] KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: MyArray.Draw(Win, Font, extraAcceses=2) Writes += 1 if not MyArray.isSorted: MyArray.Draw_Check_Sorted(Win, Font) MyArray.isSorted = True return MyArray
def Tim_InsertionSort(MyArray, Left, Right, Win, Font): for i in range(Left + 1, Right + 1): Temp = MyArray.Array[i] j = i - 1 while MyArray.Array[j] > Temp and j >= Left: MyArray.Array[j + 1] = MyArray.Array[j] j -= 1 KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: MyArray.Draw(Win, Font) MyArray.Array[j + 1] = Temp KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: MyArray.Draw(Win, Font)
def Rotate(MyArray, rotationTimes, Win, Font): if not MyArray.isSorted: for _ in range(rotationTimes): MyArray.Array = rotLeft(MyArray.Array) KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: MyArray.Draw(Win, Font) MyArray.Draw_Check_Sorted(Win, Font) MyArray.isSorted = True return MyArray
def ElementsIncreaser(MyArray, increaseSize, Win, Font): if not MyArray.isSorted: for _ in range(increaseSize): for arrayPosition in range(len(MyArray)): MyArray.Array[arrayPosition] += 1 KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: MyArray.Draw(Win, Font) MyArray.Draw_Check_Sorted(Win, Font) MyArray.isSorted = True return MyArray
def InsertionSort(MyArray, Win, Font, DrawSort=True): for arrayPosition in range(1, len(MyArray.Array)): actualElement = MyArray.Array[arrayPosition] # Elemento que se compara. while arrayPosition > 0 and MyArray.Array[arrayPosition - 1] > actualElement: MyArray.Array[arrayPosition] = MyArray.Array[arrayPosition - 1] arrayPosition -= 1 MyArray.Array[arrayPosition] = actualElement if DrawSort: MyArray.Moving_Elements = [MyArray.Array[arrayPosition]] KEY = KEY_PRESSED() if KEY == "QUIT": pygame.quit() sys.exit() else: MyArray.Draw(Win, Font) return MyArray
return False if AL == 4: if not isIntegrer(log(ARRAY_SIZE, 2)): raise ArraySizeError( "When using Tim Sort, array size should be power of two. (64, 128, 256, 512)" ) MAIN_LOOP = False MyArray = Visualized_Array(ARRAY_SIZE, 5, WIN_SIZE[1], 1900 / ARRAY_SIZE, WIN_SIZE[1] - 20, CompleteArray=True) while MAIN_LOOP: KEY = KEY_PRESSED() if KEY == "QUIT": MAIN_LOOP = False pygame.quit() sys.exit() else: if AL == 1: InsertionSort(MyArray, WIN, FONT).Draw(WIN, FONT, True) elif AL == 2: CocktailShakerSort(MyArray, WIN, FONT).Draw(WIN, FONT, True) elif AL == 3: BubbleSort(MyArray, WIN, FONT).Draw(WIN, FONT, True) elif AL == 4: TimSort(MyArray, WIN, FONT).Draw(WIN, FONT, True)