“trier l'insertion python” Réponses codées

Comment effectuer un tri d'insertion, dans Python?

"""
This is an implementation of the insertion sort
algorithm. 
Idea is to grow a sorted subset from one element
and then pull all elements of array into that
sorted subset while keeping that subset sorted.
Let n be the size of the list to sort

Time complexity: O(n^2), 
Space complexity: O(1)
"""
def insertion_sort(arr):
    for idx in range(1, len(arr)):
        scan = idx
        while scan > 0 and arr[scan] < arr[scan-1]:
            swap(arr, scan, scan-1)
            scan -= 1
    return arr

def swap(arr, idx1, idx2):
    arr[idx1], arr[idx2] = arr[idx2], arr[idx1]

arr_to_sort = [1, 10, 3, 2]
print(insertion_sort(arr_to_sort))  # [1, 2, 3, 10]
Wissam

trier l'insertion python

#Insertion sort
ar = [34, 42, 22, 54, 19, 5]

for i in range(1, len(ar)):
    while ar[i-1] > ar[i] and i > 0:
        ar[i-1], ar[i] = ar[i], ar[i-1]
        i -= 1
print(ar)
Kumaran KM

Réponses similaires à “trier l'insertion python”

Questions similaires à “trier l'insertion python”

Plus de réponses similaires à “trier l'insertion python” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code