1. Using Eclipse to develop the following Java program that implement bubble-sort and selection-sort sorting algorithms.
The following are skeletons of the bubble-sort and selection-sort algorithms:
Bubble sort:
for (int i = 0; i < array.length; i++)
for (int j = 0; j < array.length-1; j++)
if (array[j] >= array[j+1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
Selection sort:
for (int i = 0; i < array.length-1; i++) {
int maxIndex = 0;
int j;
for (j = 1; j < array.length-i; j++)
if (array[j] > array[maxIndex]) maxIndex = j;
int temp = array[maxIndex];
array[maxIndex] = array[j-1];
array[j-1] = temp;
}
A sorting algorithm is stable if two data items or objects with equal key values, according to which sorting is performed, do not change their precedence order in the resulting sorted array. The above two sorting algorithms may not be stable sorting algorithms. Your task in this lab assignment is to use debugging techniques to detect unstableness of these two algorithms and fix the problem to make the algorithms become stable.
The following are you programming task and debugging task.
a)
In Eclipse, create a new Java project with name cs2043lab1.
b)
Write the following class
es and interface
s.
·
Define a class called “StudentRecord” as follows:
class StudentRecord {
private String name, int grade;
public StudentRecord(String name, int grade) {this.name = name; this.grade = grade;}
public int getKey() {return grade;}
}
· Define an interface called “Sorter” as follows:
interface Sorter {public void sort(StudentRecord[] students);}
· Define a class called “BubbleSorter” that implement sorter interface as follows:
class BubbleSorter implements Sorter{public void sort(StudentRecord[] students){/*implement bubble sort algorithm*/}}
· Define a class called “SelectSorter” that implement sorter interface as follows:
class SelectSorter implements Sorter{public void sort(StudentRecord[] students){/*implement selection sort algorithm*/}}
· Define the main class called “Lab1App” with main method. The main method asks the user to input data to create multiple StudentRecord objects into an array, and also asks the user to select a sorting algorithm. It then creates a Sorter object according to the user’s choice, and asks the Sorter object to sort the array.
c)
Test the program one or more times using bubble sort. Make sure to create at least 2 or more student records which have same grade value. Using breakpoint debugging technique to detect the unstableness of the algorithm. Make sure to take one or more screen shot when you see unstable sorting of the array on debugging window for writing your lab report.
d)
Test the program one or more times using selection sort. Make sure to create at least 2 or more student records which have same grade. Using step-by-step debugging technique to detect the unstableness of the algorithm. Make sure to take one or more screen shot when you see unstable sorting of the array on debugging window for writing your lab report.
e)
Fix the unstableness of the both algorithms and test again.