Advertisement
Vanya_Shestakov

Untitled

Dec 2nd, 2021
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. def main(args: Array[String]) = {
  2.  
  3. val xxxxx = 1
  4. xxxxx = 2
  5. val a = 0
  6. val b = 3
  7. if(a > b) {
  8. println(b)
  9. } else {
  10. println(a)
  11. }
  12.  
  13. val input = scala.io.StdIn.readLine()
  14. val output = 0
  15. println(output)
  16.  
  17. if(a > b) {
  18. if(a > b) {
  19. if(a > b) {
  20. if(a > b) {
  21. if(a > b) {
  22. if(a > b) {
  23. println(a)
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30.  
  31. for(i <- 0 to 5) {
  32. if(a > b) {
  33. if(a > b) {
  34. if(a > b) {
  35. println("a > b")
  36. }
  37. }
  38. }
  39. }
  40.  
  41. val x = 11
  42.  
  43. x match {
  44. case 0 =>
  45. "zero"
  46. case 1 =>
  47. "one"
  48. case 2 =>
  49. "two"
  50. case 3 =>
  51. "three"
  52. case 4 =>
  53. "four"
  54. case 5 =>
  55. "five"
  56. }
  57.  
  58. val array1 = Array(3, 6, 1, 5, 2, 0, 8)
  59. bubbleSort1(array1)
  60.  
  61. val array2 = Array(4, 6, 22, 56, 11, 55, 223, 1, 7, 33, 9, 10, 67, 88, 2, 5, 6, 9, 213, 6, 3)
  62. bubbleSort2(array2)
  63.  
  64. val array3 = Array(4, 6, 11, 55, 223, 1, 7, 33, 9, 10, 67, 88, 2, 5, 6, 9, 213, 6, 3)
  65. bubbleSort(array3)
  66.  
  67. val array5 = Array(4, 6, 22, 56, 11, 55, 223, 1, 7, 6, 3, 0, 1, 2, 3)
  68. recursiveInsertionSort(array5)
  69.  
  70. val array6 = Array(4, 6, 22, 56, 55, 223, 43, 23, 1, 7, 6, 3)
  71. selectionSort(array6)
  72.  
  73. val array7 = Array(4, 6, 22, 56, 11, 3)
  74. insertionSort(array7)
  75.  
  76. val array8 = Array(56, 11, 55, 223, 1, 7, 6, 3)
  77. mergeSort(array8)
  78.  
  79. }
  80.  
  81. def bubbleSort(numbers: Array[Int]) = {
  82. for(k <- 1 until numbers.length; j <- 0 until numbers.length - 1) {
  83. if (numbers(j) > numbers(j + 1)) {
  84. val x = numbers(j)
  85. numbers(j) = numbers(j + 1)
  86. numbers(j + 1) = x
  87. }
  88. }
  89. }
  90.  
  91. def bubbleSort2(numbers: Array[Int]) = {
  92. for(k <- 1 until numbers.length; j <- 0 until numbers.length - 1 if numbers(j) > numbers(j+1)) {
  93. (numbers(j), numbers(j + 1)) match {
  94. case (x, y) =>
  95. numbers(j) = y
  96. numbers(j + 1) = x
  97. }
  98. }
  99. }
  100.  
  101. def bubbleSort3(numbers: Array[Int]) = {
  102. for(k <- 1 until numbers.length; j <- 0 until numbers.length - k
  103. if(numbers(j) > numbers(j + 1)) {
  104. val x = numbers(j)
  105. numbers(j) = numbers(j + 1)
  106. numbers(j + 1) = x
  107. }
  108. }
  109.  
  110. def fibonacci(userInput: Int) = {
  111. var temp1, temp2 = 1;
  112. var result = 0;
  113. for (i <- 3 to userInput) {
  114. result = temp1 + temp2;
  115. temp1 = temp2;
  116. temp2 = result;
  117. i + userInput;
  118. }
  119. }
  120.  
  121. def recursiveInsertionSort(array: List[Int]): List[Int] = {
  122.  
  123. def insertion(x: List[Int]): List[Int] = {
  124. x match {
  125. case List() => List()
  126. case x :: xs => ins(x, insertion(xs))
  127. }
  128. }
  129.  
  130. def ins(x: Int, xs: List[Int]): List[Int] = {
  131. xs match {
  132. case List() => List(x)
  133. case x2 :: xs2 => if(x <= x2) x :: xs else x2 :: ins(x, xs2)
  134. }
  135. }
  136.  
  137. insertion(array)
  138. }
  139.  
  140. def selectionSort(array: Array[Int]): Array[Int] = {
  141.  
  142. for(i <- 0 to array.length - 1) {
  143. var min: Int = i
  144. var minVal = array(i)
  145.  
  146. for(j <- i + 1 to array.length - 1) {
  147. if(array(j) < minVal) {
  148. min = j
  149. minVal = array(j)
  150. }
  151. }
  152. val temp: Int = array(i)
  153. array(i) = array(min)
  154. array(min) = temp
  155. }
  156. array
  157. }
  158.  
  159. def insertionSort(array: Array[Int]): Array[Int] = {
  160. for(i <- 0 to array.length - 1) {
  161. val temp: Int = array(i)
  162. var j = i - 1
  163. while(j >= 0 && temp < array(j)) {
  164. array(j + 1) = array(j)
  165. j -= 1
  166. }
  167. array(j + 1) = temp
  168. }
  169. array
  170. }
  171.  
  172. def mergeSort(array: Array[Int]): Array[Int] = {
  173.  
  174. def sort(array: Array[Int]): Array[Int] = {
  175. MS(array, 0, array.length - 1)
  176. }
  177.  
  178. def MS(array: Array[Int], low: Int, high: Int): Array[Int] = {
  179. if(low < high) {
  180. val mid = (low + high) / 2
  181. MS(array, low, mid)
  182. MS(array, mid + 1, high)
  183. merge(array, low, mid, high)
  184. } else {
  185. array
  186. }
  187. }
  188.  
  189. def merge(array: Array[Int], low: Int, mid: Int, high: Int): Array[Int] = {
  190.  
  191. val left = array.slice(low, mid + 1)
  192. val right = array.slice(mid + 1, high + 1)
  193.  
  194. var i = 0
  195. var j = 0
  196. var k = low
  197. while(k < high + 1) {
  198. if(i > left.length - 1) {
  199. array(k) = right(j)
  200. j = j + 1
  201. } else if(j > right.length - 1) {
  202. array(k) = left(i)
  203. i = i + 1
  204. } else if(left(i) <= right(j)) {
  205. array(k) = left(i)
  206. i = i + 1
  207. } else {
  208. array(k) = right(j)
  209. j = j + 1
  210. }
  211. k = k + 1
  212. }
  213. array
  214. }
  215. sort(array)
  216. }
  217.  
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement