Skip to content

Changed Folder Structure, Updated ReadMe.md and added Binary Search #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
48 changes: 48 additions & 0 deletions Binary Search/Binary Search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include<bits/stdc++.h>
using namespace std;

void solve()
{
int n;
cin >> n;

int k;
cin >> k;

vector<int> arr(n);

for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
int low = 0, high = n-1;

while(low<=high)
{
int mid = high + (low-high)/2;

if(arr[mid] == k){
cout<<mid;
break;
}

if(arr[mid] > k){
high = mid - 1;
}
else{
low = mid + 1;
}

}
}

int main()
{
ios_base::sync_with_stdio(false);

cin.tie(NULL);
cout.tie(NULL);

solve();

}
47 changes: 47 additions & 0 deletions Binary Search/Find Minimum in Rotated Sorted Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include<bits/stdc++.h>
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);

cin.tie(NULL);
cout.tie(NULL);

int n = 6;
vector<int> arr {4, 5, 6 ,7, 2, 3};

int low = 0;
int high = n-1;

if(n == 1)
cout << arr[0];
// return arr[0];
if(arr[high] > arr[0])
cout << arr[0];


while(low < high){
int mid = low + (high-low)/2;

if(arr[mid] > arr[mid + 1])
{
cout << arr[mid];
break;
}

if(arr[mid-1] > arr[mid])
{
cout << arr[mid];
break;
}

if(arr[mid] > arr[low]){
low = mid+1;
}
else{
high = mid - 1;
}
}

}
71 changes: 71 additions & 0 deletions Binary Search/First and Last occurance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include<bits/stdc++.h>
using namespace std;

void solve()
{
int n;
cin >> n;

int k;
cin >> k;

vector<int> arr(n);

int first = 0, last = 0;

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

int low = 0, high = n-1;

while(low<=high)
{
int mid = high + (low-high)/2;

if(arr[mid] == k){
first = mid;
high = mid-1;
}

if(arr[mid] > k){
high = mid - 1;
}
else if(arr[mid] < k){
low = mid + 1;
}
}

low = 0;
high = n-1;

while(low<=high)
{
int mid = high + (low-high)/2;

if(arr[mid] == k){
last = mid;
low = mid+1;
}

if(arr[mid] > k){
high = mid - 1;
}
else{
low = mid + 1;
}
}
cout<<first << " " << last;
}

int main()
{
ios_base::sync_with_stdio(false);

cin.tie(NULL);
cout.tie(NULL);

solve();

}
53 changes: 53 additions & 0 deletions Binary Search/Number of Times an Array is Sorted.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include<bits/stdc++.h>
using namespace std;

void solve()
{
int n;
cin >> n;
vector<int> arr(n);

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

int low = 0;
int high = n-1;

while(low <= high){

int mid = (low + high)/2;

if(arr[mid] <= arr[mid-1] && arr[mid] <= arr[mid+1])
{
cout<<mid+1;
break;
}
else if( arr[mid] < arr[high] )
{
high = mid-1;
}
else
{
low = mid+1;
}
}

}

int main()
{
ios_base::sync_with_stdio(false);

cin.tie(NULL);
cout.tie(NULL);

int t;
cin >> t;

while(t--)
{
solve();
}
}
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,34 @@
# Basic_cpp
Basic c++ codes

<p align="center">
<img src="https://hacktoberfest.digitalocean.com/assets/HF-full-logo-b05d5eb32b3f3ecc9b2240526104cf4da3187b8b61963dd9042fdc2536e4a76c.svg">
</p>

A community repository to allow enthusiasts and beginners add their projects for [@DigitalOcean's HacktoberFest](https://hacktoberfest.digitalocean.com)
## About this Repository:

This repository allows any type of CPP Code to be added! When adding your own Code, place it in a folder within most suitable category. If there are no folder of that category yet, make a new folder and add it there! Making your PR will get your 1 step closer to your free T-Shirt!

## What is Hacktoberfest?

Hacktoberfest® is open to everyone in our global community. Whether you’re a developer, student learning to code, event host, or company of any size, you can help drive growth of open source and make positive contributions to an ever-growing community. All backgrounds and skill levels are encouraged to complete the challenge.

- Hacktoberfest is a celebration open to everyone in our global community.
- Pull requests can be made in any GitHub-hosted repositories/projects.
- You can sign up anytime between October 1 and October 31.

Sign up [Here](https://hacktoberfest.digitalocean.com)

## Making A Thoughtful Pull Request

Pull requests should include the following:

- A title about the project/program you are including
- Detailed information about the commits
- No low effort pull requests or duplicate programs
- Place your Code in the appropriate folder with a README and information on the code


### Languages Used Already:

- C
- CPP
File renamed without changes.
134 changes: 67 additions & 67 deletions Sorting - Merge Sort.cpp → Sorting/Sorting - Merge Sort.cpp
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
// Sorting - Merge Sort
#include<iostream>
using namespace std;
void Merge(int A[],int l,int m,int r)
{
int i,j,k;
int n1=m-l+1;
int n2=r-m;
int L[n1],R[n2];
for(i=0;i<n1;i++)
L[i]=A[l+i];
for(j=0;j<n2;j++)
R[j]=A[m+1+j];
i=0;
j=0;
k=l;
while(i<n1 && j<n2){
if(L[i]<=R[j]){
A[k]=L[i];
i++;
}else{
A[k]=R[j];
j++;
}
k++;
}
while(i<n1){
A[k]=L[i];
i++;
k++;
}
while(j<n2){
A[k]=R[j];
j++;
k++;
}
}
void Mergesort(int arr[],int l,int r)
{
if(l<r)
{
int m=l+(r-l)/2;
Mergesort(arr,l,m);
Mergesort(arr,m+1,r);
Merge(arr,l,m,r);
}
}
int main()
{
int arr[]={2,1,3,4,8,5,7,6};
int arr_size=sizeof(arr)/sizeof(arr[0]);
cout<<"Given array :\n";
for(int i=0;i<arr_size;i++)
cout<<arr[i]<<" ";
Mergesort(arr,0,arr_size-1);
cout<<"\n\nAfter sorting :\n";
for(int i=0;i<arr_size;i++)
cout<<arr[i]<<" ";
cout<<"\n\n";
}

// Sorting - Merge Sort

#include<iostream>
using namespace std;
void Merge(int A[],int l,int m,int r)
{
int i,j,k;
int n1=m-l+1;
int n2=r-m;

int L[n1],R[n2];
for(i=0;i<n1;i++)
L[i]=A[l+i];
for(j=0;j<n2;j++)
R[j]=A[m+1+j];

i=0;
j=0;
k=l;
while(i<n1 && j<n2){
if(L[i]<=R[j]){
A[k]=L[i];
i++;
}else{
A[k]=R[j];
j++;
}
k++;
}
while(i<n1){
A[k]=L[i];
i++;
k++;
}
while(j<n2){
A[k]=R[j];
j++;
k++;
}
}
void Mergesort(int arr[],int l,int r)
{
if(l<r)
{
int m=l+(r-l)/2;
Mergesort(arr,l,m);
Mergesort(arr,m+1,r);
Merge(arr,l,m,r);
}
}
int main()
{
int arr[]={2,1,3,4,8,5,7,6};
int arr_size=sizeof(arr)/sizeof(arr[0]);

cout<<"Given array :\n";
for(int i=0;i<arr_size;i++)
cout<<arr[i]<<" ";

Mergesort(arr,0,arr_size-1);

cout<<"\n\nAfter sorting :\n";
for(int i=0;i<arr_size;i++)
cout<<arr[i]<<" ";
cout<<"\n\n";
}