Skip to content

Instantly share code, notes, and snippets.

View misterpoloy's full-sized avatar
👽
Software Engineer

Juan P. Ortiz misterpoloy

👽
Software Engineer
View GitHub Profile
@misterpoloy
misterpoloy / BubbleSort.cpp
Created April 9, 2020 22:22
Bubble Sort implementation in cpp
#include <iostream>
void bubbleSort(int* A, int length) {
// Guarantee of iterations to sort the array
for (int k = 0; k < length - 1; k++) { // Loop K
int flag = 0;
// Actual implementation
for (int i = 0; i < length - k - 1; i++) { // i < length - 2
if (A[i] > A[i + 1]) {
// Swap positions
@misterpoloy
misterpoloy / MergeSort.cpp
Created April 11, 2020 04:21
Merge Sort in cpp
#include <iostream>
// Arreglo final
// Arreglo izquierdo
// Arreglo derecho
// tamaño del arreglo izquierdo
// tamaño del arreglo derecho
void Merge(int* A, int* L, int lefCount, int* R, int rightCount) {
int i, leftIndex, rightIndex;
@misterpoloy
misterpoloy / InsertionSort.cpp
Created April 12, 2020 05:43
Insertion Sort in c++
#include <iostream>
void insertionSort(int* A, int length) {
for (int i = 1; i < length; i++) {
int value = A[i];
int hole = i;
// Regresion con hoyos hasta el más chico
while (hole > 0 && A[hole - 1] > value) {
A[hole] = A[hole -1];
hole--;
@misterpoloy
misterpoloy / QuickSort.cpp
Created April 12, 2020 09:19
Quick Sort in cpp
#include <iostream>
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int Partition(int* A, int start, int end) {
int pivotValue = A[end]; // VALOR de pivot
@misterpoloy
misterpoloy / AlgoExpertTraverse.cpp
Last active May 31, 2020 09:49
Binary Search in cpp iterative and recursive
#include <vector>
using namespace std;
class BST {
public:
int value;
BST *left;
BST *right;
BST(int val);
@misterpoloy
misterpoloy / ElementCounter.cpp
Created April 15, 2020 07:24
#CodeChallenge count how many times does N is contained in an array in C++
#include <iostream>
int findFirstIndex(int* A, int n, int target, int position) {
int low, high;
low = 0;
high = n - 1;
int result = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (target == A[mid]) {
@misterpoloy
misterpoloy / CountRotation.cpp
Last active April 16, 2020 05:29
Count how many times a circular array has been rotated in cpp
// https://www.youtube.com/watch?v=4qjprDkJrjY&list=PL2_aWCzGMAwL3ldWlrii6YeLszojgH77j&index=6
#include <iostream>
int ArrayCountRotation(int* A, int n) {
/**
// low <= high
// A[mid] menor a next y prev
// binary search
// binary search
@misterpoloy
misterpoloy / SearchRingBinary.cpp
Created April 16, 2020 06:46
Search element in a circular sorted array log N in cpp
// https://www.youtube.com/watch?v=uufaK2uLnSI&list=PL2_aWCzGMAwL3ldWlrii6YeLszojgH77j&index=7
#include <iostream>
int SearchRing(int* A, int n, int target) {
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = low + (high - low)/2;
if (target == A[mid]) return mid; // Case 1: Found X
@misterpoloy
misterpoloy / FibonacciDynamic
Created April 19, 2020 10:37
Fibonacci using memoization in Cpp
https://www.youtube.com/watch?v=wAyrtLAeWvI&list=PL2_aWCzGMAwLz3g66WrxFGSXvSsvyfzCO&index=7
#include <iostream>
int fibDynamic(int n, int* A) {
//if (n <= 1) return n; // Toggle*
if (A[n] != -1) return A[n];
A[n] = fibDynamic(n-1, A) + fibDynamic(n-2, A);
return A[n];
}
@misterpoloy
misterpoloy / Exponesiation.cpp
Created April 19, 2020 11:05
3 ways of exponesiation, iterative, recursive and dynamic
// https://www.youtube.com/watch?v=wAyrtLAeWvI&list=PL2_aWCzGMAwLz3g66WrxFGSXvSsvyfzCO&index=7
#include <iostream>
// Big(Log N) //if (n <= 1) return n; // Toggle^1/2 * Toggle^1/2
int expoIterativeBest(int x, int n) {
if (n == 0) return 1;
if (n % 2 == 0) {
int Y = expoIterativeBest(x, n - 1);
return Y * Y;
}