Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Insertion Sort in PHP #1596

Merged
merged 3 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions archive/p/php/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Welcome to Sample Programs in PHP!
- [Palindrome Word in PHP][11]
- [ROT13 in PHP][15]
- [String Reverse in PHP][10]
- [Insertion Sort in PHP][17]

## Fun Facts

Expand Down Expand Up @@ -47,3 +48,4 @@ Welcome to Sample Programs in PHP!
[14]: https://www.w3resource.com/php-exercises/searching-and-sorting-algorithm/searching-and-sorting-algorithm-exercise-17.php
[15]: https://github.com/TheRenegadeCoder/sample-programs/issues/1530
[16]: https://github.com/TheRenegadeCoder/sample-programs/issues/1533
[17]: https://github.com/TheRenegadeCoder/sample-programs/issues/1524
21 changes: 21 additions & 0 deletions archive/p/php/insertion-sort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

function insertion_Sort($my_array)
{
for($i=0;$i<count($my_array);$i++){
$val = $my_array[$i];
$j = $i-1;
while($j>=0 && $my_array[$j] > $val){
$my_array[$j+1] = $my_array[$j];
$j--;
}
$my_array[$j+1] = $val;
}
return $my_array;
}
$test_array = array(3, 0, 2, 5, -1, 4, 1);
echo "Original Array :\n";
echo implode(', ',$test_array );
echo "\nSorted Array :\n";
print_r(insertion_Sort($test_array));
?>