Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. /*Submitted by: Roman Kotoh , ID: 313102576 & Dima Yankovoy , ID: 311868442
  2.  
  3.  
  4. Proof for the algorithm for m=n case. Let name middle element as "k"
  5. m1:=median (A, N) - O (1)
  6. m2:=median (B, N) - O (1)
  7. Proof:
  8. If m1 < m2, so m1 < k < m2 otherwise m2 < k < m1
  9. Without loss of generality –if m1 < k < m2, we know that the median of the two arrays must be
  10. Must be A[m1] < k <B [m2] so we can drop the irrelevant parts (A[0….m1] , B[m2……N]) by searching for new medians in each array.
  11.  
  12. Proof by contradiction:
  13. Again without loss of generality…
  14. We will assume that k is found within ((A[0….m1] U B[m2……N]))
  15. Array A contains (((N+1)/2) -1) elements smaller than m1
  16. Array B contains maximum (((N+1)/2) -1) elements smaller than m2
  17. Unified set is - ((A[0….m1] U B[m2……N])) :
  18. (((N+1)/2) -1) + (((N+1)/2) -1) = N-1 <- elements before m1
  19. Contradicting the definition of median which is N= [((2N+1) / 2)]
  20. We can come to a conclusion that k cannot be a part of ((A[0….m1] U B[m2……N]))
  21. Therefore it must be found somewhere ((A[m1….n] U B[0….m2]))
  22.  
  23. m1:=median (A+m1, N / 2) - O (1)
  24. m2:=median (B, N/ 2) - O (1)
  25. By reducing the size of the arrays by half each iteration we logarithmically decreasing the running time and thus improving efficiency – O (logN)
  26. And we repeat until size = 1 or 2
  27.  
  28.  
  29. Efficiency :
  30.  
  31. T(N) = T(N/2) + 1
  32. T(1) = 1
  33. T(N) = T(N/2) + 1 = T(N/4) + 1 + 1 = .......
  34. T(N/(2^k)+k)
  35. N/(2^k) = 1
  36. k = logN so - > T(N) = logN + 1
  37.  
  38. We can sum the efficiency of the algorithm to O (logN)
  39.  
  40.  
  41. */
  42.  
  43. #include<stdio.h>
  44.  
  45. int max(int x, int y); /* maximum of two integers */
  46. int min(int x, int y); /* minimum of two integeres */
  47. int median_of_array(int arr[], int n); /*median of a sorted array */
  48. int median_of_2_sorted_arrays_iterative(int ar1[], int ar2[], int n); /*returns median of ar1[] and ar2[]*/
  49.  
  50.  
  51. /* main for test */
  52. void main()
  53. {
  54. int ar1[] = { 1, 2, 3, 5, 7, 9, 11, 14, 16 };
  55. int ar2[] = { 6, 8, 10, 12, 13, 18, 19, 27, 33 };
  56. int n1 = sizeof(ar1) / sizeof(ar1[0]);
  57. int n2 = sizeof(ar2) / sizeof(ar2[0]);
  58.  
  59. if (n1 == n2)
  60. printf("Median is %d (found iteratively)\n\n", median_of_2_sorted_arrays_iterative(ar1, ar2, n1));
  61. else
  62. printf("Doesn't work for arrays of unequal size");
  63.  
  64.  
  65. getchar();
  66.  
  67. }
  68.  
  69. /* This function returns median of ar1[] and ar2[].
  70. Assumptions in this function:
  71. Both ar1[] and ar2[] are sorted arrays
  72. Both have n elements */
  73. int median_of_2_sorted_arrays_iterative(int ar1[], int ar2[], int n)
  74. {
  75. int m1; /* For median of ar1 */
  76. int m2; /* For median of ar2 */
  77. int * parr1;
  78. int * parr2;
  79.  
  80. /* return -1 for invalid input */
  81. if (n < 1)
  82. return -1;
  83. parr1 = ar1;
  84. parr2 = ar2;
  85.  
  86. while (2 < n)
  87. {
  88. m1 = median_of_array(parr1, n); /*median of the first array */
  89. m2 = median_of_array(parr2, n); /*median of the second array */
  90. if (m1 == m2)
  91. return m1;
  92.  
  93. if (m1 < m2)
  94. {
  95. if (n % 2 == 0)
  96. {
  97. parr1 += ((n / 2) - 1);
  98. n = n - (n / 2) + 1;
  99. }
  100. else
  101. {
  102. parr1 += (n / 2);
  103. n -= (n / 2);
  104. }
  105. }
  106. else
  107. {
  108. if (n % 2 == 0)
  109. {
  110. parr2 += (n / 2 - 1);
  111. n = n - (n / 2 + 1);
  112. }
  113. else
  114. {
  115. parr2 += (n / 2);
  116. n -= (n / 2);
  117. }
  118. }
  119. }
  120.  
  121. if (n == 1)
  122. return (((*parr1) + (*parr2)) / 2);
  123.  
  124. if (n == 2)
  125. return (((max(*parr1, *parr2) + min(*(parr1 + 1), *(parr2 + 1)))) / 2);
  126.  
  127. }
  128.  
  129.  
  130.  
  131.  
  132. /* Function to get median of a sorted array */
  133. int median_of_array(int arr[], int n)
  134. {
  135. if (n % 2 == 0)
  136. return (arr[n / 2] + arr[n / 2 - 1]) / 2;
  137. else
  138. return arr[n / 2];
  139. }
  140.  
  141.  
  142. /* maximum func */
  143. int max(int x, int y)
  144. {
  145. if (x > y)
  146. return x;
  147. else
  148. return y;
  149. }
  150.  
  151. /* minimum func */
  152. int min(int x, int y)
  153. {
  154. if (x < y)
  155. return x;
  156. else
  157. return y;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement