├── .idea ├── .gitignore ├── dbnavigator.xml ├── libraries │ └── KotlinJavaRuntime.xml ├── misc.xml ├── modules.xml ├── uiDesigner.xml └── vcs.xml ├── README.md ├── SearchAlgaritim.iml ├── out └── production │ └── SearchAlgaritim │ ├── META-INF │ └── SearchAlgaritim.kotlin_module │ ├── MainKt.class │ ├── pdpMasala2.zip │ ├── pdpMasala2 │ ├── MainKt.class │ └── MergeSort.class │ ├── pdpMasala5.zip │ ├── pdpMasala5 │ └── MainKt.class │ ├── pdpMasala6 │ └── FibonacciNumbers.class │ ├── pdpMasla1.zip │ ├── pdpMasla1 │ ├── Main1Kt.class │ └── ShellSort.class │ ├── pdpMasla3.zip │ ├── pdpMasla3 │ └── MainKt.class │ ├── pdpMasla4.zip │ └── pdpMasla4 │ ├── HeapSort.class │ └── MainKt.class └── src ├── Main.kt ├── pdpMasala2.zip ├── pdpMasala2 ├── Main.kt └── MergeSort.kt ├── pdpMasala5.zip ├── pdpMasala5 └── Main.kt ├── pdpMasala6.zip ├── pdpMasala6 ├── Fibonacci.kt └── Main.kt ├── pdpMasla1.zip ├── pdpMasla1 ├── Main1.kt └── ShellSort.kt ├── pdpMasla3.zip ├── pdpMasla3 └── Main.kt ├── pdpMasla4.zip └── pdpMasla4 ├── HeapSort.kt └── Main.kt /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/dbnavigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | -------------------------------------------------------------------------------- /.idea/libraries/KotlinJavaRuntime.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kotlin-Algroitms 2 | Kotlin tutorial Algorithm Search 3 | 4 | 5 | 6 | ![image](https://user-images.githubusercontent.com/103598601/163670409-b82314ab-a70b-4843-b773-9047c42fdda8.png) 7 | -------------------------------------------------------------------------------- /SearchAlgaritim.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/META-INF/SearchAlgaritim.kotlin_module: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | MainKt 5 |  6 | 7 | pdpMasala2MainKt 8 |  9 | 10 | pdpMasala5MainKt 11 |  12 | pdpMasla1Main1Kt 13 |  14 | pdpMasla3MainKt 15 |  16 | pdpMasla4MainKt"* -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/MainKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/MainKt.class -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasala2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasala2.zip -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasala2/MainKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasala2/MainKt.class -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasala2/MergeSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasala2/MergeSort.class -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasala5.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasala5.zip -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasala5/MainKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasala5/MainKt.class -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasala6/FibonacciNumbers.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasala6/FibonacciNumbers.class -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasla1.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasla1.zip -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasla1/Main1Kt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasla1/Main1Kt.class -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasla1/ShellSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasla1/ShellSort.class -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasla3.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasla3.zip -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasla3/MainKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasla3/MainKt.class -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasla4.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasla4.zip -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasla4/HeapSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasla4/HeapSort.class -------------------------------------------------------------------------------- /out/production/SearchAlgaritim/pdpMasla4/MainKt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/out/production/SearchAlgaritim/pdpMasla4/MainKt.class -------------------------------------------------------------------------------- /src/Main.kt: -------------------------------------------------------------------------------- 1 | fun main() { 2 | 3 | var a= arrayListOf(1,34,78,8,12,45,7,9,2,4,3,7,5,0,9) 4 | insertionSort(a) 5 | println(a) 6 | var b= arrayListOf(1,34,78,8,12,45,7,9,2,4,3,7,5,0,9) 7 | selectionSort(b) 8 | println(a) 9 | 10 | 11 | 12 | } 13 | 14 | fun insertionSort(array:ArrayList){ 15 | var position:Int 16 | for (i in 1 until array.size){ 17 | var temp=array[i] 18 | position=i 19 | 20 | while (0){ 32 | for (i in 0 until array.size-1){ 33 | var smallPosition=i 34 | for (j in i+1 until array.size){ 35 | if (array[j]) { 4 | var mergeSort=MergeSort() 5 | val numbers = mutableListOf(38,27,43,3,82,10,11,2,9,46,7,8,1,) 6 | val sortedList = mergeSort.mergeSort(numbers) 7 | println("Unsorted: $numbers") 8 | println("Sorted: $sortedList") 9 | } -------------------------------------------------------------------------------- /src/pdpMasala2/MergeSort.kt: -------------------------------------------------------------------------------- 1 | package pdpMasala2 2 | 3 | class MergeSort { 4 | fun mergeSort(list: List): List { 5 | if (list.size <= 1) { 6 | return list 7 | } 8 | 9 | val middle = list.size / 2 10 | var left = list.subList(0,middle); 11 | var right = list.subList(middle,list.size); 12 | 13 | return merge(mergeSort(left), mergeSort(right)) 14 | } 15 | fun merge(left: List, right: List): List { 16 | var indexLeft = 0 17 | var indexRight = 0 18 | var newList : MutableList = mutableListOf() 19 | 20 | while (indexLeft < left.count() && indexRight < right.count()) { 21 | if (left[indexLeft] <= right[indexRight]) { 22 | newList.add(left[indexLeft]) 23 | indexLeft++ 24 | } else { 25 | newList.add(right[indexRight]) 26 | indexRight++ 27 | } 28 | } 29 | 30 | while (indexLeft < left.size) { 31 | newList.add(left[indexLeft]) 32 | indexLeft++ 33 | } 34 | 35 | while (indexRight < right.size) { 36 | newList.add(right[indexRight]) 37 | indexRight++ 38 | } 39 | return newList; 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /src/pdpMasala5.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/src/pdpMasala5.zip -------------------------------------------------------------------------------- /src/pdpMasala5/Main.kt: -------------------------------------------------------------------------------- 1 | package pdpMasala5 2 | 3 | import java.util.* 4 | import kotlin.math.sqrt 5 | 6 | 7 | 8 | fun main(ar: Array) { 9 | val sc = Scanner(System.`in`) 10 | println("Array uzunligi :") 11 | val n = sc.nextInt() 12 | val a = IntArray(n) 13 | println("Arraydagi elementlar :") 14 | for (i in 0 until n) { 15 | println("${i.plus(1)}] element : ") 16 | a[i] = sc.nextInt() 17 | } 18 | print("[") 19 | for (i in a) { 20 | print("$i,") 21 | 22 | } 23 | println("]") 24 | val block = sqrt(n.toDouble()).toInt() 25 | println("qidirilayotgan element :") 26 | val x = sc.nextInt() 27 | if (x > a[n - 1] || x < a[0]) { 28 | println("Bunday element arrayda yuq") 29 | return 30 | } 31 | var i = 0 32 | var j = block 33 | while (a[j] < x && j < n) { 34 | i = j 35 | j += block 36 | if (j > n - 1) { 37 | j = n - 1 38 | } 39 | } 40 | for (k in i..j) { 41 | if (a[k] == x) { 42 | println("is Success !") 43 | println("element :" + (k + 1)) 44 | return 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /src/pdpMasala6.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/src/pdpMasala6.zip -------------------------------------------------------------------------------- /src/pdpMasala6/Fibonacci.kt: -------------------------------------------------------------------------------- 1 | package pdpMasala6 2 | 3 | class Fibonacci { 4 | fun fibonacci(n: Int): Int { 5 | return if (n == 0) { 6 | 0 7 | } else if (n == 1) { 8 | 1 9 | } else if (n > 1) { 10 | fibonacci(n - 1) + fibonacci(n - 2) 11 | } else { 12 | 0 13 | } 14 | } 15 | 16 | 17 | } -------------------------------------------------------------------------------- /src/pdpMasala6/Main.kt: -------------------------------------------------------------------------------- 1 | package pdpMasala6 2 | 3 | import java.util.* 4 | 5 | fun main(args: Array) { 6 | var fibonacci=Fibonacci() 7 | val `in` = Scanner(System.`in`) 8 | println("This program produces the (n)th Fibonacci number.") 9 | println() 10 | var i = 0 11 | while (i < 1) { 12 | print("Choose an integer value greater than 1 to be n (Type '0' to quit): ") 13 | val n = `in`.nextInt() 14 | var f = 0 15 | if (n > 1) { 16 | f = fibonacci.fibonacci(n) 17 | println("The " + n + "th Fibonacci number is: " + f) 18 | println() 19 | } else if (n == 0) //terminating input 20 | { 21 | i++ 22 | } else //Error trap 23 | { 24 | println("Input not greater than 1. Try again.") 25 | println() 26 | } 27 | i += 0 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/pdpMasla1.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/src/pdpMasla1.zip -------------------------------------------------------------------------------- /src/pdpMasla1/Main1.kt: -------------------------------------------------------------------------------- 1 | package pdpMasla1 2 | 3 | import java.util.* 4 | 5 | fun main(args: Array) { 6 | val data = intArrayOf(9, 8, 3, 7, 5, 6, 4, 1) 7 | val size = data.size 8 | val ss = ShellSort() 9 | ss.shellSort(data, size) 10 | println("Sorted Array in Ascending Order: ") 11 | println(Arrays.toString(data)) 12 | } -------------------------------------------------------------------------------- /src/pdpMasla1/ShellSort.kt: -------------------------------------------------------------------------------- 1 | package pdpMasla1 2 | 3 | // She'll sort 4 | internal class ShellSort { 5 | // Rearrange elements at each n/2, n/4, n/8, ... intervals 6 | fun shellSort(array: IntArray, n: Int) { 7 | var interval = n / 2 8 | while (interval > 0) { 9 | var i = interval 10 | while (i < n) { 11 | val temp = array[i] 12 | var j: Int = i 13 | while (j >= interval && array[j - interval] > temp) { 14 | array[j] = array[j - interval] 15 | j -= interval 16 | } 17 | array[j] = temp 18 | i += 1 19 | } 20 | interval /= 2 21 | } 22 | } 23 | 24 | 25 | 26 | 27 | } -------------------------------------------------------------------------------- /src/pdpMasla3.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/src/pdpMasla3.zip -------------------------------------------------------------------------------- /src/pdpMasla3/Main.kt: -------------------------------------------------------------------------------- 1 | package pdpMasla3 2 | 3 | fun quicksort(items:List):List{ 4 | if (items.count() < 2){ 5 | return items 6 | } 7 | val pivot = items[items.count()/2] 8 | 9 | val equal = items.filter { it == pivot } 10 | 11 | val less = items.filter { it < pivot } 12 | 13 | val greater = items.filter { it > pivot } 14 | 15 | return quicksort(less) + equal + quicksort(greater) 16 | } 17 | fun main(args: Array) { 18 | val numbers = listOf(2, 4, 7, 3, 6, 9, 5, 1, 0) 19 | println("No Sorted"+numbers) 20 | val ordered = quicksort(numbers) 21 | println("Sorted "+ordered) 22 | } -------------------------------------------------------------------------------- /src/pdpMasla4.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/professorDeveloper/Kotlin-Algorithms/3dae31c7a0da2042bf593bd44b5cd9c3573797ad/src/pdpMasla4.zip -------------------------------------------------------------------------------- /src/pdpMasla4/HeapSort.kt: -------------------------------------------------------------------------------- 1 | package pdpMasla4 2 | 3 | class HeapSort { 4 | /** 5 | * Heap sort 6 | * 7 | * Given an array, sort it in ascending order using heap sort algorithm 8 | */ 9 | 10 | 11 | 12 | fun buildMaxHeap(array: IntArray) { 13 | 14 | for (i in ((array.size/2) - 1) downTo 0) { 15 | maxHeapify(array, i) 16 | } 17 | 18 | } 19 | 20 | fun maxHeapify(array: IntArray, rootIndex: Int, heapSize: Int = array.size - 1) { 21 | 22 | val leftChildIndex = (2 * rootIndex) + 1 23 | val rightChildIndex = (2 * rootIndex) + 2 24 | var largestElementIndex = rootIndex 25 | 26 | if (leftChildIndex <= heapSize && array[leftChildIndex] > array[rootIndex]) { 27 | largestElementIndex = leftChildIndex 28 | } 29 | 30 | if (rightChildIndex <= heapSize && array[rightChildIndex] > array[largestElementIndex]) { 31 | largestElementIndex = rightChildIndex 32 | } 33 | 34 | if (largestElementIndex != rootIndex) { 35 | val temp = array[rootIndex] 36 | array[rootIndex] = array[largestElementIndex] 37 | array[largestElementIndex] = temp 38 | maxHeapify(array, largestElementIndex, heapSize) 39 | } 40 | 41 | } 42 | } -------------------------------------------------------------------------------- /src/pdpMasla4/Main.kt: -------------------------------------------------------------------------------- 1 | package pdpMasla4 2 | 3 | fun main(args: Array) { 4 | var heapSort=HeapSort() 5 | val array = intArrayOf(4, 1, 3, 2, 16, 9, 10, 14, 8, 7) 6 | print("no Sorted :") 7 | for (i in array) { 8 | print("$i ,") 9 | } 10 | heapSort.buildMaxHeap(array) 11 | 12 | for (i in array.size - 1 downTo 1) { 13 | val temp = array[0] 14 | array[0] = array[i] 15 | array[i] = temp 16 | heapSort.maxHeapify(array, 0, i - 1) 17 | } 18 | println() 19 | print("sorted :") 20 | for (i in array) { 21 | print("$i,") 22 | } 23 | 24 | } --------------------------------------------------------------------------------