Skip to content

Instantly share code, notes, and snippets.

View dvt32's full-sized avatar

Dimitar Trifonov dvt32

View GitHub Profile
@dvt32
dvt32 / CODETIPS-5-Example-1.cpp
Last active November 26, 2016 17:14
CODETIPS #5 - Example 1
void someFunction() {
int myVar = 4;
}
void anotherFunction() {
int myVar = 5;
std::cout << myVar << std::endl;
}
@dvt32
dvt32 / CODETIPS-5-Example-2.cpp
Created November 26, 2016 17:18
CODETIPS #5 - Example 2
int main() {
int a = 1;
int b = 2;
int c = 3;
a = a + 1;
b = b + 1;
return 0;
}
@dvt32
dvt32 / ALGORITHMO-1-Euclidean-Algorithm-Implementation.java
Created December 2, 2016 11:56
ALGORITHMO #1 - Euclidean Algorithm Implementation
import java.util.Scanner;
public class Solution {
public static int getGreatestCommonDivisor(int a, int b) {
if (a == b) {
return a;
}
while (a != b) {
if (a > b) {
@dvt32
dvt32 / ALGORITHMO-9-Euclidean-Algorithm-Implementation.java
Last active September 3, 2022 08:35
ALGORITHMO #9 - Euclidean Algorithm Implementation
public class Solution {
static int getGreatestCommonDivisor(int a, int b) {
while (b != 0) {
int temp = a;
a = b;
b = temp % b;
}
int greatestCommonDivisor = a;
return greatestCommonDivisor;
@dvt32
dvt32 / ALGORITHMO-10-Binary-GCD-Algorithm-Implementation.java
Last active July 26, 2020 12:29
ALGORITHMO #10 - Binary GCD Algorithm Implementation
public class Solution {
static int getGreatestCommonDivisor(int u, int v) {
int greatestCommonDivisor = -1;
/* 1.
* - НОД(0, v) = v, тъй като 0 дели всичко, а v е най-голямото число, което дели v.
* - Aналогично, НОД(u, 0) = u.
* - НОД(0, 0) е неопределен.
*/
@dvt32
dvt32 / ALGORITHMO-10-Least-Common-Multiple-Algorithm-Implementation.java
Last active July 26, 2020 12:30
ALGORITHMO #10 - Least Common Multiple Algorithm Implementation
public class Solution {
static int getGreatestCommonDivisor(int a, int b) {
while (b != 0) {
int temp = a;
a = b;
b = temp % b;
}
int greatestCommonDivisor = a;
return greatestCommonDivisor;
@dvt32
dvt32 / ALGORITHMO-11-Sieve-of-Eratosthenes-Algorithm-Implementation.java
Last active January 14, 2017 21:46
ALGORITHMO #11 - Sieve of Eratosthenes Algorithm Implementation
public class Solution {
static void sieveOfEratosthenes(int n) {
boolean sieve[] = new boolean[n+1];
int i = 2, j = 0;
while (i <= n) {
if (sieve[i] == false) {
System.out.print(i + " ");
j = i * i;
@dvt32
dvt32 / ALGORITHMO-12-Fast-Multiplication-Algorithm-Implementation.java
Last active July 27, 2020 12:03
ALGORITHMO #12 - Fast Multiplication Algorithm Implementation
/* Optimization ideas:
- use less data structures
- use less loops etc.
*/
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Solution {
@dvt32
dvt32 / ALGORITHMO-13-Selection-Sort-Implementation.java
Last active July 27, 2020 21:13
ALGORITHMO #13 - Selection Sort Implementation
public class SelectionSort {
public static void selectionSort(int[] arr) {
int lastElementIndex = arr.length - 1;
for (int subarrayStartIndex = 0; subarrayStartIndex < lastElementIndex; ++subarrayStartIndex) {
int minElementIndex = subarrayStartIndex;
for (int i = subarrayStartIndex + 1; i < arr.length; ++i) {
if (arr[i] < arr[minElementIndex]) {
minElementIndex = i;
@dvt32
dvt32 / ALGORITHMO-14-Bubble-Sort-Implementation.java
Last active July 28, 2020 10:33
ALGORITHMO #14 - Bubble Sort Implementation
public class BubbleSort {
public static void bubbleSort(int[] arr) {
boolean stillSwappingElements = true;
int j = 0;
while (stillSwappingElements) {
stillSwappingElements = false;
j++;
for (int i = 0; i < (arr.length - j); ++i) {
if (arr[i] > arr[i+1]) {