Вы находитесь на странице: 1из 22

UNIVERSITI TEKNOLOGI MALAYSIA

TEST 1
SEMESTER I 2010/2011

CODE OF SUBJECT : SCJ2013, SCK 2243
NAME OF SUBJECT : Data Structure and Algorithm
YEAR / COURSE : 2SCV, 2SCR, 2SCJ, 2SCI, 2SCD, 2SCB
TIME : 8.30 PM 10.30 PM (2 Hours)
DATE : 21 August 2010
VENUE : N24 BK8 & BK9 (Lecture Hall 8 & 9)

INSTRUCTIONS TO THE STUDENTS:

This test book consists of 3 parts:
Part A: 10 True/False Questions 10 marks
Part B: 10 Objective Questions 15 marks
Part C: 5 Structured Questions 75 marks

ANSWER ALL QUESTIONS IN THE SPACES ALLOCATED IN THIS TEST BOOK.
Name
Identity card (or matric) Number:
Name of Lecturer
Subject Code and Section
This examination book consists of 21 printed pages excluding this page.
Test 1 - Data Structure and Algorithm 1

PART A TRUE/FALSE QUESTIONS [10 marks]
[BAHAGIAN A SOALAN BENAR/ SALAH] [10 markah]

There are 10 questions in this section. For each statement given in this section, identify
whether the statement is TRUE/FALSE and write your answer in the space given in page
7. Each question carries 1 mark.
[Bahagian ini terdiri daripada 10 soalan. Bagi setiap kenyataan yang diberi, tentukan
samada BENAR/SALAH dan tuliskan jawapan anda pada ruang yang disediakan di
mukasurat 7. Setiap soalan bernilai 1 markah.]

1. Array and struct are examples of storage structure in structured data types.
[Tatasusunan dan struktur adalah contoh-contoh struktur storan dalam jenis data
berstruktur.]

2. If function void getValue() pass a parameter by reference from class
employee, the correct syntax prototype for this function can be written as:
[Jika fungsi void getValue() menghantar parameter secara rujukan berjenis
kelas employee maka sintaks yang betul bagi prototaip fungsi ini adalah: ]

void getValue(employee &);

3. Similar to constructor, destructor could not have return type but can have
parameters.
[Seperti konstruktor, distruktor tidak boleh mempunyai jenis pulangan, tetapi
boleh mempunyai parameter.]

4. If two programs perform the same task, then the faster program is always better.
[Jika dua atur cara melaksanakan tugas yang sama, maka atur cara yang lebih
pantas perlaksanaannya selalunya adalah atur cara yang lebih baik. ]

5. Retrieving an item at index 10 of an array is an example of a constant function.
[Capaian ke atas elemen pada indeks 10 pada sebuah tatasusunan merupakan
contoh fungsi konstan.]

6. A recursive function cannot call another function.
[Fungsi rekursif tidak boleh memanggil fungsi yang lain.]
7. The efficiency of a sorting algorithm is measured ONLY based on the number of
comparisons done during a sorting process.
[Keberkesanan sesuatu algoritma isihan diukur HANYA berdasarkan bilangan
perbandingan yang berlaku semasa isihan. ]

8. In any case of Selection Sort (worst case, best case or average case), the number
of comparisons between elements is the same.
[Bagi mana-mana kes Isihan Pilihan (kes terburuk, kes terbaik atau kes purata,
bilangan perbandingan di antara elemen adalah sama. ]
Test 1 - Data Structure and Algorithm 2

9. One of the differences between Quick sort and Merge sort is Quick Sort partition
the list based on the pivot value and Merge Sort partition the list by dividing the
list into two balanced list.
[Salah satu perbezaan antara Isihan Cepat dan Isihan Cantum ialah Isihan Cepat
membahagikan senarai berdasarkan nilai pivot dan Isihan Cantum
membahagikan senarai kepada dua senarai yang seimbang.]

10. Quicksort and Mergesort have the same time complexity for average case, which
is O(n * log
2
n) but Quicksort requires more computation space for second array.
[Isihan Cepat dan Isihan Cantum mempunyai masa kerumitan yang sama bagi
kes purata, iaitu O(n * log
2
n) tetapi Isihan Cepat memerlukan lebih ruang
komputasi untuk tatasusunan ke dua.]



PART B OBJECTIVE QUESTIONS [15 marks]
[BAHAGIAN B SOALAN OBJEKTIF] [15 markah]
Part B consists of 10 objective questions. Choose the correct answer, and write your
answer in the answer sheet provided in page 7 Each question carries 1.5 mark.
[Bahagian B mengandungi 10 soalan objektif. Pilih jawapan yang betul, dan jawab pada
kertas jawapan yang disediakan pada mukasurat 7. Setiap soalan bernilai 1.5 markah. ]

1. Given the following statement. Identify the testing level described.
[Diberikan kenyataan berikut. Kenalpasti aras pengujian yang dinyatakan.]

Involves building a system from its components or modules, and testing the system
for problems that arise from component interactions.

A. Unit Testing [Pengujiaan Unit]
B. Integration Testing [Pengujian Integrasi]
C. System Testing [Pengujian Sistem]
D. Accepteance Tesitng [Pengujian Penerimaan]

Question 2 is based on Figure 1 below.
[Soalan 2 adalah berdasarkan pada Rajah 1 berikut.]



Car
CarModel
Registration number
Year
Color
Move()
Brake()
Reverse()

Figure 1. Car [Rajah 1. Kereta]
Test 1 - Data Structure and Algorithm 3

2. Which of the following activity is done in Figure 1?
[Yang mana di antara aktiviti berikut dilakukan dalam Rajah 1?]

A. Encapsulation [Enkapsulasi]
B. Abstraction [Pengabstrakan]
C. Information hiding [Penyembunyian maklumat]
D. C++ class implementation [Implementasi class dalam C++]

3. Which of the following is NOT a structured data type?
[Yang mana di antara berikut BUKAN jenis struktur data?]

A. Storage [Storan]
B. Linked [Pautan]
C. Integral [Integral]
D. State [Keadaan]

Question 4 is based on Program 1.0.
[Soalan 4 adalah berdasarkan kepada Aturcara 1.0.]

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
// Program 1.0

class Info {
int a,b ;
public :
Info (int x,int y);
void function1(Info c);
};
Info::Info(int x,int y) {
a = x;
b = y;
}
void Info:: function1(Info d) {
a+=d.a;
b+=d.b;
}

main() {
Info obj1(2,4);
Info obj2(3,6);
obj1.function1(obj2);
}


4. Given Program 1.0, what is the final value for a and b of object obj1 after execution
of the program ?
[Diberi Program 1.0, apakah nilai terakhir bagi a dan b bagi objek obj1 setelah
aturcara ini dilaksanakan?]

A. a = 2 , b =4 C. a = 3, b = 6
B. a= 5, b= 10 D. a= 2, b=3
Test 1 - Data Structure and Algorithm 4

Question 5 is based on Program 2.0 which consists of constructor declaration for class
Kelas.
[Soalan 4 adalah berdasarkan pada Aturcara 2.0 yang merupakan pengishtiharan
konstruktor bagi kelas Kelas.]


1.
2.
3.
4.
5.
6.
7.
8.
// Program 2.0

class Kelas {
:
public:
Kelas (int, char = A, int = 50);

};


5. Which of the following statement for creating an object is INVALID?
[Yang mana di antara peryataan pengisytiharan objek berikut yang TIDAK SAH?]

A. Kelas dataStructure(C,100);
B. Kelas dataStructure (1);
C. Kelas dataStructure (1,B);
D. Kelas dataStructure (1,C,100);


6. How many times will function showMessage() in Program 3.0 calls itself, if 5 is
passed as the argument?
[Berapa kali kah fungsi showMessage() dalam Atur cara 3.0 memanggil dirinya
sendiri, jika nilai 5 dihantar sebagai argumen?]

1.
2.
3.
4.
5.
6.
7.
8.
9.
// Program 3.0

void showMessage(int n)
{ if (n > 0)
{
cout << "Good day!" << endl;
showMessage(n - 1);
}
}

A. 4
B. 5
C. 6
D. An infinite number of times
[Bilangan yang tak terhingga.]




Test 1 - Data Structure and Algorithm 5

Question 7 and 8 is based on Table 1.
[Soalan 7 dan 8 adalah berdasarkan kepada Jadual 1.]

Table 1 : Running Time [Jadual 1: Masa Larian]
Algorithm Running Time
Input Size O(n
2
) O(nlog
2
n) O(n)
n = 10 0.000004 0.000006 0.000003
n = 100 0.000109 0.000045 0.000006
n = 1000 0.010203 0.000485 0.000031
n = 10,000 1.2329 0.005712 0.000317

7. Which of the following is NOT represented by Table 1:
[Yang mana di antara berikut yang TIDAK diwakili oleh Jadual 1.]

A. The running time of 3 algorithms based on the growth rate of the input size.
[Masa larian bagi 3 algoritma berdasarkan kadar perkembangan dan saiz
input.]
B. The growth rate of the input size based on the running time.
[Kadar perkembangan bagi saiz input berdasarkan kepada masa larian.]
C. The running time for the algorithms increase as the problem size increase.
[Masa larian bagi semua algoritma bertambah apabila saiz masalah
bertambah.]
D. The complexity of 3 algorithms based on the input size.
[Kerumitan bagi 3 algoritma berdasarkan saiz input.]


8. Which of the following tell the efficiency of the algorithms in Table 1?
[Yang mana di antara berikut menerangkan keberkesanan bagi semua algoritma
dalam Jadual 1? ]

A. O(n) algorithm is the most efficient compare to the other algorithms.
[Algoritma O(n) merupakan yang paling berkesan berbanding algoritma yang
lain.]
B. O(n
2
) algorithm is more efficient compare to O(nlog
2
n) algorithms.
[Algoritma O(n
2
) paling berkesan berbanding algoritma O(nlog
2
n).]
C. O(nlog
2
n) algorithm is the most efficient compare to other algorithms.
[Algoritma O(nlog
2
n ) paling berkesan berbanding algoritma yang lain.]
D. It is hard to identify the efficiency of the algorithms from the data in the Table
1.
[Agak sukar untuk mengenalpasti keberkesanan algoritma-algoritma
berdasarkan data pada Jadual 1.]

Test 1 - Data Structure and Algorithm 6

9. Which of the following is TRUE for Simple Sort technique?


[Di antara berikut yang manakah BENAR mengenai teknik Isihan mudah?]

A. Both Bubble and Selection Sort are non-quadratic algorithms which are only
appropriate for small data sizes.
[Kedua-dua Isihan Buih dan Isihan Pilihan ialah algoritma bukan kuadratik
yang hanya sesuai bagi set data yang kecil.]
B. Main activities in sorting process are compare and swap. A comparison of
two elements of an array will always be followed by a swap.
[Aktiviti utama di dalam proses isihan ialah banding dan alih. Perbandingan
antara dua nombor dari tatasusunan akan sentiasa diikuti dengan aktiviti
peralihan.]
C. Selection Sort does not depend on the initial arrangement of data and has the
same time complexity for both worse case and best case analysis.
[Isihan Pilihan tidak bergantung kepada susunan awal data dan mempunyai
masa kerumitan yang sama bagi analisa kes terbaik dan kes terburuk.]
D. Insertion Sort works by selecting the largest/smallest item in an array and
place the item in its correct place.
[Dalam Isihan Selitan, item yang paling besar/paling kecil nilainya akan
dipilih daripada tatasusunan dan kemudian ditempatkan di posisi yang betul.]


10. Which of the following is NOT TRUE regarding the Quick sort analysis?
[Di antara berikut yang manakah TIDAK BENAR mengenai analisis Isihan
Cepat?]

A. The best case for Quick Sort happens when the list is partition into
balance segment.
[Kes terbaik untuk Isihan Cepat berlaku apabila senarai dibahagi kepada
segmen yang seimbang.]
B. The worst case for Quick Sort occurs when the smallest item or the largest
item always be chosen as pivot.
[Kes terburuk untuk Isihan Cepat berlaku bila item terkecil atau terbesar
sentiasa dipilih sebagai pivot.]
C. The average case is O(n * log
2
n).
[Kes purata ialah O(n * log
2
n).]
D. The worst case is O(n * log
2
n)
[Kes terburuk ialah O(n * log
2
n).]

Test 1 - Data Structure and Algorithm 7


Answer sheet for Part A and B:

Write all your answers for Section A and B in the tables below.
[Jawab semua soalan bahagian A dan B pada jadual-jadual di bawah.]

Name: _________________________________
Section: _______________________________






SECTION A
TRUE/FALSE
SECTION B
Multiple Choice

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

10

10

Total
Marks




Total
Marks



Test 1 - Data Structure and Algorithm 8


PART C - STRUCTURED QUESTIONS [75 marks]
BAHAGIAN C SOALAN STRUKTUR [75 markah]
Part C consists of 5 structured questions. Answer all questions in the space provided.
The marks for each part of the question is as indicated.
[Bahagian C mengandungi 5 soalan struktur. Jawab semua soalan pada ruang yang
disediakan. Markah setiap soalan adalah seperti yang dinyatakan. ]

Question 1 [17 marks]

Given definition of class point in Program 4.0. Answer question a to f of this section.
[Diberi definisi kelas point di dalam Program 4.0. Jawab soalan a hingga soalan f.]

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
// Program 4.0

class point {
double x;
double y;

public:
point(double initial_x=0.0, double initial_y=0.0);
void shift(double x_amount, double y_amount);
// to move the point by x_amount along the x axis
// and y_amount along the y axis
void rotate90(); // to rotate the point 90
// degree clockwise
void rotate_to_upper_right(point& p);
double get_x() const { return x; }
double get_y() const { return y; }
};


a) Write the constructor for the class point that initialize the values of x and y with the
passed arguments, initial_x and initial_y, respectively.
[Tuliskan konstruktor bagi kelas point yang akan memberi nilai awal bagi x dan y
dengan nilai-nilai yang dihantar iaitu initial_x dan initial_y. ]
[2 marks]










Test 1 - Data Structure and Algorithm 9

b) Outside the class declaration, write definition of the member function shift() that
modify values of x and y by summing up with the passed arguments, x_amount
and y_amount, respectively.
[Tuliskan ahli fungsi shift() di luar pengistiharan kelas di mana fungsi ini akan
mengubah nilai x dan y dengan menambahnya dengan nilai-nilai yang dihantar
iaitu, x_amount dan y_amount.]
[2 marks]











c) Given the rotate90() , rotated_to_upper_right()and main() functions as
shown below.
[Diberi fungsi rotate90(), rotated_to_upper_right()dan main() seperti
yang ditunjukkan di bawah.]










void point::rotate90()
{
double new_x, new_y;

new_x = y;
new_y = -x;
x = new_x;
y = new_y;
}

void point::rotated_to_upper_right(point p)
{
while ((p.get_x() < 0) || (p.get_y() < 0))
p.rotate90();
}
int main()
{ point p1(6, -4);
cout << "x coordinate is " << p1.get_x() << endl
<< "y coordinate is " << p1.get_y() << endl;
p1.rotate_to_upper_right(p1);
cout << "x coordinate is " << p1.get_x() << endl
<< "y coordinate is " << p1.get_y() << endl;
return 0;
}
Test 1 - Data Structure and Algorithm 10

