enh(Python): rename from insertionSort_rec to insertion_sort_rec (#669)
parent
fd9f7b4806
commit
110518e7c6
|
@ -4,17 +4,16 @@ Recursively sort first n-1 elements.
|
|||
Insert last element at its correct position in sorted array.
|
||||
"""
|
||||
|
||||
def insertionSort_rec(array, n):
|
||||
# base case
|
||||
if n <= 1:
|
||||
def insertion_sort_rec(array, length_arr):
|
||||
"""Base Case"""
|
||||
if length_arr <= 1:
|
||||
return
|
||||
# Sort first n-1 elements
|
||||
insertionSort_rec(array, n-1)
|
||||
'''Insert last element at its correct position
|
||||
in sorted array.'''
|
||||
end = array[n-1]
|
||||
j = n-2
|
||||
|
||||
insertion_sort_rec(array, length_arr-1)
|
||||
# Insert last element at its correct position in sorted array.
|
||||
end = array[length_arr-1]
|
||||
j = length_arr-2
|
||||
|
||||
# Move elements of arr[0..i-1], that are
|
||||
# greater than key, to one position ahead
|
||||
# of their current position
|
||||
|
@ -23,8 +22,8 @@ def insertionSort_rec(array, n):
|
|||
j -= 1
|
||||
array[j+1] = end
|
||||
|
||||
|
||||
arr = [6, 5, 2, 7, 12, 9, 1, 4]
|
||||
insertionSort_rec(arr, len(arr))
|
||||
print("Sorted array is:")
|
||||
print(arr)
|
||||
if __name__ == '__main__':
|
||||
arr = [6, 5, 2, 7, 12, 9, 1, 4]
|
||||
insertion_sort_rec(arr, len(arr))
|
||||
print("Sorted array is:")
|
||||
print(arr)
|
||||
|
|
Loading…
Reference in New Issue