Skip to content
This repository was archived by the owner on Oct 16, 2021. It is now read-only.
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
4 changes: 4 additions & 0 deletions Bitwise operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ int main() {

// The result is 00000100
cout<<"b >> 1 "<<"= " << (b >> 1 )<<endl;

cout<<"a % b "<<"= " << (a % b )<<endl;

cout<<"a / b "<<"= " << (a / b )<<endl;

return 0;
}
Binary file added Bitwise operations.exe
Binary file not shown.
201 changes: 120 additions & 81 deletions Infix-prefix.cpp
Original file line number Diff line number Diff line change
@@ -1,83 +1,122 @@
// Infix to Prefix using Stack in C++
#include <bits/stdc++.h>
using namespace std;
bool isOperator(char c)
{
return (!isalpha(c) && !isdigit(c));
}
int preceed(char a)
{
if(a == '+' || a == '-')
return 1;
else if(a == '*' || a == '/')
return 2;
else if(a == '^')
return 3;
else
return -1;
#include <bits/stdc++.h>
using namespace std;

bool isOperator(char c)
{
return (!isalpha(c) && !isdigit(c));
}

int getPriority(char C)
{
if (C == '-' || C == '+')
return 1;
else if (C == '*' || C == '/')
return 2;
else if (C == '^')
return 3;
return 0;
}
string infixToPostfix(string infix)
{
infix = '(' + infix + ')';
stack<char> char_stack;
string output;
for (int i = 0; i < infix.size(); i++) {

if (isalpha(infix[i]) || isdigit(infix[i]))
output += infix[i];

else if (infix[i] == '(')
char_stack.push('(');

else if (infix[i] == ')') {

while (char_stack.top() != '(') {
output += char_stack.top();
char_stack.pop();
}

char_stack.pop();
}

else {

if (isOperator(char_stack.top())) {
while (preceed(infix[i]) <= preceed(char_stack.top())) {
output += char_stack.top();
char_stack.pop();
}
char_stack.push(infix[i]);
}
}
}
return output;
}

string infixToPrefix(string infix)
{

reverse(infix.begin(), infix.end());
for (int i = 0; i < infix.size(); i++) {

if (infix[i] == '(') {
infix[i] = ')';
i++;
}
else if (infix[i] == ')') {
infix[i] = '(';
i++;
}
}

string prefix = infixToPostfix(infix);
reverse(prefix.begin(), prefix.end());
return prefix;
}


int main()
{
string s = ("(a+(b/c)*(d^e)-f)");
cout << infixToPrefix(s) << std::endl;
return 0;

string infixToPostfix(string infix)
{
infix = '(' + infix + ')';
int l = infix.size();
stack<char> char_stack;
string output;

for (int i = 0; i < l; i++) {

// If the scanned character is an
// operand, add it to output.
if (isalpha(infix[i]) || isdigit(infix[i]))
output += infix[i];

// If the scanned character is an
// ‘(‘, push it to the stack.
else if (infix[i] == '(')
char_stack.push('(');

// If the scanned character is an
// ‘)’, pop and output from the stack
// until an ‘(‘ is encountered.
else if (infix[i] == ')') {
while (char_stack.top() != '(') {
output += char_stack.top();
char_stack.pop();
}

// Remove '(' from the stack
char_stack.pop();
}

// Operator found
else
{
if (isOperator(char_stack.top()))
{
if(infix[i] == '^')
{
while (getPriority(infix[i]) <= getPriority(char_stack.top()))
{
output += char_stack.top();
char_stack.pop();
}

}
else
{
while (getPriority(infix[i]) < getPriority(char_stack.top()))
{
output += char_stack.top();
char_stack.pop();
}

}

// Push current Operator on stack
char_stack.push(infix[i]);
}
}
}
return output;
}

string infixToPrefix(string infix)
{
/* Reverse String
* Replace ( with ) and vice versa
* Get Postfix
* Reverse Postfix * */
int l = infix.size();

// Reverse infix
reverse(infix.begin(), infix.end());

// Replace ( with ) and vice versa
for (int i = 0; i < l; i++) {

if (infix[i] == '(') {
infix[i] = ')';
i++;
}
else if (infix[i] == ')') {
infix[i] = '(';
i++;
}
}

string prefix = infixToPostfix(infix);

// Reverse postfix
reverse(prefix.begin(), prefix.end());

return prefix;
}

// Driver code
int main()
{
string s = ("x+y*z/w+u");
cout << infixToPrefix(s) << std::endl;
return 0;
}
Binary file added Infix-prefix.exe
Binary file not shown.
135 changes: 84 additions & 51 deletions MergeSort.cpp
Original file line number Diff line number Diff line change
@@ -1,57 +1,90 @@
#include<bits/stdc++.h>
#include <iostream>
using namespace std;

void merge(int arr[],int mid,int left,int right){
int n1 = mid - left + 1;
int n2 = right - mid;
int leftArr[n1],rightArr[n2];

for(int i=0;i<n1;i++){
leftArr[i]=arr[left+i];
}

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

int i{},j{},k = left;
while(i<n1 && j<n2){
if(leftArr[i]<=rightArr[j]){
arr[k++]=leftArr[i++];
}else {
arr[k++]=rightArr[j++];

// Merges two subarrays of array[].
// First subarray is arr[begin..mid]
// Second subarray is arr[mid+1..end]
void merge(int array[], int const left, int const mid, int const right)
{
auto const int subArrayOne = mid - left + 1;
auto const int subArrayTwo = right - mid;

// Create temp arrays
auto *leftArray = new int[subArrayOne],
*rightArray = new int[subArrayTwo];

// Copy data to temp arrays leftArray[] and rightArray[]
for (auto int i = 0; i < subArrayOne; i++)
leftArray[i] = array[left + i];
for (auto int j = 0; j < subArrayTwo; j++)
rightArray[j] = array[mid + 1 + j];

auto int indexOfSubArrayOne = 0, // Initial index of first sub-array
indexOfSubArrayTwo = 0; // Initial index of second sub-array
int indexOfMergedArray = left; // Initial index of merged array

// Merge the temp arrays back into array[left..right]
while (indexOfSubArrayOne < subArrayOne && indexOfSubArrayTwo < subArrayTwo) {
if (leftArray[indexOfSubArrayOne] <= rightArray[indexOfSubArrayTwo]) {
array[indexOfMergedArray] = leftArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
}
else {
array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo];
indexOfSubArrayTwo++;
}
indexOfMergedArray++;
}

while(i<n1)
arr[k++]=leftArr[i++];

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

void mergeSort(int arr[],int left,int right){
if(left>=right)
return;

int mid = left + (right-left)/2;

mergeSort(arr,left,mid);
mergeSort(arr,mid+1,right);
merge(arr,mid,left,right);
}

int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
// Copy the remaining elements of
// left[], if there are any
while (indexOfSubArrayOne < subArrayOne) {
array[indexOfMergedArray] = leftArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
indexOfMergedArray++;
}
mergeSort(arr,0,n-1);
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
// Copy the remaining elements of
// right[], if there are any
while (indexOfSubArrayTwo < subArrayTwo) {
array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo];
indexOfSubArrayTwo++;
indexOfMergedArray++;
}
cout<<"\n";
}

// begin is for left index and end is
// right index of the sub-array
// of arr to be sorted */
void mergeSort(int array[], int const begin, int const end)
{
if (begin >= end)
return; // Returns recursively

auto int mid = begin + (end - begin) / 2;
mergeSort(array, begin, mid);
mergeSort(array, mid + 1, end);
merge(array, begin, mid, end);
}

// UTILITY FUNCTIONS
// Function to print an array
void printArray(int A[], int size)
{
for (auto int i = 0; i < size; i++)
cout << A[i] << " ";
}

// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
auto int arr_size = sizeof(arr) / sizeof(arr[0]);

cout << "Given array is \n";
printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

cout << "\nSorted array is \n";
printArray(arr, arr_size);
return 0;
}
}
Binary file added MergeSort.exe
Binary file not shown.
Loading