Advertisement
Guest User

Untitled

a guest
Jan 12th, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.04 KB | None | 0 0
  1. object BinarySearch {
  2.     def main(): Unit = {
  3.         println(new BS().Start(20));
  4.     }
  5. }
  6.  
  7. // This class contains an array of integers and
  8. // methods to initialize, print and search the array
  9. // using Binary Search
  10. class BS {
  11.     var number : Int[];
  12.     var size : Int;
  13.  
  14.     // Invoke methods to initialize, print and search
  15.     // for elements on the array
  16.     def Start(sz : Int) : Int = {
  17.         var aux01 : Int;
  18.         var aux02 : Int;
  19.  
  20.         aux01 = this.Init(sz);
  21.         aux02 = this.Print();
  22.  
  23.         if (this.Search(8))
  24.             println(1);
  25.         else
  26.             println(0);
  27.         if (this.Search(19))
  28.             println(1);
  29.         else
  30.             println(0);
  31.         if (this.Search(20))
  32.             println(1);
  33.         else
  34.             println(0);
  35.         if (this.Search(21))
  36.             println(1);
  37.         else
  38.             println(0);
  39.         if (this.Search(37))
  40.             println(1);
  41.         else
  42.             println(0);
  43.         if (this.Search(38))
  44.             println(1);
  45.         else
  46.             println(0);
  47.         if (this.Search(39))
  48.             println(1);
  49.         else
  50.             println(0);
  51.         if (this.Search(50))
  52.             println(1);
  53.         else
  54.             println(0);
  55.  
  56.         return 999;
  57.     }
  58.  
  59.  
  60.     // Search for a specific value (num) using
  61.     // binary search
  62.     def Search(num : Int) : Bool = {
  63.         var bs01 : Bool;
  64.         var right : Int;
  65.         var left : Int;
  66.         var var_cont : Bool;
  67.         var medium : Int;
  68.         var aux01 : Int;
  69.         var nt : Int;
  70.  
  71.         aux01 = 0;
  72.         bs01 = false;
  73.         right = number.length;
  74.         right = right - 1;
  75.         left = 0;
  76.         var_cont = true;
  77.         while (var_cont) {
  78.             medium = left + right;
  79.             medium = medium / 2;
  80.             aux01 = number[medium];
  81.             if (num < aux01)
  82.                 right = medium - 1;
  83.             else
  84.                 left = medium + 1;
  85.             if (aux01 == num)
  86.                 var_cont = false;
  87.             else
  88.                 var_cont = true;
  89.             if (right < left)
  90.                 var_cont = false;
  91.             else
  92.                 nt = 0;
  93.         }
  94.  
  95.         if (aux01 == num)
  96.             bs01 = true;
  97.         else
  98.             bs01 = false;
  99.         return bs01;
  100.     }
  101.  
  102.     // Print the integer array
  103.     def Print() : Int = {
  104.         var j : Int;
  105.  
  106.         j = 1 ;
  107.         while (j < (size)) {
  108.             println(number[j]);
  109.             j = j + 1;
  110.         }
  111.         println(99999);
  112.         return 0 ;
  113.     }
  114.  
  115.  
  116.     // Initialize the integer array
  117.     def Init(sz : Int) : Int = {
  118.         var j : Int;
  119.         var k : Int;
  120.         var aux01 : Int;
  121.         var aux02 : Int;
  122.  
  123.         size = sz;
  124.         number = new Int[sz] ;
  125.  
  126.         j = 1 ;
  127.         k = size + 1 ;
  128.         while (j < (size)) {
  129.             aux01 = 2 * j ;
  130.             aux02 = k - 3 ;
  131.             number[j] = aux01 + aux02 ;
  132.             j = j + 1 ;
  133.             k = k - 1 ;
  134.         }
  135.         return 0 ;  
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement