Lab 4 : Sorting Techniques Objectives Write source codes for the implementation of simple sort algorithms : Bubble Sort, Insertion Sort and Selection Sort. [Tulis aturcara dengan melaksanakan isihan mudah : Isihan Buih, Isihan Selitan dan Isihan Pilihan] Write source codes for the implementation of divide and conquer sorting algotithms : Merge Sort and Quick Sort. [Tulis aturcara dengan melaksanakan isihan pecah dan takluk : Isihan Pantas dan Isihan Gabung.] Identify the efficiency of the sorting algorithms and determine the suitable sorting technique for certain problem. [Mengenalpasti keberkesanan setiap jenis isihan dan berupaya memilih algoritma isihan yang paling berkesan untuk sesuatu masalah.] Lab Exercises Lab 1 : Simple Sort Write a program that will sort 25 integers in ascending order using simple sort algorithms: Bubble Sort, Insertion Sort and Selection Sort. For each sorting techniques learned in class, modify the algorithms so that the programs are able to count and print the number of passes, the number of data comparisons and the number of data swapping that take place in the sorting process. [Hasilkan satu aturcara yang boleh mengisih 25 nilai integer secara menaik dengan menggunakan teknik isihan mudah Isihan Selitan, Isihan Pilihan dan Isihan Buih. Ubahsuai algoritma isihan yang dipelajari dalam kelas supaya dapat mengira dan mencetak jumlah laluan, jumlah perbandingan data dan jumlah pertukaran data semasa proses isihan dilaksanakan.] a. Run the programs on the following list of integer values. [Larikan atur cara dengan menggunakan data pengujian berikut.] Data set 1 : 100 50 88 30 60 45 25 12 10 5 98 15 65 55 45 70 20 90 66 22 120 48 35 85 2 Data set 2 : 5 8 30 25 35 40 42 50 55 22 24 60 66 70 75 78 80 88 95 100 118 98 120 122 121
b. Data set 1 is an example of a worse case data totally unsorted list, while data set 2 is an example of a best case data almost sorted list. Analyze the output displayed from each sorting technique based on the number of passes, number of data comparison and number of data swapping. Make your conclusion on which sorting technique is the best for worse case data and which sorting technique is the best for best case data. Which technique has performance that does not depend on the initial arrangement of data? [Set data 1 merupakan contoh data bagi kes terburuk (data sama sekali tidak terisih), manakala set data 2 merupakan contoh data bagi kes terbaik (data hampir terisih). Analisa output yang anda perolehi yang memaparkan jumlah laluan, jumlah perbandingan data dan jumlah pertukaran data semasa proses isihan dilaksanakan. Berikan kesimpulan anda mengenai teknik isihan manakah yang paling berkesan digunakan untuk mengisih data bagi kes terbaik dan teknik isihan manakah yang paling berkesan dilaksanakan ke atas kes terburuk? Teknik manakah yang tidak dipengaruhi oleh mana-mana keadaan susunan data?] Lab 2 : Divide and Conquer Technique Write a program that is able to create 1000 integer values randomly and without duplicates. Save the data into a data file. Write another program to sort the data into ascending order using divide and conquer technique: Merge Sort and Quick Sort. Get the running times to sort the data using both techniques. Draw a conclusion of which technique is the fastest based on the sorting time observed from the output.. [Tulis satu atur cara yang boleh menghasilkan 1000 data integer secara rawak dan simpan data tersebut dalam satu fail data. Dengan menggunakan data yang telah dihasilkan, tulis satu lagi atur cara yang akan mengisih data tersebut secara menaik dengan menggunakan teknik isihan pecah dan takluk, iaitu Isihan Cepat dan Isihan Cantum. Bagi setiap teknik isihan tersebut, dapatkan masa bagi mengisih senarai data tersebut. Buat perbandingan dari segi kepantasan masa isihan bagi kedua-kedua teknik isihan tersebut. Apakah kesimpulan yang anda dapat rumuskan berdasarkan masa isihan yang anda perolehi. Teknik manakah yang lebih pantas?] Lab 3 : Sorting an Array of Object Given a class diagram for Student as below: Student Student_Name:char[] Matric_No:char[] Cpa : float register() : void getcpa() : float printdetail() : void a. Write class declaration for Student based on the attributes and member functions given in the class diagram. b. Write the implementation of the member function based on the following specifications: 2
register() read values for Student_Name, Matric_No and cpa. getcpa() return a float value for Cpa printdetail() - print values for Student_Name, Matric_No and cpa. c. Complete the Bubble Sort algorithm below to sort the list of Student into descending order based on the student s cpa. void BubbleSort( data[],int listsize) { int x ; Student tempvalue; for ( pass =1;pass < listsize; pass++ ) { for (x = 0; x < listsize - pass; x++) { //compare adjacent list based on student s cpa if // swap if the data is not in the right order { // end if // end for loop (internal) // end for loop (external) // end BubbleSort() function d. Write main program that consists of the following: Declare an array of Student with size 10. Read values for 10 Students and print the values of the array to show the content of the array before sort. Call Bubble Sort algoritm as coded in 3c and send appropriate parameter to the function. Print the array of students after the array has been sorted. 3
Exercises Exercise 1 : Simple Sort Program 4.1 is Bubble Sort algorithm: [Keratan aturcara 4.1 merupakan atur cara Isihan Buih. ] 1. // Program 4.1 2. // Bubble Sort 3. void BubbleSort(dataType data[], int listsize) 4. { int pass, tempvalue; 5. for ( pass =1;pass < listsize; pass++ ) 6. { 7. // moves the largest element to the 8. // end of the array 9. for (int x = 0; x < listsize - pass; x++) 10. //compare adjacent elements 11. if ( data[x]>data[x+1] ) 12. {//swap elements 13. tempvalue = data[x]; 14. data[x] = data[x+1]; 15. data[x+1] = tempvalue; 16. // end if 17. // end for 18. // end Bubble Sort a. Using the following set of data, show step by step of implementing Bubble Sort in program 4.1 on the data. Compare the efficiency of the algorithm. [Bandingkan keberkesanan algoritma isihan buih pada atur cara 4.1, ke atas senarai data berikut. Tunjukkan langkah demi langkah proses isihan ke atas data tersebut.] [ 3 5 7 6 9 8 ] dan [ 8 9 6 7 5 3 ] b. Based on your answer given in 1a, give reason why Bubble Sort algorithm in program 4.1 is not efficient. Explain how the program can be improved. [Berdasarkan jawapan yang diberi pada 1a, huraikan mengapa algoritma isihan buih pada program 4.1 dianggap kurang berkesan. Jelaskan bagaimana algoritma tersebut boleh ditingkatkan keberkesanannya.] c. Rewrite program 4.1 in order to improve the efficiency. [Tuliskan semula algoritma isihan buih di atas yang telah diubah suai supaya keberkesanannya dapat dipertingkatkan.] d. Given the following data set: [Diberi set data berikut;] [ 5 3 8 9 1 7 2 ] Show step by step the process of sorting the data using the following sorting techniques: [Tunjukkan langkah demi langkah operasi isihan ke atas data tersebut dengan menggunakan teknik berikut:] 4
Selection Sort. [Isihan Pilihan.] Insertion Sort [Isihan Selitan.] Exercise 2 a. What is the difference between Bubble Sort and Selection Sort operation? [Apakah perbezaan di antara operasi Isihan Buih dan Isihan Pilihan?] b. Trace the Selection Sort as it sorts the following array into descending order. [Tunjukkan langkah demi langkah bagi mengisih senarai dengan menggunakan Isihan Pilihan dalam susunan menurun.] 20, 80, 40, 25, 60, 30 c. Given the following list of keys, [Diberikan senarai unsur seperti berikut:] 28, 18, 21, 10, 25, 30, 12, 71, 32, 58, 15 This list is to be sorted using the Insertion Sort algorithm. Show the resulting list for the first 5 passes of the sorting phase. [Senarai di atas akan diisih dengan menggunakan algorithma Isihan Selitan. Tunjukkan hasil susunan unsur sehingga laluan ke-5 dilalui.] 5 Exercise 3 Given function Sort in program 4.2 and answer question a f. [Diberi keratan fungsi Sort di bawah, jawab soalan a-f.] 1. // Program 4.2 2. // function Sort: English Version 3. void Sort(int Data[], int n) 4. { int temp, X, last; 5. for (last=n-1;last>=1;last--) 6. { X = 0; 7. for (int p=1;p <= last; ++p) 8. { if (Data[p]< Data[X]) 9. X = p; 10. // end for 11. temp = data[last]; 12. data[last] = data[x; 13. data[x] = temp; 14. // end for 15. // end Function Sort a) Based on the function Sort given above, show step by step the implementation of the sorting algorithm in sorting array X with 6 values. [Berdasarkan kepada fungsi Sort yang diberi, tunjukkan langkah demi langkah proses isihan yang berlaku ke atas tatasusunan X yang bersaiz 6]
X : [ 3 5 7 6 8 9 ] b) Name the sorting algorithm performed by function Sort. [Namakan Teknik isihan yang dilaksanakan oleh fungsi Sort.] c) Give the sorting order performed by the sorting algorithm (ascending/descending). [Nyatakan urutan isihan yang dilaksanakan dalam teknik isihan ini (susunan menaik/menurun) ] d) Describe in detail how the sorting process is implemented in function Sort. [Terangkan dengan jelas bagaimana proses isihan dilaksanakan oleh fungsi SORT]. e) Reapply function Sort to sort array Y below. Show step by step implementation of the sorting algorithm in sorting array Y with 6 values. [Berdasarkan kepada fungsi Sort yang diberi, tunjukkan semula langkah demi langkah bagi mengisih tatasusunan Y yang bersaiz 6.] Y : [ 9 8 7 6 5 3 ] f) Compare the efficiency of the algorithm when implemented on array X and Y. Give your conclusion. [Bandingkan keberkesanan algoritma isihan tersebut ke atas kedua-dua senarai data pada tatasusunan X dan Y dan berikan kesimpulan anda. ] Exercise 4 : Divide and Conquer Technique a. Explain what is divide and conquer technique. [Apakah yang dimaksudkan dengan pendekatan pecah dan takluk.] b. Give 2 similarities between Quick Sort and Merge Sort. [Nyatakan 2 persamaan di antara isihan cantum dan isihan cepat.] c. Explain the difference between the dividing operation in Merge-sort and Quick-sort. [Terangkan perbezaan bagi operasi pembahagian untuk Isihan Cantum dan Isihan Cepat.] d. What is the drawback of the mergesort with respect to storage? [Apakah kelemahan algoritma Isihan Cantum dari segi penggunaan ruang ingatan?] e. Give 3 main steps in Merge Sort and explain. [Nyatakan 3 langkah utama isihan cantum dan perihalkan.] Exercise 5 : Quick Sort Program 4.3 is a Quick Sort function 6
[Atur cara 4.3 merupakan fungsi Isihan Cepat.] // Program 4.3 // Quick Sort function int partition(int T[], int first,int last) { int pivot, temp; int loop, cutpoint, bottom, top; pivot=t[first]; bottom=first; top= last; loop=1; //always TRUE while (loop) { while (T[top]>pivot) { top--; while(t[bottom]<pivot) { bottom++; if (bottom<top) { temp=t[bottom]; T[bottom]=T[top]; T[top]=temp; else { loop=0; cutpoint = top; //end while return cutpoint; 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); a. Explain the purpose of the following loops in function partition. [Terangkan apakah yang dilakukan oleh kedua-dua gelung yang terdapat dalam fungsi partition.] while (T[top]>pivot) { top--; while(t[bottom]<pivot) { bottom++; 7 b. Given an array T [40 10 20 80 60 5 50 55], draw diagram that shows how partition function is implemented using partition(t,0,7). [Berdasarkan tatasusunan [40 10 20 80 60 5 50 55] tersebut, lukiskan gambar rajah untuk menunjukkan proses bahagi bagi fungsi partition(t,0,7).]
quicksort(t,0,7) partition(t,0,7)= [0] [1] [2] [3] [4] [5] [6] [7] T 40 10 20 80 60 5 50 55 bottom top c. Draw a complete diagram that show how Quick Sort is performed on the array. [Lukis gambar rajah perlaksanaan isihan cepat langkah demi langkah bagi tatasususunan di atas. ] d. Is the input data T [40 10 20 80 60 5 50 55], is a best case for Quick Sort? [Adakah kes input tatasusunan [40 10 20 80 60 5] adalah kes terbaik? ] e. Give your reason to justify your answer in c. [ Berikan alasan bagi jawapan yang diberi pada c.] f. Explain the worse case condition for Quick Sort. Give example. [Huraikan bagaimana kes terburuk bagi isihan cepat berlaku. Berikan contoh.] Exercise 6 : Merge Sort Given an array named Data with 8 integer values. [Diberikan tatasusunan Data menyimpan 8 nilai integer] [0] [1] [2] [3] [4] [5] [6] [7] Data 70 15 85 25 8 16 22 35 a. Draw a complete diagram that show how Merge Sort is performed on the array. [Lukis gambar rajah perlaksanaan isihan cantum langkah demi langkah bagi tatasususunan Data di atas. Pastikan pada lukisan anda setiap fungsi lengkap dengan nama, nilai parameter fungsi dan nombor langkah bila fungsi dipanggil.] References: Nor Bahiah et al. Struktur Data & Algoritma Menggunakan C++. UTM Press, 2005 Past year Exam Questions for SCK2243/SCJ 2043 8