From 44ddbf3c4c50fbd8cebbafc0ec04097060b97002 Mon Sep 17 00:00:00 2001 From: yyash01 Date: Mon, 8 Jun 2020 01:34:58 +0530 Subject: [PATCH] improved library --- sorting/binary_Insertion_Sort.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/sorting/binary_Insertion_Sort.c b/sorting/binary_Insertion_Sort.c index 4c3f2dd2..fb91a746 100644 --- a/sorting/binary_Insertion_Sort.c +++ b/sorting/binary_Insertion_Sort.c @@ -2,11 +2,13 @@ * Using binary search to find the proper location for * inserting the selected item at each iteration. */ #include +#include +//nothing to add on library /*Displays the array, passed to this method*/ void display(int arr[], int n) { int i; - for(i = 0; i < n; i++){ + for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); @@ -14,9 +16,9 @@ void display(int arr[], int n) { int binarySearch(int arr[], int key, int low, int high) { if (low >= high) - return (key > arr[low]) ? (low + 1): low; + return (key > arr[low]) ? (low + 1) : low; int mid = low + (high - 1) / 2; - if(arr[mid] == key) + if (arr[mid] == key) return mid + 1; else if (arr[mid] > key) return binarySearch(arr, key, low, mid - 1); @@ -30,14 +32,14 @@ int binarySearch(int arr[], int key, int low, int high) { */ void insertionSort(int arr[], int size) { int i, j, key, index; - for(i = 0; i < size; i++) { + for (i = 0; i < size; i++) { j = i - 1; key = arr[i]; /* Use binrary search to find exact key's index */ index = binarySearch(arr, key, 0, j); /* Move all elements greater than key from [index...j] * to one position */ - while(j >= index) { + while (j >= index) { arr[j + 1] = arr[j]; j = j - 1; } @@ -54,7 +56,7 @@ int main(int argc, const char * argv[]) { printf("Enter the elements of the array\n"); int i; int arr[n]; - for(i = 0; i < n; i++) { + for (i = 0; i < n; i++) { scanf("%d", &arr[i] ); }