I tried the following code for searching an element in the array using binary search without using the function, but it does not work as it stops just after asking the Number I am searching for in the array. Not able to figure out, As exactly where I am mistaken.
Using Visual Studio Code.
int main()
{
int arr[10],n,num,mid,l=0,h=n-1,i;
cout<<"Enter the number of elements in the array\n";
cin>>n;
cout<<"Enter the elements of the array\n";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Enter the number to be searched.\n";
cin>>num;
while(l<=h)
{
mid=(l+h)/2;
if(arr[mid]==num)
{
cout<<"Number found at "<<mid<<"\n";
break;
}
if(arr[mid]>num)
{
h=mid-1;
}
else
{
l=mid+1;
}
}
if(l>h)
{
cout<<"Number not found.\n";
}
return 0;
}
You have initialized h = n-1 before initializing n. Hence, we have Undefined behaviour.
#include <iostream>
using namespace std;
int main()
{
int arr[10], n, num , mid, l, h, i;
cout<<"Enter the number of elements in the array\n";
cin>>n;
cout<<"Enter the elements of the array\n";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Enter the number to be searched.\n";
cin>>num;
l = 0;
h = n-1;
while(l <= h)
{
mid = (l+h)/2;
if(arr[mid] == num)
{
cout<<"Number found at index "<<mid<<"\n";
break;
}
if(arr[mid] > num)
{
h = mid-1;
}
else
{
l = mid+1;
}
}
if(l > h)
{
cout<<"Number not found.\n";
}
return 0;
}
so here is my code -->
#include <iostream>
#include <cmath>
using namespace std;
int binarySearch(int arr[] , int first , int last ,int value){
if(last >= first){
int mid = first + (last-first)/2;
if(arr[mid] == value){
return mid;
}
if(value > arr[mid]){
return binarySearch(arr , mid+1 , last , value);
}else if(value< arr[mid]){
return binarySearch(arr , first , mid-1 , value);
}
}
return -1;
}
int main(){
int n; cin >> n;
int arr[n];
for(int i=0 ; i<n ; i++){
cin >> arr[i];
} // requires sorted array
int value ; cin >> value;
cout << binarySearch(arr , 0 , n-1 , value);
return 0;
}
Related
This is a simple binary search program, but for some reason, the program just doesn't move on after asking for the value of the key from the user. At first, I thought it is an issue with my compiler, but it still happens wherever I paste the code, and I don't know why.
#include <iostream>
using namespace std;
int binary(int arr[], int n, int k){
int s = 0;
int e = n;
int mid = (s+e)/2;
while(s<=e){
if(k==arr[mid]){
return mid;
}
else if(k>arr[mid]){
s = mid+1;
}
else if(k<arr[mid]){
e = mid-1;
}
}
return -1;
}
int main(){
int i, n, key;
cin>>n;
int a[n];
for(i=0;i<n;i++){
cin>>a[i];
}
cout<<"Enter key:"<<endl;
cin>>key;
cout<< binary(a, n, key);
}
Instead of moving on after k, the code just does nothing.
you code is looping in the 'binary ' function
try this to see
while(s<=e){
cout << s << e; <<<=====
learn to use your debugger to step through the code
The middle element is found inside the loop in binary search because the search interval is reduced to half in every iteration.
Your mid is not changing that is why the program is not terminating.
So final code after correction is:
#include <iostream>
using namespace std;
int binary(int arr[], int n, int k)
{
int s = 0;
int e = n;
while (s <= e)
{
int mid = (s + e) / 2;
if (k == arr[mid])
{
return mid;
}
else if (k > arr[mid])
{
s = mid + 1;
}
else if (k < arr[mid])
{
e = mid - 1;
}
}
return -1;
}
int main()
{
int i, n, key;
cin >> n;
int a[n];
for (i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "Enter key:" << endl;
cin >> key;
cout << binary(a, n, key);
}
I found this code online on tutorials point. link https://www.tutorialspoint.com/cplusplus-program-to-generate-all-possible-combinations-out-of-a-b-c-d-e.
I tried to think of how to modify it so that it would randomly a single combination from the generated list, but I'm haven't figured it out yet.
#include<iostream>
using namespace std;
void Combi(char a[], int reqLen, int s, int currLen, bool check[], int l)
{
if(currLen > reqLen)
return;
else if (currLen == reqLen) {
cout<<"\t";
for (int i = 0; i < l; i++) {
if (check[i] == true) {
cout<<a[i]<<" ";
}
}
cout<<"\n";
return;
}
if (s == l) {
return;
}
check[s] = true;
Combi(a, reqLen, s + 1, currLen + 1, check, l);
check[s] = false;
Combi(a, reqLen, s + 1, currLen, check, l);
}
int main() {
int i,n;
bool check[n];
cout<<"Enter the number of element array have: ";
cin>>n;
char a[n];
cout<<"\n";
for(i = 0; i < n; i++) {
cout<<"Enter "<<i+1<<" element: ";
cin>>a[i];
check[i] = false;
}
for(i = 1; i <= n; i++) {
cout<<"\nThe all possible combination of length "<<i<<" for the given array set:\n";
Combi(a, i, 0, 0, check, n);
}
return 0;
}
im not a c++ specialist, but i think you should add a random number from -ArrayLenght to ArrayLenght, at least this works in python(which is written in c++)
i hope i understood your question right
As you can see, I want to find the location of an given array.
For example :
I have an array {5 , 2, 3, 1} (I want to decide how many guesses I want to guess)
Then the program will sort it {1, 2, 3, 5}
At last, I'll be given a chance to guess the number which I desired how many guess (example : I want 2 number guesses, they are 5 and 3. then the program will search for the number 5 and 3. The program will tell me the location in the sorted array. Then it is "3 found at 3, 5 found at 4"
However, I have my code stuck in the sorting
this is my code :
#include <iostream>
using namespace std;
int main(void)
{
int temp, i, j, n, list[100];
cin>>n;
for(i=0; i<n; i++)
{
cin>>list[i];
}
for(i=0; i<n-1; i++)
for(j=i+1; j<n; j++)
if(list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
for(i=0; i<n; i++)
cout<<" "<<list[i];
cout<<endl;
return 0;
}
And this link is the full question of my project :
http://uva.onlinejudge.org/external/104/p10474.pdf
There is no problem in your sort function , by the way you can solve original problem in O(nlogn) , yours is O(n^2)
code:
#include <iostream>
#include <algorithm>
using namespace std;
int binary_search(int A[], int key, int imin, int imax)
{
if (imax < imin)
return -1;
else
{
int imid = (imax + imin)/2;
if (A[imid] > key)
// key is in lower subset
return binary_search(A, key, imin, imid - 1);
else if (A[imid] < key)
// key is in upper subset
return binary_search(A, key, imid + 1, imax);
else
// key has been found
return imid;
}
}
int main() {
// your code goes here
int n,q;
while(1){
cin >> n>> q;
if(n==0)
break;
int* a = new int[n];
int i;
for(i=0;i<n;i++)
cin >> a[i];
sort(a,a+n);
while(q--){
int k;
cin >> k;
k=binary_search(a,k,0,n-1);
if(k==-1)
cout << "not found" << endl;
else
cout << "found at :" << k+1 << endl;
}
}
return 0;
}
I am trying to implement a simple merge sort for even number of elements using the following code :
#include <iostream>
using namespace std;
void merge(int arr1[10],int len1,int arr2[10],int len2,int arr3[10],int len3)
{
int i,j,k;
while(i<len1&&j<len2&&k<len3)
{
if(arr1[i]<arr2[j])
{
arr3[k] = arr1[i];
i++;k++;
}
else if(arr1[i]>arr2[j])
{
arr3[k] = arr2[j];
k++;
j++;
}
}
}
void mergeSort(int a[10],int n)
{
int arr1[10],arr2[10];
for(int i=0;i<n/2;i++)
{
arr1[i] = a[i];
}
for(int i=(n/2+1);i<n;i++)
{
arr2[i] = a[i];
}
mergeSort(arr1,n/2);
mergeSort(arr2,n/2);
merge(arr1,n/2,arr2,n/2,a,n);
}
int main() {
int arr[10],n;
cout << "Enter number of elements\n";
cin >> n;
cout<<"Enter elements\n";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
mergeSort(arr, n);
cout<<"Sorted array is"<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
But I am getting a EXC_BAD_ACESS error on the opening brace of the mergeSort() method. I am new to Xcode and not sure on how to fix this. How do I fix this ?
Thanks !
You have forgot to initialize the int variables i, j, k to 0 (zero) in the function definition for merge(...).
void merge(int arr1[10],int len1,int arr2[10],int len2,int arr3[10],int len3)
{
// int i,j,k; // Not initialized.
int i = j = k = 0;
while(i<len1&&j<len2&&k<len3)
{
...
...
}
After Edit:
void mergeSort(int a[10],int n)
{
int arr1[10],arr2[10];
for(int i=0;i<n/2;i++)
{
arr1[i] = a[i];
}
// Here you are skipping the middle index.
// Let's say array is of length 10, then arr1 contains elements on index from 0 to 4 (5 elements),
// whereas arr2 contains elements on index from 6 to 9 (4 elements).
// 5th index element is missing.
// for(int i=(n/2+1);i<n;i++)
for(int i=(n/2);i<n;i++)
{
arr2[i] = a[i];
}
mergeSort(arr1,n/2);
mergeSort(arr2,n/2);
merge(arr1,n/2,arr2,n/2,a,n);
}
Hope it helps!
After reading through the theory of Merge Sort on TopCoder, I tried to write it's implementations, but it's getting weird, and I'm more or less a beginner in programming, especially algorithms. Can somebody assist me?
#include <iostream>
using namespace std;
int arr[] = {2, 0, 43, 12, 98};
int sizeOfarr(int a[])
{
return sizeof(a)/sizeof(a[0]);
}
int minElement(int x, int y)
{
if (x > y)
{
return y;
}
else if (x < y)
{
return x;
}
else
{
return x, y;
}
}
int main()
{
int t, z;
int n = sizeOfarr(arr);
int finalList[n];
int list1[n];
int list2[n];
for(int i = 0; i<=((n/2)-1); i++)
{
list1[i] = arr[i];
}
for(int j = n/2; j<n; j++)
{
for(int k = 0; k<=((n/2)-1); k++ )
{
list2[k] = arr[j];
}
}
for(int y = 0; y<=n; y++)
{
while(sizeOfarr(finalList)!=n)
{
t = list1[0];
z = list2[0];
finalList[y] = minElement(t, z);
if(finalList[y]==t)
{
list1[0] = list1[1];
}
else if(finalList[y]==z)
{
list2[0] = list2[1];
}
else
{
list1[0] = list1[1];
list2[0] = list2[1];
}
}
}
cout << "The sorted list is: " << finalList << endl;
return 0;
}
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
int temp[10000];
void merge(int *A,int low,int mid,int high)
{
int i=low;
int j=mid+1;
int k=low;
int l;
while(i<=mid && j<=high)
{
if(A[i]<A[j])
{
temp[k]=A[i];
i=i+1;
}
else
{
temp[k]=A[j];
j=j+1;
}
k++;
}
for(l=i;l<=mid;l++,k++)
{
temp[k]=A[l];
}
for(l=j;l<=high;l++,k++)
{
temp[k]=A[l];
}
memcpy(A,temp,sizeof(A[0])*k);
}
void mergeSort(int *A,int low,int high)
{
int mid;
if(low<high)
{
mid=floor((low+high)/2);
mergeSort(A,low,mid);
mergeSort(A,mid+1,high);
merge(A,low,mid,high);
}
}
int main(int argc,char *argv[])
{
int n;
int array[10000];
cout<<"please enter the number numbers\n";
cin>>n;
cout<<"please enter the nubers\n";
for(int i=0;i<n;i++)
{
cin>>array[i];
}
mergeSort(array,0,n-1);
for(int i=0;i<n;i++)
{
cout<<array[i]<<" ";
}
cout<<"\n";
}
This is my implementation
mergeSort function divide recursively at middle and repeats until low lt(less than) high then a merge function is called.
I see from your code that the operator "," (return x,y) would replace x value by y value.
A few comments on the code:
return x,y // this just returns y. this is the case when x==y so it probably is OK bit not what one would write.
while(sizeOfarr(finalList)!=n) // The size of your array finalist is n elements. This is never going to change so this while condition is always false and the loop will never execute.