pastebin

Paste Search Dynamic
Recent pastes
program to find the common ele
  1. <php?
  2. for ( int i = 0 ; i < n ; i += 4 ) {
  3.  
  4.  
  5. }
  6. ?>
  7. <?php
  8. //program to find the common elements of the two array
  9. //here we have to array A and B from which w have to find the common element
  10. //first we sort then using merge sort and after then for traversing through
  11. //the array in one iteration we can find the comman elements the given array
  12. //this is an inspace algorithm meansno extra space is needed
  13.  
  14. //best case time complexity=O(nlogn)
  15. //O(nlogn)-> for sorting
  16. //O(n)-> for while loop to find comman element
  17.  
  18. //average case time complexity=O(nlogn)
  19. //O(nlogn)-> for sorting
  20. //O(n)-> for while loop to find comman element
  21.  
  22. //worst case time complexity =O(nlogn)
  23. //O(nlogn)-> for sorting
  24. //O(n)-> for while loop to find comman element
  25.  
  26.  
  27.  
  28. $commonArray=array();
  29. $A=array(3,4,5,6,7,8,9,10,36,58,27,48);
  30. $B=array(3,10,4,5,6,8,12,24,37,27,50);
  31. sort($A);
  32. sort($B);
  33. $size1=sizeof($A);
  34. $size2=sizeof($B);
  35. $counter1=0;
  36. $counter2=0;
  37. while(($counter1< $size1) && ($counter2)<($size2))//traversing through the array
  38. {
  39.  
  40. if ($A[$counter1] == $B[$counter2])
  41. {
  42.       array_push($commonArray,$A[$counter1]);  //to enter comman element in the output array
  43.       $counter1=$counter1+1;
  44.       $counter2=$counter2+1;
  45. }
  46.     else if ($A[$counter1] < $B[$counter2])
  47. {
  48.      $counter1=$counter1+1; }
  49.  
  50.     else
  51. {
  52.      $counter2=$counter2+1;
  53. }
  54. }
  55.  
  56. print_r($commonArray);//to print the output array
  57. ?>
  58.  
  59.  
Parsed in 0.054 seconds