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

Advalgo #2

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
32f49fa
Update Readme.md
Sandip-Kanzariya Jul 10, 2023
bc780aa
Update README.md
Sandip-Kanzariya Jul 10, 2023
dfe72da
Update README.md
Sandip-Kanzariya Jul 11, 2023
406f2b3
Update README.md
Sandip-Kanzariya Jul 12, 2023
8a841cf
Lab02 added
Sandip-Kanzariya Jul 13, 2023
585a637
Merge branch 'advalgo' of https://github.com/Sandip-Kanzariya/Advance…
Sandip-Kanzariya Jul 13, 2023
9263eac
Update README.md
Sandip-Kanzariya Jul 13, 2023
58856c2
Create README.md
Sandip-Kanzariya Jul 13, 2023
b9a6e0b
Update README.md
Sandip-Kanzariya Jul 17, 2023
b0e592f
Update README.md
Sandip-Kanzariya Jul 23, 2023
2c43ccd
Update README.md
Sandip-Kanzariya Jul 24, 2023
3ad9ea2
Update Readme.md
Sandip-Kanzariya Aug 2, 2023
d3896d4
primality test
Sandip-Kanzariya Aug 2, 2023
c11a0bb
update-lab01
Sandip-Kanzariya Aug 5, 2023
7d86c62
ha
Sandip-Kanzariya Aug 5, 2023
27c5d66
ha
Sandip-Kanzariya Aug 5, 2023
0c035de
Lab01
Sandip-Kanzariya Aug 5, 2023
46d13e6
Lab02
Sandip-Kanzariya Aug 5, 2023
34e9173
Lab03
Sandip-Kanzariya Aug 5, 2023
7355db2
Lab01
Sandip-Kanzariya Aug 8, 2023
c45069b
Lab04
Sandip-Kanzariya Aug 8, 2023
388d0b0
Lab04
Sandip-Kanzariya Aug 8, 2023
0f17f07
Lab05
Sandip-Kanzariya Aug 13, 2023
ebcc2bc
Lab05
Sandip-Kanzariya Aug 13, 2023
b77ef40
Lab05
Sandip-Kanzariya Aug 13, 2023
d3ab8f8
Lab08
Sandip-Kanzariya Oct 24, 2023
5eae19a
Lab09
Sandip-Kanzariya Oct 24, 2023
7267839
Lab05
Sandip-Kanzariya Oct 24, 2023
b2eaa0b
Lab07
Sandip-Kanzariya Oct 24, 2023
99e50f6
algorithms added
Sandip-Kanzariya Oct 24, 2023
d201d62
Lab06
Sandip-Kanzariya Oct 24, 2023
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
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
#include <bits/stdc++.h>


using namespace std;

