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 Cpp codes for MergeSort, QuickSort and longest Palindrome #6

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
Binary file added .DS_Store
Binary file not shown.
Binary file added code/.DS_Store
Binary file not shown.
41 changes: 41 additions & 0 deletions code/Cpp/Permutations_of_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;

void perm(string str, vector<string> &arr){
if(str.size()==1){
arr.push_back(str);
return;
}

char ch = str.back();
str.pop_back();

perm(str, arr);
int len = arr.size();
for(int i=0; i< len; i++){
string x = arr[0];
arr.erase(arr.begin());
for(int i=0; i<= x.size(); i++){
string w = x;
w.insert(w.begin() + i, ch);
arr.push_back(w);
}

}

}

int main(){
string s;
cout << "Enter the string:" << endl;
cin >> s;
vector<string> arr;
perm(s, arr);

cout << "the permutations are:" << endl;
for(int i=0; i< arr.size(); i++){
cout << arr[i] << endl;
}
}
44 changes: 44 additions & 0 deletions code/Cpp/mergeSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
using namespace std;

void mergeSort(int *input, int size){
// Write your code here
int temp;
int len1=size/2;
int len2=size-len1;


if(size<=1){
return ;
}

mergeSort(input, len1);
mergeSort(input+len1, len2);

for(int j=0; j<=len1; j++){
for(int i=0; i<size-1; i++){
if(input[i]>input[i+1]){
temp=input[i];
input[i]=input[i+1];
input[i+1]=temp;

}
}
}
return;


}

int main() {
int input[1000],length;
cin >> length;
for(int i=0; i < length; i++)
cin >> input[i];
mergeSort(input, length);
for(int i = 0; i < length; i++) {
cout << input[i] << " ";
}
}


70 changes: 70 additions & 0 deletions code/Cpp/quickSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include<iostream>
using namespace std;

int partition(int input[], int size){
int x=input[0];
int count=0;
for(int i=1; i<size; i++){
if(input[i]<=x){
count++;
}
}

input[0]=input[count];
input[count]=x;
int i=0;
int j=size-1;
while(i<count && j>count){
if(input[i]<=x){
i++;
}
if(input[j]>x){
j--;
}
else if(input[i]>x && input[j]<=x){
int temp=input[i];
input[i]=input[j];
input[j]=temp;
i++;
j--;
}
}
return count;
}

void quickSort(int input[], int size) {
/* Don't write main().
Don't read input, it is passed as function argument.
Change in the given array itself.
Taking input and printing output is handled automatically.
*/
if(size<=1){
return;
}

int count=partition(input, size);
quickSort(input, count);
quickSort(input+count+1, size-count-1);
}

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

int *input = new int[n];

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

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

delete [] input;

}



7 changes: 7 additions & 0 deletions code/addInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"place":"Ghaziabad"
},
{

"name":"Sneha Aggarwal",
"age": 20,
"place":"Delhi"
}

"name":"",
"age": ,
"place":""
Expand All @@ -20,5 +26,6 @@
"place": "Delhi"
}


]