def bubble_sort(app, array):
    print("Starting {Bubble sort} ...")
    while not is_sorted(array):
        for i in range(len(array) - 1):
            if array[i] > array[i + 1]:
                swap_elements(app, array, i, i + 1)
    print("End of {Bubble sort}.")
Exemple #2
0
def selection_sort(app, array):
    print("Starting {Selection sort} ...")
    for i in range(len(array) - 1):
        index_of_min = i
        for j in range(i, len(array)):
            if array[j] < array[index_of_min]:
                index_of_min = j
        if index_of_min != i:
            swap_elements(app, array, index_of_min, i)
    print("End of {Selection sort}.")
def gnome_sort(app, array):
    print("Starting {Gnome sort} ...")
    position = 0
    while position < len(array) - 1:
        if (position == -1 or array[position] <= array[position + 1]):
            position += 1
        else:
            swap_elements(app, array, position, position + 1)
            position -= 1
    print("End of {Gnome sort}.")
Exemple #4
0
def cocktail_shaker_sort(app, array):
    swapped = True
    while swapped:
        swapped = False
        for i in range(len(array) - 1):
            if array[i] > array[i + 1]:
                swapped = True
                swap_elements(app, array, i, i + 1)
        if not swapped:
            return  # if no swapp was done then the array is sorted.
        for i in range(len(array) - 1, 0, -1):
            if array[i] < array[i - 1]:
                swap_elements(app, array, i, i - 1)
                swapped = True
def partitionArray(app, array, low, high):
    pivot = array[low]
    indexOfLowers = low
    swap_elements(app, array, low, high)
    for i in range(low, high):
        if array[i] < pivot:
            swap_elements(app, array, i, indexOfLowers)
            indexOfLowers += 1

    swap_elements(app, array, high, indexOfLowers)
    return indexOfLowers
def shiftSortedElements(app, array, i_first_element_superior, index_to_insert):
    while (i_first_element_superior < index_to_insert):
        i_first_element_superior += 1
        swap_elements(app, array, i_first_element_superior, index_to_insert)
def insert_element(app, array, index_to_insert):
    i_first_element_superior = findFirstSuperior(array, index_to_insert)
    if i_first_element_superior != index_to_insert:
        swap_elements(app, array, index_to_insert, i_first_element_superior)
        shiftSortedElements(app, array, i_first_element_superior,
                            index_to_insert)