From 4b4691b5512377c85472d333f6168e1fe3d7999e Mon Sep 17 00:00:00 2001 From: SnehaAgg0212 Date: Wed, 14 Oct 2020 14:52:30 +0530 Subject: [PATCH] Added Cpp codes for MergeSort, QuickSort and longest Palindrome --- .DS_Store | Bin 0 -> 6148 bytes code/.DS_Store | Bin 0 -> 6148 bytes code/Cpp/Permutations_of_string.cpp | 41 ++++++++++++++++ code/Cpp/mergeSort.cpp | 44 +++++++++++++++++ code/Cpp/quickSort.cpp | 70 ++++++++++++++++++++++++++++ code/addInfo.json | 6 +-- 6 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 .DS_Store create mode 100644 code/.DS_Store create mode 100644 code/Cpp/Permutations_of_string.cpp create mode 100644 code/Cpp/mergeSort.cpp create mode 100644 code/Cpp/quickSort.cpp diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a686f2290551ae918caa69039e4fe2f0a09cd46e GIT binary patch literal 6148 zcmeHK!A=4(5N!dqV2m70nIsDWLV$R@ZYu((mg>~H8F`33%t zGi@OfH1TAN%p}t{ozArB+fAosjPb@Os4`Y$j0sT0Odgsq1jkVqBx5~@9Ov+pVc+j5 zANp?#(d77x4B)rRu^XmXz&fA5pWp9=!?;p;=b3D7Wpz!6f+%bhuHCLm+}Mjp!-hAw z#NMeFcYSN0Db<_Vo_`*8l2&o+NJX(1MxB994ucMaTwR1wppu3fMZr+d@*>|V zmd4}Vdc7>Gwf#w1j_dVGS?<*iCX>9_-l-m*wC^4s$4`^z7sD!tA5hDt#VNdkG27t1 zJBWHJx&_}fei}ze3=jjvz!EWFk3g}$M0=*K5(C7*<e-L4qPW7E^=z=zs>Vk2sz~ zL;)M$5{S~GV=*-dBOu(Q0-98ApBUVvgJ0S>$6{*Gq%*EphI#DD+`dq_ULE{WhcoUN zq?Q;U2Id*anr;#6|MB;hKvlK9N hDTY`q#U)TJ;Fo9sIu=ud-~pkFfTn>OV&GR9_y8LNP>uiq literal 0 HcmV?d00001 diff --git a/code/.DS_Store b/code/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..ff1ed848406d7dfa22778d2658f460bfbb826bc1 GIT binary patch literal 6148 zcmeHK!EO^V5FIxQby6X6Kx&V^^iU~>gaSDsEonHE6Vjq`ps3yLM$Jl_^(xskg0N_R zgZ6KF>xC;H(+e~9P}+uagsAdJ<2Uwryq+i7-T?s7o5mf0CICoOLa%|%AB6m*3$nr^ zlSgCmr9o2q?C7irlWZ_H!AU&8Df{n}EY`E0 zo@VjHtP@y=lyzA@Xl~5sPrKcg>TEwRTWa3zwp;4i_KUKt%k|C9%iY7#r_b{*<=1bP zPl4Buk;ja`;1mr`FFo!AM|yPqVq(uo_xpTt$?umJTjkmLtKV@W9sk9f5ARy_Bp7F@ z&W>><+MNe)AcPYL?<>t$3EoN^b9b_o4Dg|mYfHI1w<*VfW8i->K=*?}CG;69i{|RU z#*_ev9u6Bpn`#M)QH(xgWf46n!iFN+P{AdJu;Dnac%IK#S+wCGxcCtKWx*ASFki>@ zm6;C0x9D2OfMeh$12x-i()<7V?)v{`kn1@H90T`?0paz7eh*XPw{;;odTSNxAu0*Y ot1OxjZ1_1g20g_as7BDI$UyWND~mXS;(i1S4X$wv{8I*g0wtHM5dZ)H literal 0 HcmV?d00001 diff --git a/code/Cpp/Permutations_of_string.cpp b/code/Cpp/Permutations_of_string.cpp new file mode 100644 index 0000000..917cc4f --- /dev/null +++ b/code/Cpp/Permutations_of_string.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +using namespace std; + +void perm(string str, vector &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 arr; + perm(s, arr); + + cout << "the permutations are:" << endl; + for(int i=0; i< arr.size(); i++){ + cout << arr[i] << endl; + } +} diff --git a/code/Cpp/mergeSort.cpp b/code/Cpp/mergeSort.cpp new file mode 100644 index 0000000..b8e736f --- /dev/null +++ b/code/Cpp/mergeSort.cpp @@ -0,0 +1,44 @@ +#include +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; iinput[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] << " "; + } +} + + diff --git a/code/Cpp/quickSort.cpp b/code/Cpp/quickSort.cpp new file mode 100644 index 0000000..01dd2a6 --- /dev/null +++ b/code/Cpp/quickSort.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +int partition(int input[], int size){ + int x=input[0]; + int count=0; + for(int i=1; icount){ + 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; + +} + + + diff --git a/code/addInfo.json b/code/addInfo.json index a54c712..5bb1be6 100644 --- a/code/addInfo.json +++ b/code/addInfo.json @@ -10,9 +10,9 @@ "place":"Ghaziabad" }, { - "name":"", - "age": , - "place":"" + "name":"Sneha Aggarwal", + "age": 20, + "place":"Delhi" } ]