Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions arrays/reverse-array/reversearray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

// REVERSE AN ARRAY

#include<iostream>
using namespace std;

void reArr(int arr[],int start ,int last){ // function to reverse array

int temp;

while(start <last){
temp =arr[start]; // simple swap
arr[start]=arr[last];
arr[last]=temp;
last--;
start++;
}
}


int main(){
int n;
cout<<"Enter size of array : ";
cin>>n;
int arr[n];
cout<<"Enter the element of array\n";

for(int i=0;i<n;i++){
cin>>arr[i];
}
reArr(arr,0,n-1); // function for reverse array element
cout<<"Reversed array\n";

for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}

return 0;
}
49 changes: 49 additions & 0 deletions arrays/searching/binary-search/binary-search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

//Binary search .................Iterative method best for sorted array

# include<bits/stdc++.h>
using namespace std; //std::cout or std::cin

int main(){
int n,t;
cout<<"enter test cases"<<endl;
cin>>t;
//cout<<(sizeof(arr)/sizeof(arr[0])); sizeof(arr) = datatype of arr * number of variable
while(t--){
cout<<"enter size of array "<<endl;
cin>>n;
int arr[n];

cout<<"Enter array element\n";

for(int i=0;i<n;i++){
cin>>arr[i];
}

int key;
cout<<"enter key to search"<<endl;
cin>>key;

int low=0,high=n-1, flag=0;
for(int i=0;i<n;i++){
// int mid =(low +high)/2; //It take more time 5.2 second
// cout<<mid;
int mid=low+(high-low)/2; // rather using (low+high)/2 we use this bez It take less time 4.1 second
if(arr[mid]==key){
cout<<"present"<<endl;
flag=1;
break;
}
else if(arr[mid]>key){
high= mid-1;
}
else if(arr[mid] <key)
low = mid+1;
}

if(flag==0){
cout<<"Not present"<<endl;
}
}
return 0;
}
40 changes: 40 additions & 0 deletions arrays/searching/linear-search/linear-search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//Linear search key

#include<bits/stdc++.h>
using namespace std;

int main(){
int n; // size of array
cout<<"enter size of array"<<endl;
cin>>n;
int arr[n]; // array to store all the elements

cout<<"Enter array element\n";
for(int i=0 ;i<n;i++){
cin>>arr[i]; // loop for entering array
}
int key; // key variable to be search
cout<<"enter key to be search"<<endl;
cin>>key;
int i =0 ,flag =0 ,count =0,lastpos;
for(i =0 ; i<n ;i++) {
if(key == arr[i]){
// cout<<"present"<<" "<<i+1<<endl;
lastpos=i+1;
flag =1;
break;
// break; using this break statment present will run only 1 time if same element array then it will stop at first time
// using break time compx O(1)
// without break we can print both 1 or all position of key present in array
}
count++;
}
if(flag == 0){
cout<<"Not present"<<endl;
} else {
cout<<"present \n position"<<lastpos<<endl; // time compx O(n)
//cout<<count<<endl;
}

return 0;
}