From dc8d38291056f59b14ee97bb023dcb368e779b73 Mon Sep 17 00:00:00 2001 From: ergz Date: Mon, 7 Aug 2023 23:29:47 -0700 Subject: [PATCH] fix py decrement missing and in C move j-- to correct spot --- c/insertion-sort.c | 6 ++++-- python/insertion-sort.py | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/c/insertion-sort.c b/c/insertion-sort.c index 7eab1a2..486e1e7 100644 --- a/c/insertion-sort.c +++ b/c/insertion-sort.c @@ -9,8 +9,10 @@ void insertion_sort(int arr[], int len) { while (j >= 0) { if (temp < arr[j]) { arr[j + 1] = arr[j]; + j--; + } else { + break; } - j--; } arr[j + 1] = temp; } @@ -25,7 +27,7 @@ void print_array(int arr[], int len) { } int main() { - int arr[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; + int arr[10] = {10, 9, 8, 7, 6, 5, 4, 3, 1, 2}; insertion_sort(arr, 10); print_array(arr, 10); } \ No newline at end of file diff --git a/python/insertion-sort.py b/python/insertion-sort.py index 80e124b..9d62788 100644 --- a/python/insertion-sort.py +++ b/python/insertion-sort.py @@ -6,6 +6,7 @@ def insertion_sort(array): while position >= 0: if array[position] > temp_value: array[position + 1] = array[position] + position -= 1 else: break array[position + 1] = temp_value