Skip to content
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

added algorithms in C++ #33

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 24 additions & 0 deletions C++/clearBit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//Clearing the bit means to set the value of the bit at a given position to 0.
//The concept revolves around using the left shift operator to shift the binary form of 1 (0001) by the given position so that 1 is present at the required position. For ex: If I want to get the bit of a particular number at the 2nd position, I will leftshift 10 by 2 so that it becomes 0100. Now we will take the complement of it as ~(0100) which will make it (1011).
//Now if we perform an and operation on 1011 and the required number, we will get the bit. If it's 1, then the bit of the position (2nd position) is 1 as 1&0 = 0 whereas if it's 0, then the bit is 0 as 0&0=0.Thus, we have cleared the bit.

#include <iostream>
using namespace std;


int clearbit(int n, int pos)
{
int mask=~(1<<pos); //creating the complement of the temporary variable to which we have used the left shift operator
return(mask & n);
}

int main()
{
int n,pos;
cin>>n>>pos;

cout<<clearbit(n,pos)<<endl;

return 0;

}
72 changes: 72 additions & 0 deletions C++/countSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <iostream>
using namespace std;

void countSort(int arr[], int n)
{
int k = 0; //finding out the maximum entered value in the array.
for(int i=0; i<n; i++)
{
k=max(k,arr[i]);
}

int count[k+1]={0}; //Creating a count array of the length k+1 to maintain the frequency of each element of the array.

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

for(int i=1; i<k+1; i++) //Manipulating the count array to find out the position of each element in the main array.
{
count[i]+=count[i-1];
}

int result[n]; //Creating a result array to get the final output array

for(int i=n-1; i>=0; i--)
{
count[arr[i]]--;
result[count[arr[i]]] = arr[i];
}

for(int i=0; i<n; i++) //Appending the main array by the help of the result array
{
arr[i] = result[i];
}

return;
}

int main()
{
int n;

cin>>n;

int arr[n];

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

cout<<"Entered Array"<<endl;

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

countSort(arr,n);

cout<<endl;

cout<<"Sorted array"<<endl;

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

return 0;
}
67 changes: 67 additions & 0 deletions C++/dnfSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <iostream>
using namespace std;

void swap(int arr[], int i, int j)
{
int temp=arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

void dnfSort(int arr[], int n)
{
int low=0;
int mid=0;
int high=n-1;

while(mid<=high)
{
if(arr[mid]==0)
{
swap(arr,low,mid);
low++;
mid++;
}
else if(arr[mid]==1)
{
mid++;
}
else
{
swap(arr,mid,high);
high--;
}
}
return;
}

int main()
{
int n;
cin>>n;
int arr[n];

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

cout<<"Initial array:"<<endl;

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

cout<<endl;
cout<<"Sorted array:"<<endl;

dnfSort(arr,n);

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

return 0;
}
25 changes: 25 additions & 0 deletions C++/getBit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//Used to get the bit (1 or 0) at a given position of the given number.
//The concept revolves around using the left shift operator to shift the binary form of 1 (0001) by the given position so that 1 is present at the required position. For ex: If I want to get the bit of a particular number at the 2nd position, I will leftshift 10 by 2 so that it becomes 0100.
//Now if we perform an and operation on 0100 and the required number, we will get the bit. If it's 1, then the bit of the position (2nd position) is 1 as 1&1 = 1 whereas if it's 0, then the bit is 0 as 1&0=0.


#include <iostream>
using namespace std;


int getbit(int n, int pos)
{
int temp=1<<pos; //creating a temporary variable to which we have used the left shift operator
return((temp&n)!=0); //this returns (temp&n) !=0. We did this as temp&n will return the decimal value instead of the required bit. that's why we are basically checking it with 0. If it was 0; 0!=0 will give false or 0 which is the required bit while if the bit was 1; 1!=0 will give true or 1 which is the required bit.
}

int main()
{
int n,pos;
cin>>n>>pos;

cout<<getbit(n,pos)<<endl;

return 0;

}
92 changes: 92 additions & 0 deletions C++/mergeSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//MERGE SORT
//We basically keep deviding the array wrt respect to its mid element until it narrows down to 1 element. Then we *merge* the 2 arrays in a manner that the resultant array is already sorted. we perform this recursively in each heirarchial stage of the division. In the end, we are left with 2 arrays which are sorted independantly. Now when we will merge the 2 of em, we will finally get the sorted array.
//RECURRENCE RELATION: T(n)=2T(n/2) + n
// TIME COMPLEXITY: n*log(n)


#include <iostream>
using namespace std;

void merge(int arr[], int l, int mid, int r)
{
int n1=mid-l+1;
int n2=r-mid;

//Making the temporary arrays.
int a[n1];
int b[n2];

for(int i=0;i<n2;i++)
{
a[i]=arr[l+i];
}

for(int i=0;i<n2; i++)
{
b[i]=arr[mid+1+i];
}

// Merging and Sorting the arrays.
int i=0; //pointer to traverse over the array a
int j=0; //pointer to traverse over the array b
int k=0; //pointer to traverse over the main array.

while(i<n1 && j<n2)
{
if(a[i]<b[j])
{
arr[k]=a[i];
k++;
i++;
}
else
{
arr[k]=b[j];
k++;
j++;
}
}

while(i<n1)
{
arr[k]=a[i];
k++;
i++;
}

while(j<n2)
{
arr[k]=b[j];
k++;
j++;
}



}

void mergeSort(int arr[], int l, int r)
{
if(l<r)
{
int mid=(l+r)/2;
mergeSort(arr, l ,mid);
mergeSort(arr, mid+1, r);

merge(arr,l,mid,r);

}
}

int main()
{
int arr[] = {5,4,1,2,5,8};
mergeSort(arr,0,5);
for(int i=0;i<6;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;

return 0;
}
55 changes: 55 additions & 0 deletions C++/quickSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//QUICK SORT
// TIME COMPLEXITY:
// BEST CASE: n*log(n)
// WORST CASE: n^2

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

void swap(int arr[], int i, int j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}

int partition(int arr[], int l, int r)
{
int pivot=arr[r];
int i=l-1;

for(int j=l; j<r; j++)
{
if(arr[j]<pivot)
{
i++;
swap(arr,i,j);
}
}
swap(arr,i+1,r);
return i+1;
}

void quickSort(int arr[], int l, int r)
{
if(l<r)
{
int pi=partition(arr,l,r);
quickSort(arr,l,pi-1);
quickSort(arr,pi+1,r);
}
}

int main()
{
int arr[5]={5,2,6,3,1};
quickSort(arr,0,4);

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

return 0;
}
Loading