Skip to content
Open
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
6 changes: 6 additions & 0 deletions practice/mergesort/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# These are explicitly windows files and should use crlf
*.bat text eol=crlf

5 changes: 5 additions & 0 deletions practice/mergesort/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
30 changes: 30 additions & 0 deletions practice/mergesort/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
* User Manual available at https://docs.gradle.org/7.0/userguide/building_java_projects.html
*/

plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13.1'

// This dependency is used by the application.
implementation 'com.google.guava:guava:30.0-jre'
}

application {
// Define the main class for the application.
mainClass = 'mergesort.App'
}
72 changes: 72 additions & 0 deletions practice/mergesort/app/src/main/java/mergesort/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package mergesort;

public class App {
public static void main(String[] args) {
int[] array = {3,9,4,7,5,0,1,6,8,2};

int[] resultArray = new App().solution(array);

System.out.print("resultArray : ");
for (int a: resultArray) {
System.out.print(a + ", ");
}
System.out.println();
}

public void mergeSort(int[] arr, int[] tmp, int start, int end) {
// start = end 같을 경우, 원소가 한개이므로, sort할 필요 없다.
// 따라서, start < end 인 경우에만, sort!
if(start < end) {
int mid = (start + end) / 2;

// 왼쪽 정렬
mergeSort(arr, tmp, start, mid);

// 오른쪼 정렬
mergeSort(arr, tmp, mid + 1, end);

// 이미 정렬된 배열 병합
merge(arr, tmp, start, mid, end);
}
}

private void merge(int[] arr, int[] tmp, int start, int mid, int end) {
// 정렬된 배열을 임시저장소에 필요한 만큼 복사 --> why? 비교할 때 기존 값을 보존하기 위해,
for (int i = start; i <= end; i++) {
tmp[i] = arr[i];
}

int part1FirstIndex = start;
int part2FirstIndex = mid + 1;

int resultArrayIndex = start;

while (part1FirstIndex <= mid && part2FirstIndex <= end) {
if(tmp[part1FirstIndex] <= tmp[part2FirstIndex]) {
arr[resultArrayIndex] = tmp[part1FirstIndex];
part1FirstIndex++;
} else {
arr[resultArrayIndex] = tmp[part2FirstIndex];
part2FirstIndex++;
}
resultArrayIndex++;
}

// part1에 데이터가 남아 있고 part2는 비어있는 경우(예 : 5,7,9 / 1, 2) : for문 돌려주기
// cf. part1에 비어있고, part2가 남아 있는 경우에는 for문이 필요 없다. 왜냐하면 이미 정렬되어 있기 때문에
for (int i = 0; i <= mid - part1FirstIndex; i++) {
arr[resultArrayIndex + i] = tmp[part1FirstIndex + i];
}
}

public int[] solution(int[] arr) {
int[] tmp = new int[arr.length];

mergeSort(arr, tmp, 0, arr.length - 1);

return arr;
}
}
24 changes: 24 additions & 0 deletions practice/mergesort/app/src/test/java/mergesort/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package mergesort;

import org.junit.Test;
import static org.junit.Assert.*;

public class AppTest {

@Test
public void testMergeSort() {
// given
int[] array = {3,9,4,7,5,0,1,6,8,2};

// when
int[] resultArray = new App().solution(array);

// then
int[] expectedArray = {0,1,2,3,4,5,6,7,8,9};

assertArrayEquals(expectedArray, resultArray);
}
}
Binary file not shown.
5 changes: 5 additions & 0 deletions practice/mergesort/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
185 changes: 185 additions & 0 deletions practice/mergesort/gradlew

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

Loading