Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

악어새와 좀개구리밥

[C언어] Fisher-Yates Shuffle 본문

알고리즘

[C언어] Fisher-Yates Shuffle

tiobi 2021. 10. 12. 16:22
728x90
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_ARRAY_SIZE 500

void swap(int* a, int* b) {
    int temp = *b;
    *b = *a;
    *a = temp;
}

int main() {
    int i, j;
    int ar[MAX_ARRAY_SIZE] = { NULL, };

    //Array에 data 저장
    for (i = 0; i < MAX_ARRAY_SIZE; i++)
        ar[i] = i + 1;

    //Fisher-Yates Shuffle
    srand((unsigned)time(NULL));
    for (i = 0; i < MAX_ARRAY_SIZE - 2; i++) {
        j = rand() % MAX_ARRAY_SIZE;
        swap(&ar[j], &ar[i]);
    }

    for (i = 0; i < MAX_ARRAY_SIZE; i++) 
        printf("%d ", ar[i]);

    return 0;
}
Comments