I've made a program to find the missing element (if there's one) in a sequential list:
Program criteria:
Returns
-1
if a list is sequential, otherwise the missing element in a non-sequential list is returned.
To perform binary search, we need a key to half the problem size. Here the key is to find the distance between the index (
low
,high
) and their corresponding values.
Compilation procedure:
gcc -Wall -g testList.c -o testList
./testList
#include<stdio.h>
int bSearch(int array[], int low, int high){
int mid = (low + high) / 2;
if(high == low + 1){
if(array[high] > array[low] +1){
return array[low] +1;
}else{
return -1;
}
}
if(array[mid] - array[low] == mid - low){
return bSearch(array, mid, high);
}else if(array[mid] - array[low] > mid - low){
return bSearch(array, low, mid);
}else{
return -1;
}
}
int main(void){
int array [] = {22, 23, 24, 25, 26, 27, 28, 30};
int upperBound = (sizeof(array)/sizeof(array[0]))-1;
printf("%d\n", bSearch(array, 0, upperBound));
return 0;
}
My questions:
Why does removing the
else{}
case cause the compiler to give a warning for it?Can the base case look more elegant?
For code elegance and optimization, can this code be improved?
I've made a program to find the missing element (if there's one) in a sequential list:
Program criteria:
Returns
-1
if a list is sequential, otherwise the missing element in a non-sequential list is returned.
To perform binary search, we need a key to half the problem size. Here the key is to find the distance between the index (
low
,high
) and their corresponding values.
Compilation procedure:
gcc -Wall -g testList.c -o testList
./testList
#include<stdio.h>
int bSearch(int array[], int low, int high){
int mid = (low + high) / 2;
if(high == low + 1){
if(array[high] > array[low] +1){
return array[low] +1;
}else{
return -1;
}
}
if(array[mid] - array[low] == mid - low){
return bSearch(array, mid, high);
}else if(array[mid] - array[low] > mid - low){
return bSearch(array, low, mid);
}else{
return -1;
}
}
int main(void){
int array [] = {22, 23, 24, 25, 26, 27, 28, 30};
int upperBound = (sizeof(array)/sizeof(array[0]))-1;
printf("%d\n", bSearch(array, 0, upperBound));
return 0;
}
My questions:
Why does removing the
else{}
case cause the compiler to give a warning for it?Can the base case look more elegant?
For code elegance and optimization, can this code be improved?
I've made a program to find the missing element (if there's one) in a sequential list:
Program criteria:
Returns
-1
if a list is sequential, otherwise the missing element in a non-sequential list is returned.To perform binary search, we need a key to half the problem size. Here the key is to find the distance between the index (
low
,high
) and their corresponding values.Compilation procedure:
gcc -Wall -g testList.c -o testList ./testList
#include<stdio.h>
int bSearch(int array[], int low, int high){
int mid = (low + high) / 2;
if(high == low + 1){
if(array[high] > array[low] +1){
return array[low] +1;
}else{
return -1;
}
}
if(array[mid] - array[low] == mid - low){
return bSearch(array, mid, high);
}else if(array[mid] - array[low] > mid - low){
return bSearch(array, low, mid);
}else{
return -1;
}
}
int main(void){
int array [] = {22, 23, 24, 25, 26, 27, 28, 30};
int upperBound = (sizeof(array)/sizeof(array[0]))-1;
printf("%d\n", bSearch(array, 0, upperBound));
return 0;
}
My questions:
Why does removing the
else{}
case cause the compiler to give a warning for it?Can the base case look more elegant?
For code elegance and optimization, can this code be improved?
Find a missing element in a sequentialsorted list
Find a missing element in a sequential list
I've made a program to find the missing element (if there's one) in a sequential list:
Program criteria:
Returns
-1
, if, a list is sequential, otherwise the missing element in a non-sequential list is returned.
To perform binary search, we need a key to half the problem size. Here the key is, to find the distance between the index (
low
,high
) and their corresponding values.
Compilation procedure:
gcc -Wall -g testList.c -o testList
./testList
#include<stdio.h>
int bSearch(int array[], int low, int high){
int mid = (low + high) / 2;
if(high == low + 1){
if(array[high] > array[low] +1){
return array[low] +1;
}else{
return -1;
}
}
if(array[mid] - array[low] == mid - low){
return bSearch(array, mid, high);
}else if(array[mid] - array[low] > mid - low){
return bSearch(array, low, mid);
}else{
return -1;
}
}
int main(void){
int array [] = {22, 23, 24, 25, 26, 27, 28, 30};
int upperBound = (sizeof(array)/sizeof(array[0]))-1;
printf("%d\n", bSearch(array, 0, upperBound));
return 0;
}
My questions:
Why does removing the
else{}
case cause the compiler to give a warning for it?Can the base case look more elegant?
For code elegance and optimization, can this code be improved?
Find missing element in sequential list
I've made a program to find the missing element (if there's one) in a sequential list:
Program criteria:
Returns
-1
, if, list is sequential otherwise missing element in non-sequential list is returned.
To perform binary search, we need a key to half the problem size. Here the key is, to find the distance between the index (
low
,high
) and their corresponding values.
Compilation procedure
gcc -Wall -g testList.c -o testList
./testList
#include<stdio.h>
int bSearch(int array[], int low, int high){
int mid = (low + high) / 2;
if(high == low + 1){
if(array[high] > array[low] +1){
return array[low] +1;
}else{
return -1;
}
}
if(array[mid] - array[low] == mid - low){
return bSearch(array, mid, high);
}else if(array[mid] - array[low] > mid - low){
return bSearch(array, low, mid);
}else{
return -1;
}
}
int main(void){
int array [] = {22, 23, 24, 25, 26, 27, 28, 30};
int upperBound = (sizeof(array)/sizeof(array[0]))-1;
printf("%d\n", bSearch(array, 0, upperBound));
return 0;
}
My questions:
Why does removing the
else{}
case cause the compiler to give a warning for it?Can the base case look more elegant?
For code elegance and optimization, can this code be improved?
Find a missing element in a sequential list
I've made a program to find the missing element (if there's one) in a sequential list:
Program criteria:
Returns
-1
if a list is sequential, otherwise the missing element in a non-sequential list is returned.
To perform binary search, we need a key to half the problem size. Here the key is to find the distance between the index (
low
,high
) and their corresponding values.
Compilation procedure:
gcc -Wall -g testList.c -o testList
./testList
#include<stdio.h>
int bSearch(int array[], int low, int high){
int mid = (low + high) / 2;
if(high == low + 1){
if(array[high] > array[low] +1){
return array[low] +1;
}else{
return -1;
}
}
if(array[mid] - array[low] == mid - low){
return bSearch(array, mid, high);
}else if(array[mid] - array[low] > mid - low){
return bSearch(array, low, mid);
}else{
return -1;
}
}
int main(void){
int array [] = {22, 23, 24, 25, 26, 27, 28, 30};
int upperBound = (sizeof(array)/sizeof(array[0]))-1;
printf("%d\n", bSearch(array, 0, upperBound));
return 0;
}
My questions:
Why does removing the
else{}
case cause the compiler to give a warning for it?Can the base case look more elegant?
For code elegance and optimization, can this code be improved?
- 8.6k
- 5
- 33
- 76
- 8.6k
- 5
- 33
- 76