Untitled

 avatar
unknown
plain_text
2 months ago
2.1 kB
4
Indexable
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;

void BinarySearch(int arr[] , int start , int end , int key){
    
    while(start<=end){
        
        int mid = start + (end-start)/2;
        
        if(arr[mid]==key){
        cout<<key<<" found at index : "<<mid<<endl;
        return ;
        }
    
        else if(arr[mid]>key){
        end = mid-1;
        }
    
        else{
        start = mid+1;
        }    
    }
    
    cout<<"Element not FOUND"<<endl;
}

void LinearSearch(int arr[] ,int key , int size){
    
    for(int i=0 ; i<size ; i++){
        
        if(arr[i]==key){
            cout<<key<<" found at Index : "<<i<<endl;
            return;
        }
    }
    
    cout<<key<<" is not present in the given array"<<endl;
}

void BubbleSort(int arr[] , int start , int end){
    
    for(int i=0 ; i<end ; i++){
        for(int j=0 ; j<end-1 ; j++){
            if(arr[j+1]<arr[j]){
                swap(arr[j],arr[j+1]);
            }
        }
    }
}

void PrintArray(int arr[] , int size){
    
    for(int i=0 ; i<size ; i++){
        cout<<arr[i]<<" ";
    }
    
    cout<<endl;
}

int fib(int term){
    
    if(term==0 ){
        return 0;
    }
    
    if(term==1){
        return 1;
    }
    
    return fib(term-1) + fib(term-2);
}



int main() {
   
//   int arr[5] = {1,2,4,6,7};
//   LinearSearch(arr,4,5);
//   BinarySearch(arr,0,4,7);
   
//   int unsorted[10]={5,1,2,10,20,3,8,20,15,6};
//   cout<<"Array Before Sort :";
//   PrintArray(unsorted,10);
//   cout<<"Array After Sort :";
//   BubbleSort(unsorted,0,10);
//   PrintArray(unsorted,10);

    int *newArray = new int[5];
    
    cout<<"Created an array using Dynamic Memory Allocation "<<endl;
    
    for(int i=0 ; i<5 ; i++){
        newArray[i]=i+1;
    }
    
    PrintArray(newArray,5);
    
    delete []newArray;
    
    cout<<"Deallocated the memory !"<<endl;
    
    PrintArray(newArray,5);
   
//   cout<<fib(4);
   

    return 0;
}
Editor is loading...
Leave a Comment