Write the output after implementing the main() function and then justify your
answer.
[Tuliskan output selepas pelaksanaan fungsi main()ini. Kemudian terangkan
sebab terhasilnya output tersebut.]

The output:




[2 marks]



Justify your answer based on the output result.
[Terangkan sebab terhasilnya output tersebut.]

_________________________________________________________________
_________________________________________________________________
[2 marks]

d) Modify the rotated_to_upper_right()so that the parameter is passed by
reference:
[Ubahsuai fungsi rotated_to_upper_right() untuk melaksanakan
penghantaran secara rujukan.]






[2 marks]








Test 1 - Data Structure and Algorithm 11

e) Write a member function named horizontal_or_vertical() that passes two
reference parameters of object point. The function will check whether the points
being passed is a vertical line or a horizontal one. If the value of x for both
coordinates are equal, then it is a vertical line and the function will print It is a
vertical line. If the values of y for both coordinates are equal, then it is a
horizontal line and the function will print It is an horizontal line. Otherwise it
will print It is neither horizontal nor vertical. Figure 2 shows examples of vertical
and horizontal lines.
[Tuliskan satu ahli fungsi bernama horizontal_or_vertical() yang
menghantar dua parameter rujukan bagi kelas point. Fungsi ini akan memeriksa
samada objek-objek yang dihantar mewakili koordinat-koordinat bagi garisan
menegak atau melintang. Jika nilai x bagi kedua-dua koordinat adalah sama, maka
ia mewakili garisan menegak dan fungsi akan mencetak mesej It is a vertical
line. Sekiranya nilai y bagi kedua-dua koordinat adalah sama, bermakna ia
adalah garisan melintang dan fungsi akan mencetak mesej It is an horizontal
line. Jika selainnya mesej It is neither horizontal nor vertical akan dicetak.
Rajah 2 menunjukkan contoh-contoh garisan menegak dan melintang yang
dimaksudkan.]
[4 marks]

