Advertisement
Vanya_Shestakov

Untitled

Oct 6th, 2021
2,346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 6.69 KB | None | 0 0
  1. def main(args: Array[String]) = {
  2.  
  3.     println("Это тестовая программа, предназначенная для проверки работы синтаксического анализатора кода")
  4.     println("В данной программе представлены основные синтаксические конструкции языка Scala")
  5.    
  6.     val a = 0
  7.     val b = 3
  8.     if (a > b) {
  9.         println("a > b")
  10.     } else if (a < b) {
  11.         println("a < b")
  12.     } else {
  13.         println("a = b")    
  14.     }
  15.    
  16.     var counter: Int = 1
  17.  
  18.     do {
  19.       println(counter)
  20.       counter += 1
  21.     } while (counter < 5)
  22.    
  23.     counter = 1
  24.    
  25.     while (counter <= 5 ) {
  26.       println(counter)
  27.       counter += 1;
  28.     }
  29.    
  30.     for (i <- 0 to 5) {
  31.        println(counter)
  32.     }  
  33.    
  34.     val array1 = Array(3, 6, 1, 5, 2, 0, 8)
  35.     bubbleSort1(array1)
  36.    
  37.     val array2 = Array(4, 6, 22, 56, 11, 55, 223, 1, 7, 33, 9, 10, 67, 88, 2, 5, 6, 9, 213, 6, 3)
  38.     bubbleSort2(array2)
  39.    
  40.     val array3 = Array(4, 6, 11, 55, 223, 1, 7, 33, 9, 10, 67, 88, 2, 5, 6, 9, 213, 6, 3)
  41.     bubbleSort(array3)
  42.    
  43.     val array4 = Array(4, 6, 22, 56, 11, 55, 223, 1, 7, 6, 3)
  44.     quickSort(array4)
  45.    
  46.     val array5 = Array(4, 6, 22, 56, 11, 55, 223, 1, 7, 6, 3, 0, 1, 2, 3)
  47.     recursiveInsertionSort(array5)
  48.  
  49.     val array6 = Array(4, 6, 22, 56, 55, 223, 43, 23, 1, 7, 6, 3)
  50.     selectionSort(array6)
  51.  
  52.     val array7 = Array(4, 6, 22, 56, 11, 3)
  53.     insertionSort(array7)
  54.    
  55.     val array8 = Array(56, 11, 55, 223, 1, 7, 6, 3)
  56.     mergeSort(array8)
  57.  
  58.  
  59.  
  60.     val x: Int = Random.nextInt(11)
  61.  
  62.     x match {
  63.       case 0 =>
  64.           "zero"
  65.       case 1 =>
  66.           "one"
  67.       case 2 =>
  68.           "two"
  69.       case 3 =>
  70.           "three"
  71.       case 4 =>
  72.           "four"
  73.       case 5 =>
  74.           "five"
  75.       case 6 =>
  76.           "six"
  77.       case 7 =>
  78.           "seven"
  79.       case 8 =>
  80.           "ate"
  81.       case 9 =>
  82.           "nine"
  83.       case 10 =>
  84.           "ten"
  85.       case _ =>
  86.           "many"
  87.     }
  88.    
  89.     val userInput = scala.io.StdIn.readInt();
  90.    
  91.     if (userInput == 1) {
  92.       println("Nth no. of" + userInput + " using 'FOR' loop is : 1");
  93.     } else if (userInput == 2) {
  94.       println("Nth no. of" + userInput + " using 'FOR' loop is : 1");
  95.     } else if (userInput > 2) {
  96.         fibonacci(userInput);
  97.     }
  98.    
  99.     val boolRes = (true && false) || false
  100.  
  101. }
  102.  
  103. def bubbleSort(numbers: Array[Int]) = {
  104.   for (k <- 1 until numbers.length; j <- 0 until numbers.length - 1) {
  105.     if (numbers(j) > numbers(j + 1)) {
  106.       val x = numbers(j)
  107.       numbers(j) = numbers(j + 1)
  108.       numbers(j + 1) = x
  109.     }
  110.   }
  111. }
  112.  
  113. def bubbleSort2(numbers: Array[Int]) = {
  114.   for (k <- 1 until numbers.length; j <- 0 until numbers.length - 1 if numbers(j) > numbers(j+1)) {
  115.     (numbers(j), numbers(j + 1)) match {
  116.       case (x, y) =>
  117.         numbers(j) = y
  118.         numbers(j + 1) = x
  119.     }
  120.   }
  121. }
  122.  
  123. def bubbleSort3(numbers: Array[Int]) = {
  124.   for (k <- 1 until numbers.length; j <- 0 until numbers.length - k
  125.        if (numbers(j) > numbers(j+1)) {
  126.     val x = numbers(j)
  127.     numbers(j) = numbers(j + 1)
  128.     numbers(j + 1) = x
  129.   }
  130. }
  131.  
  132. def fibonacci(userInput: Int) = {
  133.     var temp1, temp2 = 1;
  134.     var result = 0;
  135.     for (i <- 3 to userInput) {
  136.       result = temp1 + temp2;
  137.       temp1 = temp2;
  138.       temp2 = result;
  139.       i + userInput;
  140.     }
  141.     println("Nth no. of " + userInput + " using 'FOR' loop is : " + result);
  142. }
  143.  
  144. def quickSort(array: Array[Int]): Array[Int] = {
  145.  
  146.     def quickSortImpl(array: Array[Int], first: Int, last: Int): Array[Int] = {
  147.       var pivot: Int = 0
  148.       var i: Int = 0
  149.       var j: Int = 0
  150.       var temp: Int = 0
  151.  
  152.       if (first < last) {
  153.         pivot = first
  154.         i = first
  155.         j = last
  156.  
  157.         while (i < j) {
  158.           while (array(i) <= array(pivot) && i < last) {
  159.             i += 1
  160.           }
  161.  
  162.           while (array(j) > array(pivot)) {
  163.             j -= 1
  164.           }
  165.  
  166.           if (i < j) {
  167.             temp = array(i)
  168.             array(i) = array(j)
  169.             array(j) = temp
  170.           }
  171.         }
  172.  
  173.         temp = array(pivot)
  174.         array(pivot) = array(j)
  175.         array(j) = temp
  176.         quickSortImpl(array, first, j - 1)
  177.         quickSortImpl(array, j + 1, last)
  178.       }
  179.  
  180.       array
  181.     }
  182.    
  183.     quickSortImpl(array, 0, array.length - 1)
  184.   }
  185.  
  186.   def recursiveInsertionSort(array: List[Int]): List[Int] = {
  187.  
  188.     def insertion(x: List[Int]): List[Int] = {
  189.       x match {
  190.         case List() => List()
  191.         case x :: xs => ins(x, insertion(xs))
  192.       }
  193.     }
  194.  
  195.     def ins(x: Int, xs: List[Int]): List[Int] = {
  196.       xs match {
  197.         case List() => List(x)
  198.         case x2 :: xs2 => if (x <= x2) x :: xs else x2 :: ins(x, xs2)
  199.       }
  200.     }
  201.  
  202.     insertion(array)
  203.   }
  204.  
  205.   def selectionSort(array: Array[Int]): Array[Int] = {
  206.  
  207.     for (i <- 0 to array.length - 1) {
  208.  
  209.       var min: Int = i
  210.       var minVal = array(i)
  211.  
  212.       for (j <- i + 1 to array.length - 1) {
  213.         if (array(j) < minVal) {
  214.           min = j
  215.           minVal = array(j)
  216.         }
  217.       }
  218.  
  219.       val temp: Int = array(i)
  220.       array(i) = array(min)
  221.       array(min) = temp
  222.  
  223.     }
  224.  
  225.     array
  226.   }
  227.  
  228.   def insertionSort(array: Array[Int]): Array[Int] = {
  229.  
  230.     for (i <- 0 to array.length - 1) {
  231.       val temp: Int = array(i)
  232.       var j = i - 1
  233.       while (j >= 0 && temp < array(j)) {
  234.         array(j + 1) = array(j)
  235.         j -= 1
  236.       }
  237.  
  238.       array(j + 1) = temp
  239.     }
  240.  
  241.     array
  242.   }
  243.  
  244.   def mergeSort(array: Array[Int]): Array[Int] = {
  245.  
  246.     def sort(array: Array[Int]): Array[Int] = {
  247.       MS(array, 0, array.length - 1)
  248.     }
  249.  
  250.     def MS(array: Array[Int], low: Int, high: Int): Array[Int] = {
  251.       if (low < high) {
  252.         val mid = (low + high) / 2
  253.         MS(array, low, mid)
  254.         MS(array, mid + 1, high)
  255.         merge(array, low, mid, high)
  256.       } else {
  257.         array
  258.       }
  259.     }
  260.  
  261.     def merge(array: Array[Int], low: Int, mid: Int, high: Int): Array[Int] = {
  262.    
  263.       val left = array.slice(low, mid + 1)
  264.       val right = array.slice(mid + 1, high + 1)
  265.  
  266.       var i = 0
  267.       var j = 0
  268.       var k = low
  269.       while (k < high + 1) {
  270.         if (i > left.length - 1) {
  271.           array(k) = right(j)
  272.           j = j + 1
  273.         } else if (j > right.length - 1) {
  274.           array(k) = left(i)
  275.           i = i + 1
  276.         } else if (left(i) <= right(j)) {
  277.           array(k) = left(i)
  278.           i = i + 1
  279.         } else {
  280.           array(k) = right(j)
  281.           j = j + 1
  282.         }
  283.         k = k + 1
  284.       }
  285.  
  286.       array
  287.  
  288.     }
  289.  
  290.     sort(array)
  291.   }
  292.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement