Skip to content

Commit

Permalink
added day 31
Browse files Browse the repository at this point in the history
  • Loading branch information
soumya997 committed Aug 2, 2021
1 parent 1753131 commit 7573206
Show file tree
Hide file tree
Showing 18 changed files with 355 additions and 12 deletions.
18 changes: 9 additions & 9 deletions Day_21_to_30/Day_30/merge_sorted_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ int main() {
#endif

fastio();
vector<int> nums1 = {1,2,3,0,0,0};
vector<int> nums2 = {2,5,6};
vector<int> A = {1,2,3,0,0,0};
vector<int> B = {2,5,6};
int m=3;
int n=3;

Expand All @@ -72,28 +72,28 @@ int main() {
int cnt=0;
vector<int> v;
while(i<m and j<n){
if(nums1[i]<nums2[j]){
v.push_back(nums1[i]);
if(A[i]<B[j]){
v.push_back(A[i]);
i++;
}
else{
v.push_back(nums2[j]);
v.push_back(B[j]);
j++;
}
}
while(j<n){
v.push_back(nums2[j]);
v.push_back(B[j]);
j++;
}
while(i<m){
v.push_back(nums1[i]);
v.push_back(A[i]);
i++;
}
int y=0;
for(int p=0;p<v.size();p++){
nums1[p] = v[y++];
A[p] = v[y++];
}
for(auto k:nums1){
for(auto k:A){
cout<<k<<",";
}
}
54 changes: 53 additions & 1 deletion Day_31_to_40/Day_31/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
# Things I learned in: Day_31_to_40/Day_31
# Things I learned in: Day_31
**Note:** use the github provided TOC for navigaing.

# Merge sort:
> recursive, divide and conquer
1. Divide the array by the middle point
2. sort the both left and the right part recursively.
3. merge both sorted array into a single array.

![merge sort](../../imgs/merge_sort.png)

```cpp
if(s>=e){
return;
}
mergesort(arr,s,mid); --->1
mergesort(arr,mid+1,e); --->2
return merge(arr,s,e); --->3
```
(1) is called untill the base case is full filled(means s==e, at the element 10).when it full fills the base case the (2) line gets executed,it also gets called untill it hits the base case(at the element 5). Then the line (3) gets executed and both the sorted arrays get merged. And this process continues untill the stack calls are done.
![merge sort1](../../imgs/merge1.jpg)
![merge sort1](../../imgs/merge2.jpg)
![merge sort1](../../imgs/merge3.jpg)
# Quick Sort Algo:
Eg: sort this array -> [10,5,2,0,7,6,4]
1. **choose a pivot**(its besically the point where we are gonna divide the array) - last element of the array. Here pivot is 4.
2. **Partition:** we will get all the points less than pivot in the left and values greater than pivot in the right. After partition it will be like->
[2,0], [4], [5,10,7,6]
3. **then recursively sort left and right part.**
![merge sort1](../../imgs/quick2.jpg)
```cpp
//Base Case
if(s>=e){
return;
}
//Rec Case
int p = partition(a,s,e); ->// 1
quicksort(a,s,p-1); ->// 2 (sort left part of pivot)
quicksort(a,p+1,e); ->// 3 (sort right part of pivot)
```

This img explains how the partition function is working.

![merge sort1](../../imgs/quick3.png)



Empty file added Day_31_to_40/Day_31/errorf.in
Empty file.
1 change: 0 additions & 1 deletion Day_31_to_40/Day_31/inputf.in
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
5
126 changes: 126 additions & 0 deletions Day_31_to_40/Day_31/inversion_count.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// https://youtu.be/8ymiMHQPgZY
#include<bits/stdc++.h>

using namespace std;



#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
# define my_sizeof(type) ((char *)(&type+1)-(char*)(&type))
#define print_vec(x) for(auto i:x) cout<<i<<" "; // prints elements of an int vector


typedef long long ll;
typedef long long int lli;
typedef unsigned long long ull;
typedef long double lld;
// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
/*===============================================================================================================================*/

int merge(vector<int> &array,int s,int e){
int i = s;
int m = (s+e)/2;
int j = m + 1;

vector<int> temp;

int cnt = 0;

while(i<=m and j<=e){
if(array[i] < array[j]){
temp.push_back(array[i]);
i++;
}
else{
cnt += (m - i + 1);
temp.push_back(array[j]);
j++;
}
}

//copy rem elements from first array
while(i<=m){
temp.push_back(array[i++]);
}

// or copy rem elements from second array
while(j<=e){
temp.push_back(array[j++]);
}

//copy back the eleemtns from temp to original array
int k = 0 ;
for(int idx = s; idx <=e ;idx++){
array[idx] = temp[k++];
}
return cnt;
}

//sorting method
int inversion_count(vector<int> &arr,int s,int e){
//base case
if(s>=e){
return 0;
}
//rec case
int mid = (s+e)/2;
int C1 = inversion_count(arr,s,mid);
int C2 = inversion_count(arr,mid+1,e);
int CI = merge(arr,s,e);
return C1 + C2 + CI;
}


int main() {
#ifndef ONLINE_JUDGE
freopen("errorf.in", "w", stderr);
#endif

fastio();
vector<int> arr{0,5,2,3,1};

int s = 0;
int e = arr.size()-1;
cout<< inversion_count(arr,s,e) <<endl;

cout<<endl;
return 0;
}
62 changes: 62 additions & 0 deletions Day_31_to_40/Day_31/merge_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,73 @@ template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_pr
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
/*===============================================================================================================================*/

void merge_arr(vector<int> &arr,int s,int e){
int i=s;
int mid = (s+e)/2;
int j = mid+1;

vector<int> temp;
while(i<=mid and j<=e){
if(arr[i]<arr[j]){
temp.push_back(arr[i]);
i++;
}
else{
temp.push_back(arr[j]);
j++;
}
}
while(i<=mid){
temp.push_back(arr[i]);
i++;
}

while(j<=e){
temp.push_back(arr[j]);
j++;
}

int idx=0;
for(int k=s;k<=e;k++){
arr[k] = temp[idx++];
}

return;



}


void merge_sort(vector<int> & arr,int s,int e){
//base case
if(s>=e){ // this says that is there is only one element present in the arr
// you cant actually say "arr.size()==1" instade of "s>=e".
return;
}

int mid = (s+e)/2;
merge_sort(arr,s,mid);
merge_sort(arr,mid+1,e);

return merge_arr(arr,s,e);


}


int main() {
#ifndef ONLINE_JUDGE
freopen("errorf.in", "w", stderr);
#endif

fastio();
// merge sort is an recursive algorithm, and its hard
vector<int> arr{2,52,6,42,1235,51};
merge_sort(arr,0,arr.size()-1);

for(auto k:arr){
cout<<k<<" ";
}

}
Binary file added Day_31_to_40/Day_31/merge_sort.exe
Binary file not shown.
1 change: 0 additions & 1 deletion Day_31_to_40/Day_31/outputf.in
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
0 1 1 12
Loading

0 comments on commit 7573206

Please sign in to comment.