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