Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions app/src/main/java/com/example/algorithmstudy/algorithm/SortUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.example.algorithmstudy.algorithm

import java.util.*

class SortUtil {

// 시간복잡도 O(n^2)
// 공간복잡도 O(n)
fun doSelectionSort(target: ArrayList<Int>): ArrayList<Int> {
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
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.example.algorithmstudy.pattern

import kotlin.properties.Delegates
package com.example.algorithmstudy.pattern.creation

/**
* Builder 패턴 (Creational pattern)
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -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")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.algorithmstudy.pattern.creation

interface Product {
fun createProduct()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.algorithmstudy.pattern.creation

/**
* 가장 단순한 Factory 패턴
*
* - getInstance() 로 생성을 위임하고, 생성한 객체를 넘겨
*/
class SimpleFactory {

fun getInstance(): SimpleFactory {
return SimpleFactory()
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions app/src/test/java/com/example/algorithmstudy/SortTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
Expand Down Expand Up @@ -70,5 +75,6 @@ class SortTest {
fun findInt() {
SortUtil()
.findBigInt(intArrayOf(6, 10 ,2))
>>>>>>> f1fd7ca0bbd56f9bfe1aba468d0722f805e61396
}
}