/** * File: time_complexity.kt * Created Time: 2024-01-25 * Author: curtishd (1023632660@qq.com) */ package chapter_computational_complexity.time_complexity /* 상수 차수 */ fun constant(n: Int): Int { var count = 0 val size = 100000 for (i in 0.. nums[j + 1]) { // nums[j]와 nums[j + 1]을 교환한다 val temp = nums[j] nums[j] = nums[j + 1] nums[j + 1] = temp count += 3 // 원소 교환에는 단위 연산 3회가 포함된다 } } } return count } /* 지수 차수(반복문 구현) */ fun exponential(n: Int): Int { var count = 0 var base = 1 // 세포가 매번 둘로 나뉘어 수열 1, 2, 4, 8, ..., 2^(n-1)을 이룬다 for (i in 0.. 1) { n1 /= 2 count++ } return count } /* 로그 차수(재귀 구현) */ fun logRecur(n: Int): Int { if (n <= 1) return 0 return logRecur(n / 2) + 1 } /* 선형 로그 차수 */ fun linearLogRecur(n: Int): Int { if (n <= 1) return 1 var count = linearLogRecur(n / 2) + linearLogRecur(n / 2) for (i in 0..