Figure 2










Test 1 - Data Structure and Algorithm 12

f) Given a piece of codes in Program 5. Answer the following question based on
these codes.
[Diberi keratan aturcara seperti di dalam Program 5. Jawab soalan berikut
berdasarkan aturcara yang diberi.]

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
// Program 5

point arrpoint [3];
double xx, yy;

for ( int i = 0 ; i < 5; i++ ) {
cout << "x coordinate for point" << i << endl;
cin >> xx;
pointarr[i].set_x(xx);
cout << "y coordinate for point" << i << endl;
cin >> yy;
pointarr[i].set_y(yy);
}

for ( int i = 0 ; i < 5; i++ ) {
cout << "x coordinate for point" << i
<< pointarr[i].get_x() << endl;
cout << "y coordinate for point" << i
<< pointarr[i].get_y() << endl;
}


Explain what happen in the first for loop (lines 6 - 13 of Program 1.2).
[Terangkan apa yang berlaku dalam gelung for pertama pada baris 6 13
dalam Program 1.2.]
[2 marks]
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________

Explain what happen in the second for loop (lines 15 - 20 of Program 1.2).
[Terangkan apa yang berlaku dalam gelung for kedua pada baris 15 20 dalam
Program 1.2.]
[1 mark]

_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
Test 1 - Data Structure and Algorithm 13

Question 2 [13 marks]

Answer all the following recursive questions. [Jawab semua soalan rekusif berikut.]

a) Describe the following recursive principle.
[Terangkan prinsip rekursif berikut.]

i) Base Case [Kes Penamat]




[1.5 marks]

ii) General Case [Kes Umum.]




[1.5 marks]


Answer the following question based on the function_1() below:
[Jawab soalan berikut berdasarkan fungsi function_1() yang diberikan berikut:]

int function_1(int m,int n)
{ if (n == 0)
return 1;
else
return (m * function_1(m,n - 5)+ 3);
} // end fun1


b-i) Based on the recursive function, function_1(), give the value being returned if
the following statement is executed.
[Berdasarkan fungsi rekursif, function_1() , berikan nilai yang akan
dikembalikan seandainya pernyataan berikut dilaksanakan. ]

cout << function_1(5,15);

[1 mark]






Test 1 - Data Structure and Algorithm 14

b-ii) Show the recursive trace for function_1(5,15)if the following statement is
executed. [Tunjukkan jejak rekursif bagi fungsi function_1() . seandainya
pernyataan berikut dilaksanakan. ]
cout << function_1(5,15);
[4 marks]

















b-iii) What will goes wrong with function_1() if function_1(5,12) is executed.
Give the reason why the situation occur?
[Nyatakan masalah yang berlaku ke atas function_1() seandainya
function_1(5,12) dilaksankan. Berikan penjelasan mengapa situasi ini
terjadi.]
[2 marks]








