728x90
https://www.hackerrank.com/challenges/quicksort1/problem
Quicksort 1 - Partition | HackerRank
Perform the first step of Quicksort: partitioning an array.
www.hackerrank.com
Algorithms > Sorting
배열이 주어지면 퀵소트로 정렬해 리턴한다.
코드
int* quickSort(int arr_count, int* arr, int* result_count) {
*result_count = arr_count;
int *temp = (int*) malloc(arr_count * sizeof(int));
int i, now = 0;
for(i = 1; i < arr_count; i++){
if(arr[i] < arr[0]){
*(temp + now) = arr[i];
now++;
arr[i] = 0;
}
}
*(temp + now) = arr[0];
for(i = 1; i < arr_count; i++){
if(arr[i]){
now++;
*(temp + now) = arr[i];
}
}
return temp;
}
설명
먼저 동적 배열 temp를 선언하고 활용했다.
for문을 이용해 주어진 배열 arr를 탐색하며 작은 값을 찾고, 해당 인덱스를 now에 저장한다.
모든 과정이 끝나면 temp를 리턴한다.
728x90
'C & C++ > HackerRank' 카테고리의 다른 글
[HackerRank] The Power Sum (0) | 2021.09.09 |
---|---|
[HackerRank] Staircase (0) | 2021.08.29 |
[HackerRank] 2D Array - DS (0) | 2021.08.29 |
[HackerRank] Print in Reverse (0) | 2021.08.29 |
[HackerRank] Tree: Level Order Traversal (0) | 2021.08.13 |