forked from xtaci/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshuffle_demo.cpp
More file actions
36 lines (29 loc) · 770 Bytes
/
shuffle_demo.cpp
File metadata and controls
36 lines (29 loc) · 770 Bytes
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
32
33
34
35
/*******************************************
* DANIEL'S PRIVATE ALGORITHM IMPLEMENTAIONS
* Fisher Yates's shuffle algorithm
* Features:
* 1. shuffle list in O(n) time.
*******************************************/
#include <stdio.h>
#include <stdlib.h>
#include "generic.h"
#include "shuffle.h"
using namespace alg;
int main()
{
const int MAX_ELEMENTS = 10;
int list[MAX_ELEMENTS];
int i = 0;
// generate random numbers and fill them to the list
for(i = 0; i < MAX_ELEMENTS; i++ ){
list[i] = i;
}
printf("The list before Shuffle is:\n");
printlist(list,MAX_ELEMENTS);
// shuffle the list
shuffle<int>(list,MAX_ELEMENTS);
// print the result
printf("The list after using Yates' shuffle algorithm:\n");
printlist(list,MAX_ELEMENTS);
return 0;
}