Write a selection sort algorithm so that it sorts an array of Comparable objects
ID: 3702357 • Letter: W
Question
Write a selection sort algorithm so that it sorts an array of Comparable objects in Java. NO PRIMITIVE DATA TYPES, ONLY WRAPPER CLASS.
For example, with selectionsort, you would have the specification shown here:
public static void selectionsort(Comparable[] data, Integer first, Integer n)
//Precondtion: data has at least n non-null
//components starting at data[first], and
//they may all be compared with one another
//using their compareTo method.
//Postcondition: The elements of data
//have been rearranged so that
//data[first] <= data[first+1] <=...<=
//data[first+n-1].
Include a demo to test the above method.
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
SortComparable.java
=================
import java.util.Scanner;
public class SortComparable {
public static void selectionsort(Comparable[] data, Integer first, Integer n)
//Precondtion: data has at least n non-null
//components starting at data[first], and
//they may all be compared with one another
//using their compareTo method.
//Postcondition: The elements of data
//have been rearranged so that
//data[first] <= data[first+1] <=...<=
//data[first+n-1].
{
for(int i = 0; i < n; i++)
{
int minIdx = i;
for(int j = i + 1; j < n; j++)
{
if(data[first+j].compareTo(data[first+minIdx]) < 0)
minIdx = j;
}
if(minIdx != i)
{
Comparable temp = data[first+i];
data[first + i] = data[first + minIdx];
data[first + minIdx] = temp;
}
}
}
public static void print(String message, Student[] s)
{
System.out.println(message);
System.out.printf("%-20s %10s ", "Name", "Marks");
for(int i = 0; i < s.length; i++)
System.out.printf("%-20s %10s ", s[i].getName(), s[i].getMarks());
System.out.println();
}
public static void main(String[] args) {
int n;
Scanner keyboard = new Scanner(System.in);
Student[] s;
System.out.print("How many students? ");
n = keyboard.nextInt();
s = new Student[n];
String name;
int marks;
for(int i = 0; i < n; i++)
{
System.out.println("Student " + (i+1));
System.out.print("Enter name: ");
name = keyboard.next();
System.out.println("Enter marks: ");
marks = keyboard.nextInt();
s[i] = new Student(name, marks);
}
print("Before Sorting", s);
selectionsort(s, 0, n);
print("After Sorting", s);
}
}
Student.java
============
public class Student implements Comparable<Student> {
private String name;
private int marks;
public Student(String name, int marks)
{
this.name = name;
this.marks = marks;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
@Override
public int compareTo(Student o) {
//compare students on their marks
if(marks < o.marks)
return -1;
else if(marks == o.marks)
return 0;
else
return 1;
}
}
output
======
How many students? 5
Student 1
Enter name: Bob
Enter marks:
80
Student 2
Enter name: Alice
Enter marks:
95
Student 3
Enter name: Michael
Enter marks:
76
Student 4
Enter name: Robert
Enter marks:
85
Student 5
Enter name: Jack
Enter marks: 70
Before Sorting
Name Marks
Bob 80
Alice 95
Michael 76
Robert 85
Jack 70
After Sorting
Name Marks
Jack 70
Michael 76
Bob 80
Robert 85
Alice 95
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.