Esempio n. 1
0
    def test_swap_array_size_2(self):
        arr = [1, 2]

        swap(arr, 0, 1)

        assert arr[0] == 2
        assert arr[1] == 1
Esempio n. 2
0
    def test_reswap(self):
        arr = [1, 2]

        swap(arr, 0, 1)
        swap(arr, 0, 1)

        assert arr[0] == 1
        assert arr[1] == 2
Esempio n. 3
0
def selectionsort(arr):
    '''
    Partition array into an unsorted and sorted portion.
    Find smallest value in unsorted portion and place at end of sorted.

    Time complexity:    O(n^2), for all cases
                        O(n^2)
                        O(n^2)
    Space complexity:   O(1)

    Use case:           n<100 since it's slow but simple.
                        Probably would use insertionsort instead though.
    '''
    n = len(arr)

    for i in range(n):
        # Find index of minimum
        minidx = i
        for j in range(i + 1, n):
            if arr[j] < arr[minidx]:
                minidx = j

        arr = swap(arr, minidx, i)

    return arr
Esempio n. 4
0
    def bubble_up(self, k):

        if k == 0:
            return

        if self.arr[self.parent(k)] > self.arr[k]:
            self.arr = swap(self.arr, self.parent(k), k)
            self.bubble_up(self.parent(k))
Esempio n. 5
0
def insertionsort(arr):
    '''
    Partition array into an unsorted and sorted portion.
    Take first value in unsorted portion,and move it to lower indices
    until it is smaller than the value above it.

    Time complexity:    O(n^2), array is reversed
                        O(n^2),
                        O(n)  , array is in order
    Space complexity:   O(1)

    Use case:           n<100 since it's slow but simple
    '''
    n = len(arr)

    for i in range(1, n):
        j = i

        while j > 0 and arr[j] < arr[j - 1]:
            swap(arr, j, j - 1)
            j -= 1

    return arr
Esempio n. 6
0
    def bubble_down(self, k):
        c = 2 * k  # Child index
        min_idx = k

        for i in range(2):
            if (c + i) <= self.get_keys() - 1:  # Check still in heap
                if self.arr[min_idx] > self.arr[
                        c + i]:  # if current value larger than child
                    min_idx = c + i

        # If found one smaller
        if min_idx != k:
            self.arr = swap(self.arr, k, min_idx)  # Swap them
            self.bubble_down(
                min_idx)  # Then bubble_down same value at new index
Esempio n. 7
0
def bubblesort(arr):
    '''
    Time complexity:    O(n^2) Backwards, so every key is compared to every other key.
                        O(n^2) On average, 50/50 chance of two elements being out of order
                        O(n), already sorted
    Space complexity:   O(1), only reading two values at a time
    '''
    n = len(arr)
    for i in range(n):
        swapped = False

        for j in range(0, n - i - 1):

            if arr[j] > arr[j + 1]:
                arr = swap(arr, j, j + 1)
                swapped = True

        # Stop sorting if nothing was swapped
        if swapped == False:
            break

    return arr