b-iv) Rewrite function_1() to handle the error. If value of variable n can be divided
by 5 (balance is 0), implement the recursive call, but if the number cannot be
divided by 5 (balance is not 0) the base case will return 1.
[Tulis semula implementasi bagi function_1() bagi menangani ralat tersebut.
Jika pembolehubah n boleh dibahagi dengan 5, laksanakan panggilan rekursif. Jika
nilai n tidak boleh dibahagi 5 (baki bukan 0) kes penamat akan pulangkan nilai 1.]
[3 marks]
Test 1 - Data Structure and Algorithm 15

Question 3 [15 marks]

Calculate the number of executions for every statement in the following algorithm.
Variable n in the program represents the number of input. Find the total executions and
determine the time complexity (Big O notation) for the program.
[Kira bilangan perlaksanaan bagi setiap pernyataan dalam algoritma berikut.
Pembolehubah n mewakili bilangan input. Seterusnya dapatkan jumlah perlaksanaan
dan tentukan masa kerumitan (dalam notasi O Besar) bagi aturcara tersebut.]

a)
for (j=1; j<=n; j=j*2)
{
cout << EXCELLENT!;
}

for (i=4; i<n; i++)
{
cout << GREAT!!!;
for (k=1; k<=n; k++)
cout << *******;
}


Statements Number of steps
for (j=1; j<=n; j=j*2)

{

cout << EXCELLENT!;

}

for (i=4; i<n; i ++)

{ cout << GREAT!!!;

for (k=1; k<=n; k++)

cout << *******;

}

Total steps =


Big O notation =

[ 6 marks]

Test 1 - Data Structure and Algorithm 16

b) Given the following source codes in Program 2, write number of steps based on the
execution of the program below.
[Diberi keratan atur cara pada Atur cara 2, tuliskan bilangan langkah berdasarkan
perlaksanaan atur cara tersebut.]

1
2
3
4
5
6
int i=n;
while (i>=1)
{ for (j=1; j<=i; j++)
cout << i << j;
i--;
}
Program 2

b-i) Fill in the following table by counting the number of steps.
[Isi jadual berikut dengan mengira bilangan langkah.] [5 marks]

Value for i
[Nilai bagi i]
Number of steps for internal loop
[Bilangan langkah bagi gelung dalaman]
for (j=1; j<=i; j++)
n
n-1
: :
3
2
1

b-ii) Total steps [jumlah langkah] = ____________________________________
[2 marks]

b-iii) Big O notation [notasi O besar] = _________________ [1 marks]

c) Give one algorithm that has the same approach and complexity as in Program 2.
[Berikan satu algorithma yng mempunyai pendekatan dan kerumitan seperti Atur
Cara 2.]
[1 mark]








Test 1 - Data Structure and Algorithm 17

Question 4 [15 marks]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Selection Sort function
void SelectionSort(int Data[], int n)
{ int temp, X, last;
for (last=n-1;last>=1;last--)
{ X = 0;
for (int p=1;p <= last; ++p)
{ if (Data[p]> Data[X])
X = p;
} // end for
temp = data[last];
data[last] = data[X};
data[X] = temp;
} // end for
} // end function

a. Given SelectionSort() function above, show step by step the tracing
implementation of the function on the Array X and Array Y below, by
completing the following diagrams with the correct values in each box. The
ArrayX and ArrayY are to be sorted ascendingly.
[Berdasarkan fungsi SelectionSort() di atas, tunjukkan langkah demi
langkah proses menjejaki perlaksanaan isihan ke atas tatasusunan X dan
tatasusunan Y dengan melengkapkan rajah di bawah dengan nilai-nilai yang
betul. Tatasusunan X dan tatasusunan Y hendaklah diisih secara menaik]

[5 marks]
Array X[] = [1,6,8,9]
Array Y[] = [9,8,6,1]



















1
6
8
9
[3]
[2]
[1]
[0]












pass=_______ pass=_______ pass=_______ Array Y
9
8
6
1
[3]
[2]
[1]
[0]












pass=_______ pass=_______ pass=_______ Array X
Test 1 - Data Structure and Algorithm 18

