Replacement Selection

王子轩

Date: 2024-6-8

Chapter 1: Introduction

Notice that as soon as the first record is written to an output tape, the memory it used becomes available for another record. Assume that we are sorting in ascending order, if the next record is not smaller than the record we have just output, then it can be included in the run.

Chapter 2: Algorithm Specification

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
Initialize arrays memo, tmp, and scan
Initialize pointers memo_tail, tmp_tail, scan_tail, and scan_head

Read input: n, m
Read the input sequence into the scan array

while scan_tail is not equal to 0:
if scan_tail is less than m - 1:
Set memo_tail to 1
while scan_head is not equal to scan_tail:
x = scan[scan_head]
Increment scan_head
memo[memo_tail] = x
Increment memo_tail
Sort memo[1] to memo[memo_tail-1]
else:
for i from 1 to m:
x = scan[scan_head]
Increment scan_head
memo[i] = x
Set memo_tail to m + 1
Sort memo[1] to memo[m]

Remove the first element from the scan array
lst = memo[1]
while scan_head is not equal to scan_tail:
x = scan[scan_head]
Increment scan_head
if x is less than lst:
tmp[tmp_tail] = x
Increment tmp_tail
else:
Insert x into the memo array
lst = memo[1]
Remove the first element from memo
Increment memo_tail

Reset scan_head and scan_tail pointers
while memo_tail is not equal to 1:
Remove the first element from memo
Print a newline character
for i from 0 to tmp_tail-1:
scan[scan_tail] = tmp[i]
Increment scan_tail
Reset tmp_tail pointer

Return 0

Function insert(x):
i = memo_tail
Increment memo_tail
while i / 2 > 0 and memo[i / 2] > x:
memo[i] = memo[i / 2]
i = i / 2
memo[i] = x

Function delete(flg):
if flg equals 1:
Print memo[1]
else:
Print " " + memo[1]
child = 2
par = 1
while child <= memo_tail - 1:
if child < memo_tail - 1 and memo[child + 1] < memo[child]:
child = child + 1
memo[par] = memo[child]
par = child
child = par * 2
memo[par] = memo[memo_tail - 1]
Decrement memo_tail

Chapter 3: Testing Results

input expect aim output
13 3
81 94 11 96 12 99 17 35 28 58 41 75 15
11 81 94 96 99
12 17 28 35 41 58 75
15
sample 11 81 94 96 99
12 17 28 35 41 58 75
15

Chapter 4: Analysis and Comments

Time Complexity Derivation:

  1. Initialization: This is a constant time operation, O(1), as it involves setting up pointers and arrays.

  2. Reading Input: Reading n and m is O(1), but reading the input sequence into the scan array is O(n), where n is the length of the input sequence.

  3. Main Loop:

    • The main loop runs until scan_tail is not equal to 0. The number of iterations of this loop depends on the input size, but let’s assume it runs k times.
    • Inside the loop, there are several operations:
      • Sorting memo: If we assume a simple comparison-based sort like quicksort or mergesort, the sorting operation would have a time complexity of O(m log m), where m is the size of the memo array.
      • Inserting elements into memo: The insert function likely has a complexity of O(log m) due to the binary heap structure.
      • Deleting elements from memo: The delete function also likely has a complexity of O(log m) for similar reasons.

    Assuming the sorting is the dominant factor, the time complexity inside the loop is O(m log m). Since the loop runs k times, the overall complexity of the loop is O(k * m log m).

  4. Resetting Pointers and Printing: These are constant time operations, O(1).

  5. Total Time Complexity: The total time complexity is the sum of all the individual complexities. Assuming k is proportional to n (the size of the input), the total time complexity is O(n + k * m log m). If k is approximately n, this simplifies to O(n * m log m).

Derivation of space complexity:

  1. Input Array scan: This array holds the input sequence and has a space complexity of O(n).

  2. Memoization Array memo: This array is used to store elements and sort them. Its size is based on m, so it has a space complexity of O(m).

  3. Temporary Array tmp: This array is used to store elements temporarily and has a space complexity of O(m).

  4. Pointers: The pointers used in the program are constant space, O(1).

  5. Total Space Complexity: The total space complexity is the sum of the spaces used by the arrays. Therefore, the total space complexity is O(n + m + m) = O(n + 2m), which simplifies to O(n + m) if m is considered a constant or if it grows with n.

Appendix: Source Code (in C)

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <stdio.h>

