이 자습서에서는 기본 정렬이 작동하는 방법을 배웁니다. 또한 C 언어로 된 예제를 찾을 수 있습니다.
카디널리티 정렬은 먼저 같은 위치에있는 숫자를 그룹화하여 요소를 정렬하는 정렬 기술입니다. 그런 다음 증가 / 감소 순서에 따라 요소를 정렬합니다.
8 개 요소의 배열이 있다고 가정합니다. 첫째, 우리는 요소의 값에 따라 요소를 정렬 할 것입니다. 그런 다음 10 자리 값에 따라 요소를 정렬합니다. 이 프로세스는 마지막 유효한 숫자까지 계속됩니다.
초기 배열은 [121, 432, 564, 23, 1, 45, 788]입니다. 아래 그림과 같이 기수별로 정렬하십시오.
이 기사를 읽기 전에 계수 정렬이 기수 정렬에서 중간 정렬로 사용되기 때문에 먼저 계수 정렬을 살펴보십시오.
기수 정렬은 어떻게 작동합니까?
- 배열에서 최대 인 가장 큰 요소를 찾습니다. X를 max의 자릿수라고합니다. X는 모든 요소의 모든 유효한 비트를 통과해야하기 때문에 계산됩니다.
배열 [121,432,564,23,1,45,788]에서 가장 큰 숫자는 788입니다. 3 개의 숫자가 있습니다. 따라서 루프는 100 위 (3 회)까지 실행되어야합니다. - 이제 유효한 각 숫자를 하나씩 확인하십시오.
안정적인 정렬 기술을 사용하여 유효한 각 위치의 숫자를 정렬 할 수 있습니다. 우리는 계산 및 분류 방법을 사용했습니다.
1의 숫자에 따라 요소를 정렬합니다.
- 이제 10 자리 숫자에 따라 요소를 정렬합니다.
- 마지막으로 100 자리 숫자에 따라 요소를 정렬합니다.
기수 정렬 알고리즘
radixSort(array)
d <- maximum number of digits in the largest element
create d buckets of size 0-9
for i <- 0 to d
sort the elements according to ith place digits using countingSort
countingSort(array, d)
max <- find largest element among dth place elements
initialize count array with all zeros
for j <- 0 to size
find the total count of each unique digit in dth place of elements and
store the count at jth index in count array
for i <- 1 to max
find the cumulative sum and store it in count array itself
for j <- size down to 1
restore the elements to array
decrease count of each element restored by 1
C 예
// Radix Sort in C Programming
#include <stdio.h>
// Function to get the largest element from an array
int getMax(int array[], int n) {
int max = array[0];
for (int i = 1; i < n; i++)
if (array[i] > max)
max = array[i];
return max;
}
// Using counting sort to sort the elements in the basis of significant places
void countingSort(int array[], int size, int place) {
int output[size + 1];
int max = (array[0] / place) % 10;
for (int i = 1; i < size; i++) {
if (((array[i] / place) % 10) > max)
max = array[i];
}
int count[max + 1];
for (int i = 0; i < max; ++i)
count[i] = 0;
// Calculate count of elements
for (int i = 0; i < size; i++)
count[(array[i] / place) % 10]++;
// Calculate cummulative count
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
// Place the elements in sorted order
for (int i = size - 1; i >= 0; i--) {
output[count[(array[i] / place) % 10] - 1] = array[i];
count[(array[i] / place) % 10]--;
}
for (int i = 0; i < size; i++)
array[i] = output[i];
}
// Main function to implement radix sort
void radixsort(int array[], int size) {
// Get maximum element
int max = getMax(array, size);
// Apply counting sort to sort elements based on place value.
for (int place = 1; max / place > 0; place *= 10)
countingSort(array, size, place);
}
// Print an array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
// Driver code
int main() {
int array[] = {
121, 432, 564, 23, 1, 45, 788};
int n = sizeof(array) / sizeof(array[0]);
radixsort(array, n);
printArray(array, n);
}
복잡성
기수 정렬은 비 비교 정렬 알고리즘이므로 비교 정렬 알고리즘에 비해 장점이 있습니다.
계수 정렬을 중간 안정 정렬로 사용하는 기본 정렬의 경우 시간 복잡도는 O (d (n + k))입니다.
여기서 d는 사이클 수이고 O (n + k)는 계산 및 정렬의 시간 복잡도입니다.
따라서 기수 정렬은 선형 시간 복잡도를 가지며 이는 비교 정렬 알고리즘의 O (nlog n)보다 낫습니다.
매우 큰 숫자 나 32 비트 또는 64 비트 숫자와 같은 기타 기본 숫자를 사용하는 경우 선형 시간으로 실행될 수 있지만 중간 정렬은 많은 공간을 차지합니다.
이것은 기수 정렬 공간을 비효율적으로 만듭니다. 이것이이 방법이 소프트웨어 라이브러리에서 사용되지 않는 이유입니다.
카디널리티 정렬 애플리케이션
카디널리티 정렬이 적용됩니다.
- DC3 알고리즘이 접미사 배열을 만들 때.
- 다양한 숫자가있는 곳.
참조 문서
[1] Parewa Labs Pvt. Ltd. Radix Sort Algorithm [EB / OL] .https : //www.programiz.com/dsa/radix-sort,2020-01-01.