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
|
* Using binary search to find the proper location for
|
||||||
* inserting the selected item at each iteration. */
|
* inserting the selected item at each iteration. */
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include<stdlib.h>
|
|
||||||
//nothing to add on library
|
|
||||||
|
|
||||||
/*Displays the array, passed to this method*/
|
/*Displays the array, passed to this method*/
|
||||||
void display(int arr[], int n) {
|
void display(int arr[], int n) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < n; i++) {
|
for(i = 0; i < n; i++){
|
||||||
printf("%d ", arr[i]);
|
printf("%d ", arr[i]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
@ -16,9 +14,9 @@ void display(int arr[], int n) {
|
|||||||
|
|
||||||
int binarySearch(int arr[], int key, int low, int high) {
|
int binarySearch(int arr[], int key, int low, int high) {
|
||||||
if (low >= high)
|
if (low >= high)
|
||||||
return (key > arr[low]) ? (low + 1) : low;
|
return (key > arr[low]) ? (low + 1): low;
|
||||||
int mid = low + (high - 1) / 2;
|
int mid = low + (high - 1) / 2;
|
||||||
if (arr[mid] == key)
|
if(arr[mid] == key)
|
||||||
return mid + 1;
|
return mid + 1;
|
||||||
else if (arr[mid] > key)
|
else if (arr[mid] > key)
|
||||||
return binarySearch(arr, key, low, mid - 1);
|
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) {
|
void insertionSort(int arr[], int size) {
|
||||||
int i, j, key, index;
|
int i, j, key, index;
|
||||||
for (i = 0; i < size; i++) {
|
for(i = 0; i < size; i++) {
|
||||||
j = i - 1;
|
j = i - 1;
|
||||||
key = arr[i];
|
key = arr[i];
|
||||||
/* Use binrary search to find exact key's index */
|
/* Use binrary search to find exact key's index */
|
||||||
index = binarySearch(arr, key, 0, j);
|
index = binarySearch(arr, key, 0, j);
|
||||||
/* Move all elements greater than key from [index...j]
|
/* Move all elements greater than key from [index...j]
|
||||||
* to one position */
|
* to one position */
|
||||||
while (j >= index) {
|
while(j >= index) {
|
||||||
arr[j + 1] = arr[j];
|
arr[j + 1] = arr[j];
|
||||||
j = j - 1;
|
j = j - 1;
|
||||||
}
|
}
|
||||||
@ -56,7 +54,7 @@ int main(int argc, const char * argv[]) {
|
|||||||
printf("Enter the elements of the array\n");
|
printf("Enter the elements of the array\n");
|
||||||
int i;
|
int i;
|
||||||
int arr[n];
|
int arr[n];
|
||||||
for (i = 0; i < n; i++) {
|
for(i = 0; i < n; i++) {
|
||||||
scanf("%d", &arr[i] );
|
scanf("%d", &arr[i] );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user