├── .gitignore ├── searching └── binary-search.php └── sorting ├── bubble-sort-algorithm.php ├── insertion-sort-algorithm.php ├── merge-sort-algorithm.php └── selection-sort-algorithm.php /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /searching/binary-search.php: -------------------------------------------------------------------------------- 1 | 0) { 29 | $right = $haystack_middle; 30 | $haystack_middle = $left + round(($right-$left)/2, 0, PHP_ROUND_HALF_DOWN); 31 | } else { 32 | $left = $haystack_middle; 33 | $haystack_middle = $haystack_middle + round(($right-$left)/2, 0, PHP_ROUND_HALF_DOWN); 34 | } 35 | } 36 | 37 | if (strcmp($haystack[$haystack_middle],$needle)==0) { 38 | echo "Found on position $haystack_middle\n"; 39 | } else { 40 | echo "Nothing found\n"; 41 | } 42 | 43 | ?> -------------------------------------------------------------------------------- /sorting/bubble-sort-algorithm.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /sorting/insertion-sort-algorithm.php: -------------------------------------------------------------------------------- 1 | =0; $j--) { 28 | 29 | sleep(1); // this is just so you can see the work in progress 30 | $comparisons++; 31 | 32 | // If the left neighbor is actually bigger than our current bit, let's swap them 33 | if ($data[$j]>$data[$j+1]) { 34 | 35 | echo "$data[$j] is bigger than ".$data[$j+1]."\n"; 36 | $data = swappositions($data, $j, $j+1); 37 | $swappings++; // count how often we're swapping things around 38 | 39 | } else { // If the left neighbor is smaller, all is well & we'll skip to the next position 40 | break; 41 | } 42 | 43 | } 44 | 45 | } 46 | 47 | // Let's output some friedly info on how we've done at the end 48 | echo "\nI compared data elements $comparisons times.\n"; 49 | echo "I executed $swappings swap-operations to sort all data!\n"; 50 | echo "Look at the fantastic result:\n"; 51 | print_r($data); 52 | 53 | 54 | /** 55 | * ############################################################################ 56 | * Our toolbelt 57 | */ 58 | 59 | function swappositions($data, $left, $right) { 60 | $backup_old_data_right_value = $data[$right]; 61 | $data[$right] = $data[$left]; 62 | $data[$left] = $backup_old_data_right_value; 63 | return $data; 64 | } 65 | 66 | 67 | ?> -------------------------------------------------------------------------------- /sorting/merge-sort-algorithm.php: -------------------------------------------------------------------------------- 1 | 1) { 28 | 29 | // Find out the middle of the current data set and split it there to obtain to halfs 30 | $data_middle = round(count($data)/2, 0, PHP_ROUND_HALF_DOWN); 31 | // and now for some recursive magic 32 | $data_part1 = mergesort(array_slice($data, 0, $data_middle)); 33 | $data_part2 = mergesort(array_slice($data, $data_middle, count($data))); 34 | 35 | // Setup counters so we can remember which piece of data in each half we're looking at 36 | $counter1 = $counter2 = 0; 37 | 38 | // iterate over all pieces of the currently processed array, compare size & reassemble 39 | for ($i=0; $i -------------------------------------------------------------------------------- /sorting/selection-sort-algorithm.php: -------------------------------------------------------------------------------- 1 | --------------------------------------------------------------------------------