Advertisement
Guest User

Main

a guest
May 11th, 2017
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.11 KB | None | 0 0
  1. clear all;close all;clc;
  2.  
  3. maxArraySize = 1024;
  4.  
  5. for N = 1:maxArraySize    
  6.    
  7.     array = 1:N;
  8.    
  9.     for searchTarget = 1:N      
  10.         comparisons(searchTarget) = exponentialSearch(array, N, searchTarget);        
  11.     end
  12.    
  13.     min_comps(N) = min(comparisons);  
  14.     avg_comps(N) = mean(comparisons);  
  15.     max_comps(N) = max(comparisons);  
  16.        
  17.     clear comparisons;    
  18.    
  19. end
  20.  
  21. figure;
  22.  
  23. % Plot Observed
  24.  
  25. plot([1:maxArraySize], min_comps,'g','LineWidth',3);hold on;
  26. plot([1:maxArraySize], avg_comps,'y','LineWidth',3);
  27. plot([1:maxArraySize], max_comps,'r','LineWidth',3);
  28.  
  29. legend('min','mean','max');
  30.  
  31. % Plot Expected
  32.  
  33. plot([1:maxArraySize], linspace(1,1,maxArraySize), 'k:');
  34. plot([1:maxArraySize], linspace(1,log2(N),maxArraySize), 'k:');
  35. plot([1:maxArraySize], linspace(1,(log2(N))*2,maxArraySize), 'k:');
  36.  
  37. % Annotate Chart
  38.  
  39. xlabel('Array Size (N)','FontSize',14);
  40. ylabel('Comparisons', 'FontSize', 14);
  41. title('Exponential Search (Successful)','FontSize', 14);
  42. xlim([0 maxArraySize]);
  43. ylim([0 max(max_comps)]);
  44.  
  45. print -f1 -r300 -dbmp exponentialSearchSuccessful.bmp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement