Guest User

Untitled

a guest
Feb 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. Test with 8 integers
  2. OK
  3. Test with 16 integers
  4. OK
  5. Test with 32 integers
  6. OK
  7. Test with 64 integers
  8. OK
  9. Test with 128 integers
  10. OK
  11. Test with 256 integers
  12. OK
  13. Test with 512 integers
  14. OK
  15.  
  16. void median(int t[], int n){
  17. int m = ((n - 1) / 2);
  18. if (t[0] > t[n - 1]) { /* t[0] > t[n - 1] */
  19. if (t[m] > t[0]); /* t[m] > t[0] > t[n - 1] */
  20.  
  21. else if (t[n - 1] > t[m]) /* t[0] > t[n - 1] > t[m] */
  22. swap(t[0], t[n-1]);
  23.  
  24. else /* t[0] >= t[m] >= t[n - 1] */
  25. swap(t[0], t[m]);
  26.  
  27. } else { /* t[0] <= t[n - 1] */
  28. if (t[m] < t[0]) ; /* t[m] < t[0] <= t[n - 1] */
  29.  
  30. else if (t[n - 1] < t[m]) /* t[0] <= t[n - 1] < t[m] */
  31. swap(t[0], t[n-1]);
  32.  
  33. else /* t[0] <= t[m] <= t[n + 1] */
  34. swap(t[0], t[m]);
  35. }
  36.  
  37. }
  38.  
  39. int partition( int t[], int n) {
  40. int l = 0, r = n, tmp;
  41.  
  42. median(t, r);
  43.  
  44. while(1) {
  45. do
  46. ++l;
  47. while(l < n && t[l] < t[0]); // l <= n-1
  48. do
  49. --r;
  50. while(t[r] > t[0]); // r >= 0
  51. if( l >= r )
  52. break;
  53. tmp = t[l];
  54. t[l] = t[r];
  55. t[r] = tmp;
  56. }
  57. tmp = t[0];
  58. t[0] = t[r];
  59. t[r] = tmp;
  60.  
  61. return r;
  62. }
  63.  
  64. void version2(int t[], int size) {
  65.  
  66. int m, l = 0, r = size ;
  67. while((r - l) > 1) {
  68.  
  69. m = partition( t + l, r);
  70. if( (m - l) < (r - m)){
  71. version2(t + l, m);
  72. l = m + 1;
  73. }
  74. else{
  75. version2(t + m + 1, r);
  76. r = m;
  77. }
  78. }
  79.  
  80. }
  81.  
  82. Test with 8 integers
Add Comment
Please, Sign In to add comment