forked from ReciHub/FunnyAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 changed file
with
67 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <ctime> | ||
#include <cstdlib> | ||
|
||
using namespace std; | ||
|
||
// Time complexity: O(n) | ||
vector<int> randomUniverse() { | ||
vector<int> universe; | ||
srand(time(NULL)); | ||
for(int i = 0; i < 100; i++) { | ||
universe.push_back(rand() % 1000); | ||
} | ||
return universe; | ||
} | ||
|
||
// Time complexity: O(n) | ||
void printUniverse(string universeName, vector<int> universe) { | ||
int universeSize = universe.size(); | ||
cout << universeName << ": [" << universe[0]; | ||
for(int i = 1; i < universeSize; i++) { | ||
cout << ", " << universe[i]; | ||
} | ||
cout << "]" << endl; | ||
} | ||
|
||
// Time complexity: O(n) | ||
bool isSorted(vector<int> array) { | ||
int size = array.size(); | ||
for(int i = 1; i < size; i++) { | ||
if(array[i - 1] > array[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
// Time complexity: O(n) | ||
void killRandomElement(vector<int>& universe) { | ||
int index = rand() % universe.size(); | ||
universe.erase(universe.begin() + index); | ||
} | ||
|
||
// Time complexity: O(n ^ 2) | ||
void killHalfUniverse(vector<int>& universe) { | ||
int halfUniverseSize = universe.size() / 2; | ||
for(int i = 0; i < halfUniverseSize; i++) { | ||
killRandomElement(universe); | ||
} | ||
} | ||
|
||
// Time complexity: O(n ^ 2 * log n) | ||
void thanosSort(vector<int>& universe) { | ||
while(!isSorted(universe)) { | ||
killHalfUniverse(universe); | ||
} | ||
} | ||
|
||
// Time complexity: O(n ^ 2 * log n) | ||
int main() { | ||
vector<int> universe = randomUniverse(); | ||
printUniverse("Universe", universe); | ||
thanosSort(universe); | ||
printUniverse("Ordered universe: ", universe); | ||
return 0; | ||
} |