#include <stdlib.h>

// This function compares two integers and returns the difference between them.

int cmp(const void *a, const void *b)

{

    return *(int *)a - *(int *)b;

}

void insert(int x);

void delete(int flg);

int n, m;

int memo[20000]; // use min heap

// Declare arrays and pointers

int tmp[20000];

// Initialize scan array to store the input sequence

int scan[20000];

// Initialize pointers for memoization

int memo_tail;

int tmp_tail;

int scan_tail;

int scan_head;

// Initialize main function

int main()

{

    // Read input: number of elements and length of subsequence to be sorted

    scanf("%d %d", &n, &m);

    // Read input sequence into scan array

    while (scan_tail < n)

    {

        scanf("%d", &scan[scan_tail++]);

    }

    // Iterate through the input sequence

    while (scan_tail != 0)

    {

        // If the subsequence to be sorted is not of length m, process the next element

        if (scan_tail < m - 1)

        {

            // Initialize the memoization array

            memo_tail = 1;

            // Iterate through the subsequence to be sorted

            while(scan_head != scan_tail)

            {

                // Store the current element of the subsequence in tmp

                int x;

                x = scan[scan_head++];

                // Add the element to the memoization array

                memo[memo_tail++] = x;

            }

            // Sort the memoization array

            qsort(memo + 1, memo_tail - 1, sizeof(int), cmp);

        }

        else

        {

            //memo[1] = scan[scan_head++];

            //memo_tail = 1;

            //qsort(memo + 1, 0, sizeof(int), cmp);

            for (int i = 1; i <= m; i++)

            {

                int x;

                x = scan[scan_head++];

                memo[i] = x;

            }

            // Insert x into the sorted part of the memo array

            memo_tail = m + 1;

            // Sort the memo array

            qsort(memo + 1, m, sizeof(int), cmp);

        }

        // Delete the first element from the scan array

        delete (1);

        // Store the smallest element in the memo array

        int lst = memo[1];

        // Iterate through the scan array

        for (; scan_head != scan_tail;)

        {

            // Store the current element in the scan array

            int x = scan[scan_head++];

            // If the current element is smaller than the smallest element in the memo array

            if (x < lst)

            {

                // Add the current element to the temporary array

                tmp[tmp_tail++] = x;

                // Continue to the next element

                continue;

            }

            // Otherwise, insert the current element into the memo array

            else

            {

                insert(x);

            }

            // Store the smallest element in the memo array

            lst = memo[1];

            // Delete the first element from the memo array

            delete (0);

        }

        // Reset the scan array pointer

        scan_head = 0;

        // Reset the scan array pointer

        scan_tail = 0;

        // Iterate through the memo array

        while (memo_tail != 1)

        {

            // Delete the first element from the memo array

            delete (0);

        }

        // Print a new line

        printf("\n");

        // Iterate through the temporary array

        for (int i = 0; i < tmp_tail;)

        {

            // Add the current element from the temporary array to the scan array

            scan[scan_tail++] = tmp[i++];

        }

        // Reset the temporary array pointer

        tmp_tail = 0;

    }

    return 0;

}// This function inserts an element into the heap

void insert(int x)

{

    // Iterate through the heap

    int i;

    // Move the tail to the right

    for (i = memo_tail++; memo[i / 2] > x; i /= 2)

    {

        // Move the parent's value down the heap

        memo[i] = memo[i / 2];

    }

    // Insert the new value at its correct position

    memo[i] = x;

}

// This function deletes an element from the heap

void delete(int flg)

{

    // If the element is to be printed, print the root

    if (flg)

    {

        printf("%d", memo[1]);

    }

    // Otherwise, print the root followed by a space

    else

    {

        printf(" %d", memo[1]);

    }



    // Initialize the child and parent variables

    int child = 2;

    int par = 1;

    // Iterate through the heap

    while (child <= memo_tail - 1)

    {

        // If there is a right child, compare it with the left child

        if (child < memo_tail - 1)

        {

            if (memo[child + 1] < memo[child])

            {

                child++;

            }

        }

        // Move the parent's value down the heap

        memo[par] = memo[child];

        par = child;

        // Move the child to its correct position

        child = par * 2;

    }

    // Insert the deleted value at the end of the heap

    memo[par] = memo[memo_tail - 1];

    // Decrement the tail

    memo_tail--;

}

Declaration

I hereby declare that all the work done in this project titled “Replacement Selection” is of my independent effort.