int c = 0, sw = 0;
int getRandom(int p, int r) {
int getRandom(int p, int r)
{
srand(time(0));
return (rand() % (r - p + 1)) + p;
}

int c = 0, sw = 0;
int partition(vector<int> &v, int l, int h)
{

int partition(vector <int> &v, int l, int h){

// Take last element as pivot
// Take last element as pivot
int pivot = v[h];

int i = l;
for(int j = l; j < h;j++){
for (int j = l; j < h; j++)
{

c++;
//
if(v[j] <= pivot){
//
if (v[j] <= pivot)
{
sw++;
swap(v[i], v[j]);
i++;
Expand All @@ -30,10 +33,12 @@ int partition(vector <int> &v, int l, int h){

return i;
}
void quickSort(vector <int> &v, int l, int h){

void quickSort(vector<int> &v, int l, int h)
{

if(l < h){
if (l < h)
{
int random = getRandom(l, h);
swap(v[random], v[h]);

Expand All @@ -42,35 +47,34 @@ void quickSort(vector <int> &v, int l, int h){
quickSort(v, q + 1, h);
}


return;
}

int main()
{

int main(){

// vector <int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// vector <int> v = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
// vector <int> v = {7, 5, 6, 10, 9, 2, 1, 8, 4, 3};
// vector <int> v = {4, 3, 6, 8, 5, 9, 20, 50, 12, 30, 432, 12, 69, 29, 40, 50, 10, 4, 23, 34, 45, 45, 24, 89, 799, 45, 80, 50, 10, 30, 43,54,65,76,87, 98, 21, 32, 43, 78, 98, 57, 29, 91, 34, 54, 64, 19, 82, 87, 65};

// vector <int> v = {1, 2, 3, 4, 5, 6};
// vector <int> v = {1, 2, 3, 4, 5, 6};

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

quickSort(v, 0, n - 1);
cout << "Sorted : ";
for(auto k : v) cout << k << " ";
for (auto k : v)
cout << k << " ";

cout << "\nComparisions : " << c << "\n";
cout << "Swaps : " << sw << "\n";

return 0;
}

73 changes: 73 additions & 0 deletions Labs/Lab01/2_Kth_Smallest_Element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <bits/stdc++.h>
using namespace std;


int getRandom(int p, int r) {
srand(time(0));
return (rand() % (r - p + 1)) + p;
}


int partition(vector <int> &v, int l, int h){


// Take last element as pivot
int pivot = v[h];


int i = l;
for(int j = l; j < h;j++){


if(v[j] <= pivot){

swap(v[i], v[j]);
i++;
}
}

swap(v[i], v[h]);


return i;
}


int find(vector <int> &v, int l, int h, int k){


if(l < h){
int random = getRandom(l, h);
swap(v[random], v[h]);
int q = partition(v, l, h);


if(k - 1 == q) return v[q];


if(k - 1 < q){
return find(v, l, q - 1, k);
}
else{
return find(v, q + 1, h, k - q - 1); //
}
}
return 0;
}




int main(){



vector <int> v = {10, 9, 1, 2, 6, 99, 11};

int k = 4;
int n = v.size();

cout << find(v, 0, n - 1, k);

return 0;
}
59 changes: 58 additions & 1 deletion Labs/Lab01/Readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
### Randomized Quick Sort
## Randomized Quick Sort
---
>### Randomizer
```c++
// Generate Random number in between p and r :

int getRandom(int l, int h) {
srand(time(0));
return (rand() % (h - l + 1)) + l;
}
```
>### Partition Function : last element as pivot
```c++
int partition(vector <int> &v, int l, int h){

// Take last element as pivot
int pivot = v[h];

int i = l;
for(int j = l; j < h;j++){

if(v[j] <= pivot){
swap(v[i], v[j]);
i++;
}
}
swap(v[i], v[h]);

return i;
}
```
>### Randomized Quick Sort
```c++
void quickSort(vector <int> &v, int l, int h){

if(l < h){
// Swap last element of array with any other element of array
int random = getRandom(l, h);
swap(v[random], v[h]);

int q = partition(v, l, h);
quickSort(v, l, q - 1);
quickSort(v, q + 1, h);
}

return;
}
```

>### Output :
For n = 10000
|Run|Output|
|---|---|
|1.|![Alt text](images/image-0.png) |
|2.|![Alt text](images/image-1.png)|

|Simple Quick Sort|Randomized Quick Sort|
|---|---|
Binary file added Labs/Lab01/images/image-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Labs/Lab01/images/image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 91 additions & 0 deletions Labs/Lab02/1_Primality_Testing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <bits/stdc++.h>

using namespace std;

using USLLI = unsigned long long int;

// Generate Random number in between p and r :
USLLI getRandom(USLLI p, USLLI r)
{
srand(time(0));
return (rand() % (r - p + 1)) + p;
}

USLLI gcd(USLLI a, USLLI b)
{

while (b != 0)
{
USLLI res = a % b;
a = b;
b = res;
}

return a;
}

USLLI power(USLLI a, USLLI x, USLLI n)
{
USLLI res = 1;

while (x > 0)
{

// Odd

if (x % 2 == 1)
{
res = (res * a) % n;
}

x = x / 2;

a = (a * a) % n;
}

return res;
}

// as k increases, probability of correct ans increases
int k = 100;

bool isPrime(USLLI n)
{

if (n <= 1 || n == 4)
return false;

if (n <= 3)
return true;

while (k > 0)
{

USLLI a = getRandom(2, n - 2);

if (gcd(n, a) != 1)
return false;

if (power(a, n - 1, n) != 1)
return false;

k--;
}

return true;
}
int main()
{

USLLI n;
cout << "Enter Number for Primality Testing : ";
cin >> n;
// 1000000007

if (isPrime(n))
cout << n << " is Prime";
else
cout << n << " is Composite";

return 0;
}
50 changes: 50 additions & 0 deletions Labs/Lab02/2_Probability_Of_liars.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <bits/stdc++.h>
using namespace std;
using USLLI = unsigned long long int;

USLLI power(USLLI a, USLLI x, USLLI n)
{
USLLI res = 1;

while (x > 0)
{

// Odd

if (x % 2 == 1)
{
res = (res * a) % n;
}

x = x / 2;

a = (a * a) % n;
}

return res;
}

int main()
{

USLLI n;
cout << "Enter Number : ";
cin >> n;

USLLI count = 0;

for (USLLI i = 2; i <= n - 2; i++)
{
if (power(i, n - 1, n) == 1)
{
count++;
}
}

cout << "Fermate's Liars : " << count << "\n";
cout << "Fermate's Witnesses : " << n - 3 - count << "\n";

cout << "Probability of Fermate's Liars: " << (double)count / (n - 3);

return 0;
}
Loading