diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 88ea3aa..3cc336b 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,6 +1,22 @@ + + diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..61a9130 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 5cd135a..9bba60d 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -14,6 +14,7 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index a76efc8..9b4a637 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index fdf1fc8..94a25f7 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,7 +1,6 @@ - \ No newline at end of file diff --git a/app/src/main/java/com/example/algorithmstudy/algorithm/SortUtil.kt b/app/src/main/java/com/example/algorithmstudy/algorithm/SortUtil.kt new file mode 100644 index 0000000..ba302ab --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/algorithm/SortUtil.kt @@ -0,0 +1,70 @@ +package com.example.algorithmstudy.algorithm + +import java.util.* + +class SortUtil { + + // 시간복잡도 O(n^2) + // 공간복잡도 O(n) + fun doSelectionSort(target: ArrayList): ArrayList { + var tempInt: Int + for (x in target.indices) { + tempInt = target[x] + + for (y in x + 1 until target.size) { + if (target[y] < tempInt) { + tempInt = target[y] + + // swap + target[y] = target[x] + target[x] = tempInt + } + } + } + + return target + } + + /** + * https://www.youtube.com/watch?v=g-PGLbMth_g + */ + // 시간복잡도 O(n^2) + // 공간복잡도 O(n) + fun doInsertionSort(arr: IntArray): IntArray { + var currentMinimum: Int + var tempInt: Int + + for (x in arr.indices) { + currentMinimum = x + + for (y in x + 1 until arr.size) { + if (arr[currentMinimum] > arr[y]) { + currentMinimum = y + } + } + + tempInt = arr[x] + arr[x] = arr[currentMinimum] + arr[currentMinimum] = tempInt + } + + return arr + } + + fun doBubbleSort(arr: IntArray): IntArray { + var tempInt: Int + + for (x in 1 until arr.size) { + + for (y in 0 until arr.size - 1) { + if (arr[x] < arr[y]) { + tempInt = arr[x] + arr[x] = arr[y] + arr[y] = tempInt + } + } + } + + return arr + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/Car.kt b/app/src/main/java/com/example/algorithmstudy/pattern/creation/Car.kt similarity index 93% rename from app/src/main/java/com/example/algorithmstudy/pattern/Car.kt rename to app/src/main/java/com/example/algorithmstudy/pattern/creation/Car.kt index f310359..4c54fd0 100644 --- a/app/src/main/java/com/example/algorithmstudy/pattern/Car.kt +++ b/app/src/main/java/com/example/algorithmstudy/pattern/creation/Car.kt @@ -1,6 +1,4 @@ -package com.example.algorithmstudy.pattern - -import kotlin.properties.Delegates +package com.example.algorithmstudy.pattern.creation /** * Builder 패턴 (Creational pattern) diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/creation/DynamicFactory.kt b/app/src/main/java/com/example/algorithmstudy/pattern/creation/DynamicFactory.kt new file mode 100644 index 0000000..c9f0367 --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/creation/DynamicFactory.kt @@ -0,0 +1,27 @@ +package com.example.algorithmstudy.pattern.creation + +/** + * 동적 생성이 가능한 Factory로 newInstance의 파라미터에 따라 다른 객체를 생성해 반환. + */ +class DynamicFactory { + + fun getInstance(type: String): Any { + return when (type) { + "Korean" -> Korean() + "English" -> English() + else -> Korean() + } + } + + class Korean() { + fun getLanuage(): String { + return "korean" + } + } + + class English() { + fun getLanuage(): String { + return "english" + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/creation/FactoryMethod.kt b/app/src/main/java/com/example/algorithmstudy/pattern/creation/FactoryMethod.kt new file mode 100644 index 0000000..6dc7b13 --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/creation/FactoryMethod.kt @@ -0,0 +1,33 @@ +package com.example.algorithmstudy.pattern.creation + +/** + * 매개변수 팩토리 메소드 패턴 + * + * - create에 별도로 parameter를 지정하지 않는 경우 단순 팩토리 메소드 패턴이 됨. + * - create에 파라미터를 지정함으로써 원하는 제품을 생성해 반환하도록 함. + */ +abstract class FactoryMethod { + enum class ProductModel { + SAMSUMG, + LG + } + + fun create(model: ProductModel): Product { + return when (model) { + ProductModel.SAMSUMG -> SamsumgProduct() + ProductModel.LG -> LgProduct() + } + } +} + +class LgProduct : Product { + override fun createProduct() { + println("Created LG Product") + } +} + +class SamsumgProduct : Product { + override fun createProduct() { + println("Created Samsung Product") + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/creation/Product.kt b/app/src/main/java/com/example/algorithmstudy/pattern/creation/Product.kt new file mode 100644 index 0000000..6b07694 --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/creation/Product.kt @@ -0,0 +1,5 @@ +package com.example.algorithmstudy.pattern.creation + +interface Product { + fun createProduct() +} \ No newline at end of file diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/creation/SimpleFactory.kt b/app/src/main/java/com/example/algorithmstudy/pattern/creation/SimpleFactory.kt new file mode 100644 index 0000000..8cbcefe --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/creation/SimpleFactory.kt @@ -0,0 +1,13 @@ +package com.example.algorithmstudy.pattern.creation + +/** + * 가장 단순한 Factory 패턴 + * + * - getInstance() 로 생성을 위임하고, 생성한 객체를 넘겨 + */ +class SimpleFactory { + + fun getInstance(): SimpleFactory { + return SimpleFactory() + } +} \ No newline at end of file diff --git a/app/src/test/java/com/example/algorithmstudy/PatternTest.kt b/app/src/test/java/com/example/algorithmstudy/PatternTest.kt index ef71011..eb775a4 100644 --- a/app/src/test/java/com/example/algorithmstudy/PatternTest.kt +++ b/app/src/test/java/com/example/algorithmstudy/PatternTest.kt @@ -1,6 +1,6 @@ package com.example.algorithmstudy -import com.example.algorithmstudy.pattern.Car +import com.example.algorithmstudy.pattern.creation.Car import org.junit.Test class PatternTest { diff --git a/app/src/test/java/com/example/algorithmstudy/SortTest.kt b/app/src/test/java/com/example/algorithmstudy/SortTest.kt index 930d705..bd6e923 100644 --- a/app/src/test/java/com/example/algorithmstudy/SortTest.kt +++ b/app/src/test/java/com/example/algorithmstudy/SortTest.kt @@ -37,6 +37,11 @@ class SortTest { println() +<<<<<<< HEAD + SortUtil().doBubbleSort(intArrayOf(9, 4, 3, 104, 1, 2)).forEach { + print("[$it]") + } +======= SortUtil() .doBubbleSort(intArrayOf(9, 4, 3, 104, 1, 2)).forEach { print("[$it]") @@ -70,5 +75,6 @@ class SortTest { fun findInt() { SortUtil() .findBigInt(intArrayOf(6, 10 ,2)) +>>>>>>> f1fd7ca0bbd56f9bfe1aba468d0722f805e61396 } } \ No newline at end of file