/** * File: time_complexity.kt * Created Time: 2024-01-25 * Author: curtishd (1023632660@qq.com) */ package chapter_computational_complexity.time_complexity /* Constant order */ fun constant(n: Int): Int { var count = 0 val size = 100000 for (i in 0.. nums[j + 1]) { // Swap nums[j] and nums[j + 1] val temp = nums[j] nums[j] = nums[j + 1] nums[j + 1] = temp count += 3 // Element swap includes 3 unit operations } } } return count } /* Exponential order (loop implementation) */ fun exponential(n: Int): Int { var count = 0 var base = 1 // Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1) for (i in 0.. 1) { n1 /= 2 count++ } return count } /* Logarithmic order (recursive implementation) */ fun logRecur(n: Int): Int { if (n <= 1) return 0 return logRecur(n / 2) + 1 } /* Linearithmic order */ fun linearLogRecur(n: Int): Int { if (n <= 1) return 1 var count = linearLogRecur(n / 2) + linearLogRecur(n / 2) for (i in 0..