Tutorial_5_Sol-for-viewing-printing

Tutorial_5_Sol-for-viewing-printing - swapped = true; }...

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, 2009-2010 Page 1 of 2 Tutorial 5 Solutions Exercise 1 answer: (a) Bubble Sort (b) Pass 1 = 5, 9, 23, 6, 17, 13, 35, 12, 43 Pass 2 = 5, 9, 6, 17, 13, 23, 12, 35, 43 Pass 3 = 5, 6, 9, 13, 17, 12, 23, 35, 43 Pass 4 = 5, 6, 9, 13, 12, 17, 23, 35, 43 Pass 5 = 5, 6, 9, 12, 13, 17, 23, 35, 43 Pass 6 = 5, 6, 9, 12, 13, 17, 23, 35, 43 Pass 7 = 5, 6, 9, 12, 13, 17, 23, 35, 43 Pass 8 = 5, 6, 9, 12, 13, 17, 23, 35, 43 (c) public static void asort(int [] element) { int temp, s = 1; boolean swapped = true; for (int k = element.length - 1; k > 0 ; k--) { swapped = false; 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;
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: swapped = true; } System.out.print (&quot;Pass &quot; + s++ + &quot; = &quot;); show(element); } } (Note: other correct algorithm is acceptable.) (Note: Read the faster version of selection sort and faster version of bubble sort in the ppt files “Early-terminating Selection Sort.ppt” and “Early-terminating Bubble Sort.ppt”) The required java source codes are given in Test.java and FastTest.java files. To compile and execute, at the DOS prompt: javac Test.java java Test CC2203 Data Structures and Algorithms Sem 2, 2009-2010 Page 2 of 2 javac FastTest.java java FastTest Exercise 2 answer: Pass 1: 4 5 7 6 3 2 8 1 9 Pass 2: 4 5 6 3 2 7 1 8 9 Pass 3: 4 5 3 2 6 1 7 8 9 Pass 4: 4 3 2 5 1 6 7 8 9 Pass 5: 3 2 4 1 5 6 7 8 9 Pass 6: 2 3 1 4 5 6 7 8 9...
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_Sol-for-viewing-printing - swapped = true; }...

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