Thursday, June 17, 2010

DPBO – Final Exam (Programming): Generic Bubble Sort (Problem A2 and C2)

This post is the second part of DPBO practical final exam on programming.  For other parts, please see:
This problem appears as both Problem A2 and C2. The problem description is given below:
You are given a Bubble Sort sorting program. This program provides a sorting method below.

[sourcecode language="java" gutter="false" toolbar="false"]
public static void sort(int var[]) {
int temp;
for (int i=var.length-1; i>1; i--) {
for (int j=0; j < i; j++) {
if (var[j] > var[j+1])  {
temp = var[j];
var[j] = var[j+1];
var[j+1] = temp;
}
}
}
for(int j=0; j<var.length; j++)
System.out.print(var[j] + " ");
System.out.println("");
}
[/sourcecode]
The above method  sorts an array of integers according to the bubble sort algorithm. The task is to build a generic version of this method that accepts multivariable list arguments, i.e., one can call the method as in the following statements:
[sourcecode language="java" gutter="false" toolbar="false"]
sort(30, 5, 3, 9, 10, 2, 25);
sort("aku", "sedang", "ujian", "programming");
[/sourcecode]
Furthermore, the task requires that the final loop which prints out the contents of the array in the sorted order must be implemented using an enhanced for-loop form (making use the fact that array is an Iterable object).
The solution for this problem is given below, together with the original sort method.
[sourcecode language="java" gutter="false" toolbar="false"]
/**
* This class is a solution for problems A2 and C2 in DPBO programming exam.
* Both problems are actually the same problem. A "sort" method is given, and
* the task is to provide a generic version of the method that accepts multi-variable list
* arguments. In addition, parts of code that prints out the sorted data must be changed
* using enhanced-for-loop form, making use the fact that the data array is iterable.
*/
public class BubbleSort {
// Write your generic sort method here.
// You are not allowed to change the order of the program statements.
// Make as little change as possible in making the generic method.
// The original "sort" method is given below as "public static void sort(int[])"
/**
* The new "sort" method: generic, accepts multi-variable list arguments and
* uses enhanced for-loop to display the sorted data.
*
* Notice the use of type parameter which must be a Comparable object because otherwise,
* it cannot be compared to each other in doing sorting (the sort method is actually
* an implementation of bubble-sort algorithm which is indeed a comparison-based sorting
* algorithm). The multi-variable list argument is adjusted accordingly to accommodate
* generics.
*
* @param <T> the generic type of input data.
* @param vars the input data to be sorted.
*/
public static > void sort(T ... vars) {
// This variable is used in swapping array elements.
// Originally, it's an int type and here is adjusted accordingly
// to allow for generic type of input data.
T temp;
// The main nested loop is not changed.
for (int i=vars.length-1; i > 1; i--) {
for (int j=0; j < i; j++) {
// The if-block below compares two adjacent elements of input data.
// The generic version uses compareTo method from java.lang.Comparable.
if (vars[j].compareTo(vars[j+1]) > 0)  {
temp = vars[j];
vars[j] = vars[j+1];
vars[j+1] = temp;
}
}
}
// enhanced for-loop to display the sorted data
for (T v : vars)
System.out.print(v + " ");
System.out.println();
}
/**
* The original "sort" method given by the problem which accepts an array of integers
* and sort its contents accordingly.
*
* @param var array of integers as input data
*/
public static void sort(int var[]) {
int temp;
for (int i=var.length-1; i>1; i--) {
for (int j=0; j < i; j++) {
if (var[j] > var[j+1])  {
temp = var[j];
var[j] = var[j+1];
var[j+1] = temp;
}
}
}
// display sorted data
// change the for-loop to an enhanced for-loop form
for(int j=0; j<var.length; j++)
System.out.print(var[j] + " ");
System.out.println("");
}
public static void main( String[] args ) {
// The original main method contains the two lines below
// int data[] = {30, 4, 7, 5};
// sort(data);
sort(30, 5, 3, 9, 10, 2, 25);
sort("aku", "sedang", "ujian", "programming");
}
}
[/sourcecode]

No comments:

Post a Comment