Esempio n. 1
0
    for i in range(index_b, len(array_b)):
        merged_array.append(array_b[i])

    return merged_array

def sort(array):

    if len(array) <= 1:
        return array

    if len(array) == 2:
        if array[0] > array[1]:
            swap(array, 0, 1)
        return array

    half = len(array) // 2

    array_a = sort(array[:half])
    array_b = sort(array[half:])
    return merge(array_a, array_b)



ARRAY_SIZE = 10000
START = -100
END = 20000

array_before = create_random_array(ARRAY_SIZE, START, END)
array_after = sort(array_before)
show(array_before, array_after, 'Merge Sort')
Esempio n. 2
0
1.  Move an element E to the left until finding an smaller element K than E.
2.  We assume that the elements from  the left are already ordered.
'''

from array_generator import create_random_array, swap, show


def insertion_sort(array):

    array_size = len(array)

    for i in range(1, array_size):
        value_to_move = array[i]
        j = i
        while j > 0:
            if array[j - 1] <= value_to_move:
                break
            array[j] = array[j - 1]
            j -= 1
        array[j] = value_to_move


ARRAY_SIZE = 1000
START = -100
END = 20000

array_before = create_random_array(ARRAY_SIZE, START, END)
array_after = array_before[:]
insertion_sort(array_after)
show(array_before, array_after, 'Insertion Sort')
Esempio n. 3
0
    one in the first position.
2.  Find the second smallest element and swap it for the one in the second
    position.
3.  Repeat this for each element of the array.
'''

from array_generator import create_random_array, swap, show


def selection_sort(array):

    array_size = len(array)

    for i in range(array_size):
        min_index = i
        for j in range(i + 1, array_size):
            if array[j] < array[min_index]:
                min_index = j
        if min_index != i:
            swap(array, i, min_index)


ARRAY_SIZE = 100
START = -100
END = 20000

array_before = create_random_array(ARRAY_SIZE, START, END)
array_after = array_before[:]
selection_sort(array_after)
show(array_before, array_after, 'Selection Sort')
Esempio n. 4
0
    return p1


def quick_sort(array, left_index, right_index):

    if right_index - left_index < 1:
        return

    if right_index - left_index == 1:
        if array[right_index] < array[left_index]:
            swap(array, right_index, left_index)
        #print(array[left_index:right_index+1])
        return

    p = partition(array, left_index, right_index)
    #print('pivot=%d %s' % (p, str(array[left_index:right_index+1])))

    quick_sort(array, left_index, p - 1)
    quick_sort(array, p + 1, right_index)


ARRAY_SIZE = 2000
START = -1000
END = 20000

array_before = create_random_array(ARRAY_SIZE, START, END)
array_after = array_before[:]
quick_sort(array_after, 0, ARRAY_SIZE - 1)
show(array_before, array_after, 'Quick Sort')
    teration.
*   If in one iteration we don't swap any element, it means that the list
    already ordered.
'''

from array_generator import create_random_array, swap, show


def bubble_sort(array):

    array_size = len(array)

    for i in range(array_size):
        moved = False
        for j in range(1, array_size - i):
            if array[j] < array[j - 1]:
                swap(array, j - 1, j)
                moved = True
        if not moved:
            break


ARRAY_SIZE = 1000
START = -100
END = 20000

array_before = create_random_array(ARRAY_SIZE, START, END)
array_after = array_before[:]
bubble_sort(array_after)
show(array_before, array_after, 'Bubble Sort')