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
void bubbleSort(int *arr, int length){ | |
for(int i = 0; i < length; i++){ | |
for(int j = 0; j < length - 1 - i; j++){ | |
if(arr[j] > arr[j+1]){ | |
int temp = arr[j+1]; | |
arr[j+1] = arr[j]; | |
arr[j] = temp; | |
} | |
} | |
} |
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
void selectionSort(int *arr, int length){ | |
for(int i = 0; i < length; i++){ | |
int minIndex = i; | |
for(int j = i + 1; j < length; j++){ | |
if(arr[minIndex] > arr[j]){ | |
minIndex = j; | |
} | |
} | |
if(minIndex != i){ | |
int temp = arr[minIndex]; |
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
bool isPrime(int number){ | |
for(int i = 2; i <= sqrt((double)number); i++){ | |
if(number%i == 0) | |
return false; | |
} | |
return true; | |
} |
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
int fibSimple(int elem){ | |
if(elem < 0){ | |
return 0; | |
} | |
if(elem == 0){ | |
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
int fib(int nElem){ | |
int *arr = new int[nElem+1]; | |
arr[0] = 0; | |
arr[1] = 1; | |
for(int i = 2; i <= nElem; i++){ | |
arr[i] = arr[i-1] + arr[i-2]; | |
} | |
return arr[nElem]; | |
} |
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
int fib2(int n){ | |
if(n == 0) | |
return 0; | |
if(n == 1 || n == 2) | |
return 1; | |
return fib2(n-2) + fib2(n-1); | |
} |
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
public static int[] insertElementIntoArray(int arr[], int position, int elem){ | |
int[] tempArr = new int[arr.length+1]; | |
for (int i = 0; i < position; i++) { | |
tempArr[i] = arr[i]; | |
} | |
tempArr[position] = elem; | |
for (int i = position+1; i < tempArr.length; i++) { | |
tempArr[i] = arr[i-1]; |
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
function sumTo(n){ | |
if(n != 1){ | |
return n + sumTo(n-1); | |
} | |
else{ | |
return 1; | |
} | |
} |
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
function sumTo(n){ | |
rez = 0; | |
for(var i = 0; i <= n; i++){ | |
rez += i; | |
} | |
return rez; | |
} |
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
function sumTo(n) { | |
return (1 + n) * n / 2; | |
} |
OlderNewer