From 31d0aa2bcac07b770128659962eac5214caaa3f7 Mon Sep 17 00:00:00 2001 From: AarushiKapoor <54901924+AarushiKapoor@users.noreply.github.com> Date: Thu, 1 Oct 2020 02:09:12 +0530 Subject: [PATCH] Update insertionSort.h --- insertionSort.h | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/insertionSort.h b/insertionSort.h index 492ab4a..3b4d292 100644 --- a/insertionSort.h +++ b/insertionSort.h @@ -6,21 +6,17 @@ using namespace std; //------------------------------------------------------------ void insertionSort(vector &v, int n) { - int current, pos; + int j, small; for (int i = 1; i < n; i++) { - current = v[i]; - pos = i; // limit of the ordered part, pos not included - - // we make space - while (pos > 0 && v[pos - 1] > current) { - v[pos] = v[pos - 1]; - pos--; + small=v[i]; + j=i-1; + while((j>=0) && (small < v[j])) + { + v[j+1]=v[j]; + j--; } - - // we move the current value to its position - if (pos != i) - v[pos] = current; + v[j+1]=small; } }