[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๊ณผ์ œ 4. 1000๋งŒ๊ฐœ ๋ฐ์ดํ„ฐ ํ€ต ์ •๋ ฌ ์‹œ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„

Quick sort

'์•Œ๊ณ ๋ฆฌ์ฆ˜' ์ „๊ณต ์ˆ˜์—…์‹œ๊ฐ„์— ๋‚˜์˜จ ๊ณผ์ œ์ธ '1000๋งŒ๊ฐœ ๋ฐ์ดํ„ฐ ์ •๋ ฌ ํ›„ ํ•ด์‹œ ๊ฐ’ ๊ตฌํ•˜๊ธฐ'๋ฅผ ํ•˜๋ฉด์„œ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ด๋‹ค.

๋ถ„ํ•  ์ •๋ณต ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๊ตฌํ˜„๋˜๋Š” ์ •๋ ฌ ๋ฐฉ๋ฒ•

์‹œ๊ฐ„ ๋ณต์žก๋„

์ตœ์•… : O(n^2)

ํ‰๊ท  : O(nlogn)

์„ค๊ณ„๋ฅผ ํ†ตํ•ด์„œ ๊ฐœ์„  ๊ฐ€๋Šฅํ•œ ๋ถ€๋ถ„

๊ตฌํ˜„ ๊ณผ์ •

  1. pivot์„ ์„ ํƒํ•œ๋‹ค.
  2. ์œ„์ชฝ ํฌ์ธํ„ฐ๊ฐ€ ๋‚ฎ์€ ์ชฝ์œผ๋กœ ๊ฐ€๋ฉด์„œ ํ”ผ๋ฒ—๋ณด๋‹ค ์ž‘์€ ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค.
  3. ์•„๋ž˜์ชฝ ํฌ์ธํ„ฐ๊ฐ€ ๋†’์€ ์ชฝ์œผ๋กœ ๊ฐ€๋ฉด์„œ ํ”ผ๋ฒ—๋ณด๋‹ค ํฐ ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค.
  4. ๋‘ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์š”์†Œ๋ฅผ ๊ตํ™˜ํ•œ๋‹ค.
  5. 2-4๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.
  6. ๋‘ ํฌ์ธํ„ฐ๊ฐ€ ๊ต์ฐจํ•œ๋‹ค๋ฉด ํ˜„์žฌ ํ”ผ๋ฒ—๊ณผ ํ•ด๋‹น ์œ„์น˜์˜ ๊ฐ’์„ ๊ตํ™˜ํ•œ๋‹ค.
  7. ํ”ผ๋ฒ—๊ณผ ํ”ผ๋ฒ—๋ณด๋‹ค ์ž‘์€ section, ํ”ผ๋ฒ—๋ณด๋‹ค ํฐ section์œผ๋กœ ๋‚˜๋ˆ„๊ณ  ๊ฐ section์— ๋Œ€ํ•ด 1-5๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
    1. section์˜ ํฌ๊ธฐ๊ฐ€ 0 ๋˜๋Š” 1์ด๋ฉด ๊ทธ๋ƒฅ return ํ•œ๋‹ค.

์„ค๊ณ„๋ฅผ ํ†ตํ•œ ๊ฐœ์„ 

pivot ์„ ํƒ ๊ณผ์ •์—์„œ ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๊ณ„์†ํ•ด์„œ ์„ ํƒํ•ด ๋‚˜๊ฐ„๋‹ค๋ฉด ์—ญ์ˆœ์œผ๋กœ ๋ฐฐ์น˜๋˜์—ˆ์„ ๋•Œ, ์ตœ์•…์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ๊ธฐ์กด ์ฝ”๋“œ์—์„œ ์ค‘๊ฐ„ ์š”์†Œ๋ฅผ ์ฒซ๋ฒˆ์งธ ์š”์†Œ์™€ ๊ตํ™˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ•จ์œผ๋กœ์จ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค๋ฅธ nlogn๋ณด๋‹ค ๋น ๋ฅธ ์ด์œ 

๋จผ ๊ฑฐ๋ฆฌ ๊ตํ™˜ ์ฒ˜๋ฆฌ์™€ ์บ์‹œ ํšจ์œจ

์บ์‹œ ํšจ์œจ : ํ•œ ๋ฒˆ ์„ ํƒ๋œ ๊ธฐ์ค€์€ ์ œ์™ธ

์ •๋ ฌ๋œ ๊ฐ’์€ ์ œ์™ธํ•˜๊ณ  ๋‚จ์€ ๊ฐ’๋งŒ ์ •๋ ฌํ•˜๋ฉด ๋จ

๋ฉ”๋ชจ๋ฆฌ ์ฐธ์กฐ๊ฐ€ ์ง€์—ญํ™”๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— CPU ์บ์‹œ์˜ ํžˆํŠธ์œจ์ด ๋†’์•„์ง€๊ธฐ ๋•Œ๋ฌธ

๋ฉ”๋ชจ๋ฆฌ

O(log n)๋งŒํผ์˜ ๋ฉ”๋ชจ๋ฆฌ ํ•„์š”

๋ถˆ์•ˆ์ • ์ •๋ ฌ

์›์†Œ๋“ค ์ค‘์— ๊ฐ™์€ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๊ฐ™์€ ๊ฐ’๋“ค์˜ ์ •๋ ฌ ์ดํ›„ ์ˆœ์„œ๊ฐ€ ์ดˆ๊ธฐ ์ˆœ์„œ์™€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์–ด ๋ถˆ์•ˆ์ • ์ •๋ ฌ์— ์†ํ•œ๋‹ค.

ํฐ ์ˆ˜๋ฅผ ๋‹ค๋ฃจ๋Š” ํ€ต์†ŒํŠธ ๊ตฌํ˜„

/*
2020-03-26 ์šฐํฌ์€
ํ€ต์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜
*/

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>

typedef struct _MY_OBJ {
    int s, e;
}obj;

int buffer[10000000]; // ์ „์—ญ ๋ณ€์ˆ˜๋กœ ๋„ฃ์–ด์•ผ stack overflow๊ฐ€ ๋‚˜์ง€ ์•Š์Œ//
obj obj_arr[10000000];//
obj tmp_arr[10000000];//
int index = 0;


void fileOpen() {
    FILE *fp = fopen("d://algorithm/unsorted10000000.txt", "r");//
    int i = 0;
    int tmp = 0;
    if (fp == NULL) {
        printf("ํŒŒ์ผ์„ ์—ด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\n");
    }
    while (fscanf(fp, "%d ", &tmp) != EOF) {
        buffer[i++] = tmp;
    }

    fclose(fp);
}
/*
void swap(int a, int b) { 
// swap์„ ์“ฐ๋ฉด stack over flow๊ฐ€ ๋‚˜์™€์„œ ๊ทธ๋ƒฅ ์ง์ ‘ ๋ฐ”๊พธ๋Š” ์‹์œผ๋กœ ๋ฐ”๊พธ์—ˆ๋‹ค.
    int tmp = buffer[a];
    buffer[a] = buffer[b];
    buffer[b] = tmp;
}
*/
void QuickSorting(int left, int right) {
    srand((unsigned int)time(NULL));
    int pivot_index = rand() % 9999999 + 1; //
    int tmp;
    int start, end;
    // ๋žœ๋ค์œผ๋กœ pickํ•œ pivot ๊ฐ’๊ณผ ์ฒซ๋ฒˆ์งธ ๊ฐ’ ๋ฐ”๊พธ๊ธฐ
    tmp = buffer[left];
    buffer[left] = buffer[pivot_index];
    buffer[pivot_index] = tmp;

    obj_arr[index].s = left; obj_arr[index++].e = right;
    do {
        int size = index;
        index = 0;
        for (int i = 0; i < size; i++) {
            left = obj_arr[i].s; right = obj_arr[i].e;

            if (right - left < 4) { // ์‚ฝ์ž… ์ •๋ ฌ์ด์šฉ
                for (int j = left + 1; j < right + 1; j++) {
                    int key = buffer[j];
                    int k;
                    for (k = j - 1; k >= left && buffer[k] > key; k--) {
                        buffer[k + 1] = buffer[k];
                    }
                    buffer[k + 1] = key;
                }
                continue;
            }
            start = left; end = right + 1;
            pivot_index = left + (right - left) / 2;
            tmp = buffer[left];
            buffer[left] = buffer[pivot_index];
            buffer[pivot_index] = tmp;

            do {
                do {
                    start++;
                } while (buffer[start] <= buffer[left] && left <= start);
                do {
                    end--;
                } while (buffer[end] > buffer[left] && right >= end);
                if (start <= end) {
                    tmp = buffer[start];
                    buffer[start] = buffer[end];
                    buffer[end] = tmp;
                }
                else
                    break;
            } while (1);

            if (left != end) {
                tmp = buffer[end];
                buffer[end] = buffer[left];
                buffer[left] = tmp;
            }

            if (end - 1 - left>0) {
                tmp_arr[index].s = left; tmp_arr[index++].e = end - 1;
            }
            if (right - end - 1>0) {
                tmp_arr[index].s = end + 1; tmp_arr[index++].e = right;
            }
        }
        for (int i = 0; i < index; i++) {
            obj_arr[i] = tmp_arr[i];
        }
    } while (index);
}

int calHash(int index_start, int index_end) {
    int sum = 0;
    for (int i = index_start; i < index_end + 1; i++) {
        sum += buffer[i] - (int)((int)(buffer[i] * pow(10, -5)) * pow(10, 5));
    }
    sum -= (int)((int)(sum * pow(10, -5)) * pow(10, 5));
    return sum;
}

int main() {
    printf("์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณผ์ œ 2\n");
    printf("              ์ปดํ“จํ„ฐ๊ณผํ•™๋ถ€ 2017920038 ์šฐํฌ์€\n");
    printf("ใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…ก\n");
    clock_t start, end;
    int n = 10000000; // ๊ณ„์‚ฐํ•  ์ˆ˜์˜ ๋ฒ”์œ„//

                      /* ํŒŒ์ผ ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ */
    fileOpen();

    /* ์‹œ๊ฐ„ ์ธก์ • */
    start = clock(NULL);

    printf("- ์ปดํ“จํ„ฐ CPU : Intel(R) Core(TM) i7-7500U CPU @ 2.70Ghz \n");
    printf("- ๋ฉ”๋ชจ๋ฆฌ : 8GB\n");

    /* ์‚ฝ์ž… ์ •๋ ฌ */
    QuickSorting(0, n - 1);

    end = clock(NULL);
    printf("- ๋™์ž‘์‹œ๊ฐ„: %lfms\n", (double)(end - start));

    /* hash ๊ณ„์‚ฐ */
    printf("- hash value: %d\n", calHash(4999999, 5000099));//4999999, 5000099

    return 0;
}

์žฌ๊ท€ ํ•จ์ˆ˜ ์‹คํ–‰ ์‹œ stackoverflow ์ด๋ฏ€๋กœ obj ๊ตฌ์กฐ์ฒด์˜ ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์Šคํƒ์ฒ˜๋Ÿผ ๊ตฌํ˜„ํ•˜๊ฒŒ ํ•˜์˜€๊ณ , swap ํ•จ์ˆ˜๋„ stackoverflow๋ฅผ ์ผ์œผํ‚ค๋ฏ€๋กœ ์ผ์ผ์ด ๋‹ค ํ’€์–ด์„œ ์จ์ฃผ์—ˆ๋‹ค.

์‚ฝ์ž… ์ •๋ ฌ๊ณผ์˜ ๋น„๊ต

์‚ฝ์ž… ์ •๋ ฌ : 15097000ms (251๋ถ„)

ํ€ต ์ •๋ ฌ : 1016ms (1์ดˆ)

๋ฌด๋ ค 1500๋ฐฐ ์ฐจ์ด์˜€๋‹ค.

์ปดํ“จํ„ฐ ์‚ฌ์–‘

์ปดํ“จํ„ฐ CPU : Intel(R) Core(TM) i7-7500U CPU @ 2.70Ghz

๋ฉ”๋ชจ๋ฆฌ : 8GB

๋ฐ˜์‘ํ˜•