b. Compare the efficiency of the SelectionSort() algorithm when implemented
on Array X and Array Y in terms of number of comparison and swapping.
Give your discussion on the efficiency problem in the algorithm.
[Bandingkan keberkesanan algoritma isihan pilihan tersebut ke atas kedua-dua
senarai data pada tatasusunan X dan Y dari segi bilangan perbandingan dan
pertukaran. Berikan huraian anda berkenaan dengan masalah keberkesanan
dalam algoritma ini. ]
[4 marks]
Pass
Array X Array X
Number of
comparisons

Number of
swapping


Discussion









c. Modify and rewrite the above SelectionSort() function to improve the
implementation efficiency on Array X and Array Y as discussed in your answer
in Question b. [Ubahsuai dan tulis semula fungsi SelectionSort() di atas
memperbaiki keberkesanan perlaksanaan ke atas tatasusunan X dan Y
berdasarkan jawapan anda di Soalan b.]
[3 marks]













d. Compare the efficiency of the above original SelectionSort() algorithm with
your modified function algorithm in terms of number of comparison and
swapping. [Bandingkan keberkesanan algoritma isihan pilihan di atas dengan
fungsi yang telah anda ubah suai dari segi bilangan perbandingan dan
pertukaran.]
[3 marks]

Test 1 - Data Structure and Algorithm 19

Question 5 [15 marks]

a. Briefly describe the basic idea of problem solving using Divide and Conquer Sorting
Algorithms strategy.
[ Terangkan dengan ringkas perlaksanaan strategi Algoritma Isihan Pecah dan
Takluk.]
[ 2 marks]






b. Both Merge Sort and Quick Sort are Divide and Conquer Sorting algorithms and the
algorithms sort a list by partitioning the list. Referring to the following partitioning
code Explain how Merge Sort differs from Quick Sort in partitioning the list.
[Kedua-dua Isihan Cantum dan Isihan Cepat adalah algortima Pecah dan Takluk
dan algoritma-algoritma ini mengisih senarai dengan pembahagikan senarai.
Merujuk kepada kod pembahagikan berikut terangkan bagaimana Isihan Cantum
berbeza daripada Isihan Cepat dalam pembahagikan senarai.]
[ 3 marks]































void mergeSort(DataType theArray[],int first,int last){
if (first < last) {
int mid = (first + last)/2;
mergesort(theArray, first, mid);
mergesort(theArray, mid+1, last);
merge(theArray, first, mid, last);
}
}
void quickSort (dataType arrayT[], int first , int last){
int cut;
if (first<last){
cut = partition(T, first,last);
quickSort(T, first,cut);
quickSort (T, cut+1, last);
}
}
Test 1 - Data Structure and Algorithm 20

c. Given the following array named theArray with 5 elements. Draw the diagram
that shows the detail process of sorting the data in ascending order using Merge
Sort technique. Make sure you number the sequence of the function call (use
functions name in Question b.).
[Diberikan tatasusunanberikut bernama theArray yang menyimpan 5 nilai. Lukis
gambar rajah perlaksanaan Isihan Cantum langkah demi langkah bagi mengisih
tatasususunan tersebut secara menaik. Pastikan anda menumborkan jujukan
panggilan fungsi (guna nama fungsi seperti di Soalan b.).]
[ 5 marks]

[0] [1] [2] [3] [4]
theArray 30 5 42 77 56













































Test 1 - Data Structure and Algorithm 21

d. Given the following named theArray with 5 elements. Draw the diagram that shows
the detail process of sorting the data in ascending order using Quick Sort technique.
Use PIVOT as the MIDDLE element of the array list. Make sure you number the
sequence of the function call (use function name in Question b.) and identify value
cut for each function partition().
[Diberikan tatasusunanberikut bernama theArray yang menyimpan 5 nilai. Lukis
gambar rajah perlaksanaan Isihan Cantum langkah demi langkah bagi mengisih
tatasususunan tersebut secara menaik.Gunakan nilai PIVOT dari nilai TENGAH
elemen senarai tatsusunan. Pastikan anda menumborkan jujukan panggilan fungsi
(guna nama fungsi seperti di Soalan b.). dan mengenalpasti nilai cut untuk setiap
fungsi partition().]

[ 5 marks]

Вам также может понравиться