Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

기수정렬 구현완료 #38

Merged
merged 2 commits into from
Aug 15, 2020
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
43 changes: 43 additions & 0 deletions yoonexample/src/main/java/sort/RadixSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package sort;

import queue.LinkedListQueue;
import queue.Queue;

public class RadixSort {

private static final int BUCKET_NUM = 10;

public void sort(int[] array, int maxLength) {
int divFac = 1;
Queue<Integer>[] buckets = new Queue[BUCKET_NUM];

// 버킷 배열 초기화
for (int i = 0; i < BUCKET_NUM; i++) {
buckets[i] = new LinkedListQueue<>();
}

// 가장 긴 데이터의 길이만큼 반복
for (int i = 0; i < maxLength; i++) {
// 정렬 대상 수의 개수만큼 반복
for (int num : array) {
// N번째 자리의 숫자 추출
int radix = (num / divFac) % 10;

// 추출한 숫자에 위치하도록 데이터 저장
buckets[radix].enqueue(num);
}

// 버킷 수 만큼 반복
for (int j = 0; j < BUCKET_NUM; j++) {
// 버킷에 저장되어 있는 것을 순서대로 꺼내서 다시 array에 저장
int arrayIndex = 0;
while (!buckets[j].isEmpty()) {
array[arrayIndex++] = buckets[j].dequeue();
}
}

// N번째 자리 숫자를 추출할 수 있도록 변경
divFac *= 10;
}
}
}
20 changes: 20 additions & 0 deletions yoonexample/src/test/java/sort/RadixSortTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package sort;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class RadixSortTest {

@Test
@DisplayName("기수_정렬_테스트")
void 기수_정렬_테스트() {
int[] array = {13, 212, 14, 7141, 10987, 6, 15};
int maxLength = 5;

RadixSort radixSort = new RadixSort();
radixSort.sort(array, maxLength);
assertThat(array).containsExactly(6, 13, 14, 15, 212, 7141, 10987);
}
}