-
Sorting:
- Bubble, or sinking sort
- Seeks for the lowers value, swaps in the inner level
- Means that every iteration would swap values
- Seeks for the lowers value, swaps in the inner level
- Selection:
- Seeks for the lowest value, swaps only when found AT the outter level
- Means that not all iteration would swap values
- Seeks for the lowest value, swaps only when found AT the outter level
- Bubble, or sinking sort
-
Fibonacci
- You got it :-)
Last active
January 30, 2017 16:51
-
-
Save kinncj/793896fc752e895a68ce92c28ada0a8b to your computer and use it in GitHub Desktop.
Common interview questions
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
<?php | |
require_once "sorting.php"; | |
require_once "fibonacci.php"; | |
/** | |
* Just a support function to run the fibonacci | |
*/ | |
function fibo($total) | |
{ | |
$time_start = microtime(true); | |
for ($counter = 0; $counter <= $total; $counter++) { | |
printf("Fibonacci of %f is: %d\n", $counter, fibonacci($counter)); | |
} | |
$time_end = microtime(true); | |
$time = $time_end - $time_start; | |
printf("Fibo spent: %F seconds", $time); | |
} | |
$input = [255,1,22,3,45,5]; | |
$output = [ | |
'bubble' => bubble($input), | |
'selection' => selection($input), | |
]; | |
var_dump($output); | |
fibo(10); |
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
<?php | |
/** | |
* If base numbers, 1 or 0, return it | |
* Return the value of the current (number - 1) + (current number - 2) | |
*/ | |
function fibonacci($number) | |
{ | |
if ($number === 0 || $number === 1) { | |
return $number; | |
} | |
return fibonacci($number - 1) + fibonacci($number - 2); | |
} |
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
<?php | |
/** | |
* First loop goes forward | |
* Second loop goes backwards | |
* compares if the previous position is less than or equal the current one | |
* if not, swap positions | |
*/ | |
function bubble(array $array) | |
{ | |
$time_start = microtime(true); | |
$n = count($array); | |
for($counter = 1; $counter < $n; $counter++) { | |
for ($current = $n -1; $current >= $counter; $current--) { | |
$next = $current - 1; | |
if ($array[$next] <= $array[$current]) { | |
continue; | |
} | |
$swap = $array[$next]; | |
$array[$next] = $array[$current]; | |
$array[$current] = $swap; | |
} | |
} | |
$time_end = microtime(true); | |
$time = $time_end - $time_start; | |
printf("Bubble spent: %F seconds\n", $time); | |
return $array; | |
} | |
/** | |
* First loop goes forward | |
* Second loop goes forward | |
* compares if the next position is less than or equal the current one | |
* if yes, swap positions | |
*/ | |
function selection(array $array) | |
{ | |
$time_start = microtime(true); | |
$n = count($array); | |
for ($current = 0; $current < $n; $current++) { | |
$lowestValueIndex = $current; | |
$lowestValue = $array[$current]; | |
for ($next = $current + 1; $next < $n; $next++) { | |
if ($array[$next] < $lowestValue) { | |
$lowestValueIndex = $next; | |
$lowestValue = $array[$next]; | |
} | |
} | |
$array[$lowestValueIndex] = $array[$current]; | |
$array[$current] = $lowestValue; | |
} | |
$time_end = microtime(true); | |
$time = $time_end - $time_start; | |
printf("Selection spent: %F seconds\n", $time); | |
return $array; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment