-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
355 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
5 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
0 1 1 12 | ||
Oops, something went wrong.