0% found this document useful (0 votes)
4 views3 pages

SecondaryStorage en

Uploaded by

pxhuy2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

SecondaryStorage en

Uploaded by

pxhuy2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Vietnam National University, Ho Chi Minh City

Ho Chi Minh City University of Technology


268 Ly Thuong Kiet Street, District 10, Ho Chi Minh City, Viet Nam

Subject: Introduction to Computing


Quiz: Secondary Storage
Once data becomes large, the kind of storage (primary/secondary) matters. This
quiz will illustrate it.

Problem 1: Data insertion

Assume that we have a file named SortedNum.txt used to store integers sorted in
descending order. Suppose that the current content of SortedNum.txt is as
{2,4,7,9,16} and we need to insert 3 into this file. Obviously, 3 should be inserted
between 2 and 4.1

However, this means that we need to “move” all the numbers of 4,7,9,16 to a new
location on the storage. Each of those numbers needs to move to a new position
according to its storage size, to reserve a right place for the newly added data of 3.
Let us consider the below example.

Starting: |2|4|7|9|16|
Moving: |2| |4|7|9|16
Insertion of 3: |2|3|4|7|9|16

Thus, in order to move one number, we need one reading and one writing operation.
For this example, we need a total of 4 times of reading operation and 5 of writing
operation.

Table 1 shows us the needed time for reading/writing operation once applied in
secondary storage (HDD) and primary (memory).

Table 1 – Needed time (estimated) for reading/writing on storage devices


Reading from HDD Writing to HDD Reading from Writing to
memory memory
50 1000 3 3

We then have 2 methods:

- Method 1: All operations will be performed on the secondary storage. Thus, the
needed time is: 4 x 50 + 5 x 1000 = 5200.

- Method 2: Copy the whole file into memory, perform the moving actions and insert
the new element of 3 on the memory. Afterwards, all of the data will be copied from
memory to file. Total requited time would be: 5 x 50 (5 times of reading operation
from HDD) + 5 x 3 (5 times writing to memory) + 4 x (3+3) (4 times of reading and
Vietnam National University, Ho Chi Minh City
Ho Chi Minh City University of Technology
268 Ly Thuong Kiet Street, District 10, Ho Chi Minh City, Viet Nam

writing for the “moving” actions) + 3 (writing new element into memory) + 6 x 1000
(6 times writing to secondary storage) = 6292.

Fill the missing information in Table 2.

Bảng 2 -Estimation of reading/writing operations


Initial array 2,4,7,9,16
Elements to be Method 1 Method 2
added
1
8
18
1,8,18
18,8,1

Problem 2: Sorting data

In Problem 1, the array stored in the file is already sorted. If the file is unsorted, we
need to sort it.

We choose the following approach. Read N elements of the file (N times reading),
select the smallest element, swap it with the element at the 1 st position (2 times
writing, we *ignore the temporary reading operations). After that, we read N-1
remaining elements, and swap the smallest element with the element at the 2 nd
position. We keep doing so until reaching the last element of the array. Observe the
following example for better illustration.

<12 4 27 9> ---> Read 4 times, select 4 as the smallest one


4 12 27 9 --> swap --> 2 times writing
4 <12 27 9> --> read 3 times, select 9 as the smallest one
4 9 27 12 --> swap --> 2 times writing
4 9 <27 12> --> read 2 times, select 12 as the smallest one
4 9 12 27 --> swap --> 2 times writing

Thus, there are a total of 9 times reading and 6 times writing.

We also try the 2 above methods. With Method 1, we perform all of the operations
on the disk. With Method 2, we copy all data into the memory, sort all data in the
memory and finally copy all data back to the memory.

Fill the missing information in Table 3.

Bảng 3 – Estimation of sorting cost


Array Method 1 Method 2
Vietnam National University, Ho Chi Minh City
Ho Chi Minh City University of Technology
268 Ly Thuong Kiet Street, District 10, Ho Chi Minh City, Viet Nam

2,4,7,9,16
16,9,7,4,2
2,4,7,19,6
2,19,4,7,6

Homeworks

Problem 3: Challenge with Big Data

The results obtained from Problem 2 show that if we load all data from the
secondary devices into the primary memory, we will generally get better
performance. However, when data becomes extremely large, we may not load all
data into memory for processing due to the capacity limitation.

Let us consider the following challenge. If the array to be sorted includes the
elements of 2,19,4,7,6,8,24,17 and the memory is only sufficient to store 3 elements
at the most (i.e. the rest must be stored in the secondary devices).

Try to develop a solution which makes minimal costs. Write down the cost incurred
by your solution.

Cost to sort the above array is .....

The team who proposes a GENERAL solution (proved by report) will earn a bonus in
the final midterm (submit the report by sending an email to me).

You might also like