기본 콘텐츠로 건너뛰기

[백준 10989] 수정렬하기 feat. Quick Sort VS Counting Sort 비교

 *QuickSort 버전*

#include <iostream>
#include <string>
using namespace std;

void swap(int arr[], int a, int b)
{
    int temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp;
}

int partition(int arr[], int left, int right)
{
    int pivot = arr[left];
    int low = left + 1;
    int high = right;
    
    while(low <= high)
    {
        while(low <= right && pivot >= arr[low])
            low++;
        
        while(high >= left+1 && pivot <= arr[high])
            high--;
        
        if(low <= high)
        {
            swap(arr, low, high);
        }
    }
    
    swap(arr, left, high);
    
    return high;
}

void quickSort(int arr[], int left, int right)
{
    if(left <= right)
    {
        int pivot = partition(arr, left, right);
        quickSort(arr, left, pivot-1);
        quickSort(arr, pivot+1, right);
    }

}
int main(int argc, const char * argv[]) {
    int n;
    cin >> n;
    
    int * arr = new int[n];
    
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    
    quickSort(arr, 0, n-1);
    
    for (int i = 0; i < n; i++)
        cout << arr[i] << endl;
}


런타임에러가 난다. 더 빠른 알고리즘이 필요하다.

*Counting Sort 버전*


#include<iostream>


using namespace std;

int cnt[10001];

int main()
{
    int n;
    cin >> n;
    
    for(int i=0; i<n; i++)
    {
        int input;
        cin >> input;
        cnt[input]++;
    }
    
    for(int i=1; i<=10000; i++)
    {
        if(cnt[i] >0)
            for(int j=0; j<cnt[i]; j++)
                printf("%d\n",i);
    }
    
    
    return 0;

}

Array Sorting Algorithms

Algorithm
Time ComplexitySpace Complexity
BestAverageWorstWorst
QuicksortΩ(n log(n))Θ(n log(n))O(n^2)O(log(n))
MergesortΩ(n log(n))Θ(n log(n))O(n log(n))O(n)
TimsortΩ(n)Θ(n log(n))O(n log(n))O(n)
HeapsortΩ(n log(n))Θ(n log(n))O(n log(n))O(1)
Bubble SortΩ(n)Θ(n^2)O(n^2)O(1)
Insertion SortΩ(n)Θ(n^2)O(n^2)O(1)
Selection SortΩ(n^2)Θ(n^2)O(n^2)O(1)
Tree SortΩ(n log(n))Θ(n log(n))O(n^2)O(n)
Shell SortΩ(n log(n))Θ(n(log(n))^2)O(n(log(n))^2)O(1)
Bucket SortΩ(n+k)Θ(n+k)O(n^2)O(n)
Radix SortΩ(nk)Θ(nk)O(nk)O(n+k)
Counting SortΩ(n+k)Θ(n+k)O(n+k)O(k)
CubesortΩ(n)Θ(n log(n))O(n log(n))O(n)

댓글

이 블로그의 인기 게시물

Tree traversal의 3가지

1. 전위 순회 (Preorder Traversal) Root -> Left Tree -> Right Tree   ( 루트를 제일 처음에 방문 ) 2. 중위 순회 (Inorder Traversal) Left Tree -> Root -> Right Tree   ( 루트를 중간에 방문 ) 3. 후위 순회 (Postorder Traversal) Left Tree -> Right Tree -> Root   ( 루트를 제일 마지막에 방문 ) <소스코드> 출처 : http://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder /// C program for different tree traversals #include <stdio.h> #include <stdlib.h> /* A binary tree node has data, pointer to left child    and a pointer to right child */ struct node {      int data;      struct node* left;      struct node* right; }; /* Helper function that allocates a new node with the    given data and NULL left and right pointers. */ struct node* newNode(int data) {      struct node* node = (struct node*)                 ...