12/12/2015

[swift] c++, srand, Incremental Sort, Randomize Array



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <Windows.h>    // rand
#include <iostream>        // cout
#include <iomanip>        // setw, setfill
using namespace std;
 
void SRAND(void);
void RandomizeArray(int *ary, const int& size, const int& range);
int IncrementalSort(int *arry, const int& size);
 
int main(void)
{
    SRAND();
    register int i = 0;
    int ary[10= { };
    register int arySize = sizeof(ary) / sizeof(ary[0]);
    {
        cout << setw(20<< setfill(' '<< "Initial Array : ";
        for (i = 0; i < arySize; ++i)
            cout << ary[i] << " ";
        cout << endl;
    }
 
 
    RandomizeArray(ary, arySize, 10);
    {
        cout << setw(20<< setfill(' '<< "Randomized Array : ";
        for (i = 0; i < arySize; ++i)
            cout << ary[i] << " ";
        cout << endl;
    }
 
    IncrementalSort(ary, arySize);
    {
        cout << setw(20<< setfill(' '<< "Incremental Sort : ";
        for (i = 0; i < arySize; ++i)
            cout << ary[i] << " ";
        cout << endl << endl;
    }
 
    return 0;
}
 
int IncrementalSort(int *arry, const int& size)
{
    register int iteration = 0// To analyze the time complexity
 
    for (register int i = 0; i < size; ++i)
    {
        register int min = arry[i], minIdx, toChange = false;
        for (register int j = i + 1; j < size; ++j)
        {
            ++iteration;
 
            if (arry[j] < min)
            {
                toChange = true;
                min = arry[j];
                minIdx = j;
            }
        }
 
        if (toChange)
        {
            register int temp = arry[i];
            arry[i] = arry[minIdx];
            arry[minIdx] = temp;
        }
    }
 
    return iteration;
}
 
void RandomizeArray(int *ary, const int& size, const int& range)
{
    for (register int i = 0; i < size; ++i)
        ary[i] = rand() % range;
}
 
void SRAND(void)
{
    LARGE_INTEGER SEED;
    ::QueryPerformanceCounter(&SEED);
    srand(static_cast<UINT>(SEED.QuadPart));
}
cs

댓글 없음:

댓글 쓰기