Merge pull request #542 from yyash01/master

improved library
This commit is contained in:
Sombit Bose 2020-06-08 22:14:07 +05:30 committed by GitHub
commit 743be6f53c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,11 +2,13 @@
* Using binary search to find the proper location for
* inserting the selected item at each iteration. */
#include <stdio.h>
#include<stdlib.h>
//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] );
}