mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
Revert "improved library"
This commit is contained in:
parent
743be6f53c
commit
9a82905aa5
@ -2,13 +2,11 @@
|
||||
* 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");
|
||||
@ -16,9 +14,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);
|
||||
@ -32,14 +30,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;
|
||||
}
|
||||
@ -56,7 +54,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] );
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user