Tutorial_5_v02-for-viewing-printing - asort(num(a Specify...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CC2203 Data Structures and Algorithms Sem 2, 2010-2011 Page 1 of 2 Tutorial 5 Instruction In this tutorial, you are required to submit your tutorial work answers for the exercises to me. Tutorial work will not be graded unless your coursework fails (coursework below D grade). If one’s coursework fails, that tutorial work will be graded to see if condone pass should be issued or not. Exercise 1 Given the following Java code: public class Test { public static void show(int [] arr) { for (int i = 0; i < arr.length-1; i++) System.out.print(arr[i] + ", "); System.out.println(arr[arr.length-1]); } public static void asort(int [] element) { int temp, s = 1; for (int k = element.length - 1; k > 0; k--) { for (int i = 0; i < k; i++) if (element[i] > element[i+1]) { temp = element[i]; element[i] = element[i+1]; element[i+1] = temp; } System.out.print ("Pass " + s++ + " = "); show(element); } } public static void main(String [] args) { int [] num = {5, 9, 23, 35, 6, 17, 13, 43, 12 };
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: asort(num); } } (a) Specify the name of the sorting method carried out by asort() method. (b) Show the output after executing java Test . (c) A better algorithm for asort() method is that it can terminate the sorting process earlier once it has found the elements in the array are in ascending order. Modify the asort() method to this early terminating version. CC2203 Data Structures and Algorithms Sem 2, 2010-2011 Page 2 of 2 Exercise 2 If the bubble sort is used for the following sequence of integers: 4, 7, 5, 9, 6, 3, 2, 8, 1 show the output of the first six bubbling passes. Review 1. Download bookprog.zip from Moodle. 2. Doubel-click bookprog.zip to extract bookprog folder. The bookprog folder contains all Java codes presented in the lectures. 3. Read the corresponding lecture slides ( Lesson Notes 6) for the compilation and execution. Understand those Java source codes, compile and execute them....
View Full Document

This note was uploaded on 08/18/2011 for the course COMP 3868 taught by Professor Keithchan during the Summer '97 term at Hong Kong Polytechnic University.

Page1 / 2

Tutorial_5_v02-for-viewing-printing - asort(num(a Specify...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online