Esempio n. 1
0
    # initializing the indices
    leftIndex, rightIndex = 0, 0

    # the indices are used as loop invariants and the loop runs until the end
    # of one of the arrays is reached
    while leftIndex < len(left) and rightIndex < len(right):
        # if we reverse this comparison the direction of the sort is reversed
        if left[leftIndex] <= right[rightIndex]:
            result.append(left[leftIndex])
            leftIndex = leftIndex + 1
        else:
            result.append(right[rightIndex])
            rightIndex = rightIndex + 1

    # at the end of the loop if either of the two arrays is left, it implies
    # that all the elements of the array that is left is larger than the
    # elements currently present in the result array.
    if left:
        result.extend(left[leftIndex:])
    if right:
        result.extend(right[rightIndex:])

    return result

# usage
if __name__ == '__main__':
    x = createRandArray(10,1000)
    print(x)
    print(mergeSort(x))
Esempio n. 2
0
    # initializing the indices
    leftIndex, rightIndex = 0, 0

    # the indices are used as loop invariants and the loop runs until the end
    # of one of the arrays is reached
    while leftIndex < len(left) and rightIndex < len(right):
        # if we reverse this comparison the direction of the sort is reversed
        if left[leftIndex] <= right[rightIndex]:
            result.append(left[leftIndex])
            leftIndex = leftIndex + 1
        else:
            result.append(right[rightIndex])
            rightIndex = rightIndex + 1

    # at the end of the loop if either of the two arrays is left, it implies
    # that all the elements of the array that is left is larger than the
    # elements currently present in the result array.
    if left:
        result.extend(left[leftIndex:])
    if right:
        result.extend(right[rightIndex:])

    return result


# usage
if __name__ == '__main__':
    x = createRandArray(10, 1000)
    print(x)
    print(mergeSort(x))
Esempio n. 3
0
It iteratively inserts the key into the proper position in the sorted sub-array
and increments the key value on each iteration
"""


def insertionSort(_list):
    # initializing j to the second item of the list
    j = 1
    while j < len(_list):
        # initializing the key
        key = _list[j]
        # setting the loop invariant for the inner loop which compares and
        # increments the position of each item of the sub array
        i = j - 1
        # moving each item of the sub array until the proper position of the
        # key is found
        while i >= 0 and _list[i] > key:
            _list[i + 1] = _list[i]
            i = i - 1
        # inserting the key into it's proper position
        _list[i + 1] = key
        j = j + 1
    return _list


if __name__ == '__main__':
    # Using the random array function to create a random array to test
    # insertion sort
    print(insertionSort(createRandArray(10, 100)))
Esempio n. 4
0
left of the key is already sorted.

It iteratively inserts the key into the proper position in the sorted sub-array
and increments the key value on each iteration
"""
def insertionSort(_list):
    # initializing j to the second item of the list
    j = 1
    while j < len(_list):
        # initializing the key
        key = _list[j]
        # setting the loop invariant for the inner loop which compares and
        # increments the position of each item of the sub array
        i = j - 1
        # moving each item of the sub array until the proper position of the
        # key is found
        while i >=0 and _list[i] > key:
            _list[i+1] = _list[i]
            i = i - 1
        # inserting the key into it's proper position
        _list[i + 1] = key
        j = j + 1
    return _list

if __name__ == '__main__':
    # Using the random array function to create a random array to test
    # insertion sort
    print(insertionSort(createRandArray(10,100)))