Advertisement
informaticage

3 biggest elements

Nov 28th, 2022
1,031
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     const V = [...Array(10)].map(x => Math.round(Math.random() * 100));
  2.  
  3.     const max3 = function (arr) {
  4.       // 3 passaggi
  5.       let [a, b, c] = [Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER - 1, Number.MIN_SAFE_INTEGER - 2];
  6.  
  7.       // N executions
  8.       for (let i = 0; i < arr.length; i++) {
  9.         // 1 o 2
  10.         if (arr[i] > a) {
  11.           a = arr[i];
  12.         }
  13.       }
  14.  
  15.       // N executions
  16.       for (let i = 0; i < arr.length; i++) {
  17.         // 1 o 2
  18.         if (arr[i] > b && arr[i] != a) {
  19.           b = arr[i];
  20.         }
  21.       }
  22.  
  23.       // N executions
  24.       for (let i = 0; i < arr.length; i++) {
  25.         // 1 o 2
  26.         if (arr[i] > c && arr[i] != a && arr[i] != b) {
  27.           c = arr[i];
  28.         }
  29.       }
  30.  
  31.       return [a, b, c];
  32.     }
  33.  
  34.     // T(N) = 3N + 3 a 6N + 3 in generale impiega T(N) = KN + K'
  35.     // S(N) = 3 = K
  36.  
  37.     // Costo lineare = T(N) = N
  38.     // Costo spaziale = S(N) = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement