Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. </head>
  5. <body>
  6. <form method="POST" active="" align="center">
  7. <div>Enter number:</div><input class="textplace" name="num" type="number">
  8. <input type="submit" name="show">
  9. </form>
  10.  
  11.  
  12. <?php
  13.  
  14. if(isset($_POST["show"])){
  15. $array = array();
  16. $size = $_POST['num'];
  17. for($i = 0; $i < $size; $i++){
  18. $array[$i] = rand(1, 100);
  19. }
  20. function insertSort(array &$array)
  21. {
  22.  
  23. for($j=1;$j<$size;$j++){
  24. $key = $array[$j];
  25. $i=$j-1;
  26. while($i>=0 && $array[$i]>$key){
  27. $array[$i+1]=$array[$i];
  28. $array[$i]=$key;
  29. $i--;
  30. }
  31. }
  32. }
  33. // $arr[$i] = rand(1, 100);
  34. // }
  35.  
  36. // function bubble_sort($arr) {
  37. // $size = count($arr);
  38. // for ($i=0; $i<$size; $i++) {
  39. // for ($j=0; $j<$size-1-$i; $j++) {
  40. // if ($arr[$j+1] < $arr[$j]) {
  41. // swap($arr, $j, $j+1);
  42. // }
  43. // }
  44. // }
  45. // return $arr;
  46. // }
  47.  
  48. // function swap(&$arr, $a, $b) {
  49. // $tmp = $arr[$a];
  50. // $arr[$a] = $arr[$b];
  51. // $arr[$b] = $tmp;
  52. // }
  53.  
  54.  
  55.  
  56. print("<b>Before sorting: </b><br>");
  57. for($i = 0; $i < $_POST['num']; $i++){ echo " ➝ ".$array[$i]; }
  58.  
  59. $time_start = microtime(true);
  60. $array = insertSort($array);
  61. $time_end = microtime(true);
  62. $execution_time = ($time_end - $time_start);
  63. echo '<b><br>Total Execution Time:</b> '.$execution_time.' Secs <br>';
  64. print("<b>After sorting: </b><br>");
  65. for($i = 0; $i < $_POST['num']; $i++){ echo " ➝ ".$array[$i];}
  66. }
  67. ?>
  68. </body>
  69. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement