/** * File: time_complexity.kt * Created Time: 2024-01-25 * Author: curtishd (1023632660@qq.com) */ package chapter_computational_complexity.time_complexity /* Constant complexity */ 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 individual operations } } } return count } /* Exponential complexity (loop implementation) */ fun exponential(n: Int): Int { var count = 0 var base = 1 // Cells split into two every round, forming the sequence 1, 2, 4, 8, ..., 2^(n-1) for (i in 0.. 1) { n1 /= 2 count++ } return count } /* Logarithmic complexity (recursive implementation) */ fun logRecur(n: Int): Int { if (n <= 1) return 0 return logRecur(n / 2) + 1 } /* Linear logarithmic complexity */ fun linearLogRecur(n: Int): Int { if (n <= 1) return 1 var count = linearLogRecur(n / 2) + linearLogRecur(n / 2) for (i in 0..