can anyone help me? thanks! Insertion Set (sorting by insertion) public static v
ID: 3618609 • Letter: C
Question
can anyone help me? thanks!Insertion Set (sorting by insertion)
public static void insertionSort(int list[])
{
for(int p= 1; p < list.length; p++)
{
int temp = list[p];
int j = p;
for ( j>0; && temp < list[j-1]; j = j-1)
list[j] = list[j-1];
list[j] = temp;
}
}
excerise #1
modify the given insertionSort() to put an array of records inalphabetical order by last name.
record list [] = new record [ number of records]; where
class record
{
int number;
String first, last;
publci record(int number, String first, String last)
{
this.number = number;
this.first = first;
this.last = last;
}
}
}
Exercise #2
Test the selectionSort() method for n = 8 random integers andrecord the number of comparisons. Repeat the test for n = 16, n =32, n= 64, n= 128, n = 256, n =512, and n= 1024.
ps: Please add the public class, public static void main, and mainas well, not jsut method . thanks!
Explanation / Answer
class record { int number; String first, last;
public record(int number, String first, String last) {
this.number = number; this.first = first; this.last = last; } }
class TestInsertion {
public static void insertionSort(record list[])
{
for(int p= 1; p < list.length; p++)
{
record temp = list[p];
int j = p;
for (;j>0 && temp.last.compareTo(list[j-1].last)< 0; j = j-1)
list[j] = list[j-1];
list[j] = temp;
}
}
public static void main(String args[]) { record []s = new record[3];int i; s[0] = new record(1,"first1","last1"); s[1] = new record(1,"first3","last3"); s[2] = new record(1,"first2","last2"); insertionSort(s); for(i=0;i<s.length;i++) System.out.println(s[i].first + " " +s[i].last); } } class record { int number; String first, last;
public record(int number, String first, String last) {
this.number = number; this.first = first; this.last = last; } }
class TestInsertion {
public static void insertionSort(record list[])
{
for(int p= 1; p < list.length; p++)
{
record temp = list[p];
int j = p;
for (;j>0 && temp.last.compareTo(list[j-1].last)< 0; j = j-1)
list[j] = list[j-1];
list[j] = temp;
}
}
public static void main(String args[]) { record []s = new record[3];int i; s[0] = new record(1,"first1","last1"); s[1] = new record(1,"first3","last3"); s[2] = new record(1,"first2","last2"); insertionSort(s); for(i=0;i<s.length;i++) System.out.println(s[i].first + " " +s[i].last); } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.