Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Why is my second array not sorted? ackage arraylistex2; import java.util.ArrayList; /** * * @author HP */ public class ArrayListEx2 { public static void SelectionSort(ArrayList

Why is my second array not sorted?

ackage arraylistex2;

import java.util.ArrayList;

/** * * @author HP */ public class ArrayListEx2 {

public static void SelectionSort(ArrayList list) { for (int i = 0; i < list.size(); i++) { int max = 0; for (int j = 1; j < list.size() - i; j++) { if (list.get(max).compareTo(list.get(j)) < 0) { max = i;

}

} String temp = list.get(max); list.set(max, list.get(list.size() - (i + 1))); list.set(list.size() - (i + 1), temp);

}

}

public static void bubbleSort(ArrayList list) {

for (int i = 1; i < list.size(); i++) { for (int j = 0; j < list.size() - i; j++) { if (list.get(j).compareTo(list.get(j + 1)) > 0) { String temp = list.get(j); list.set((j), list.get(j + 1)); list.set(j + 1, temp);

}

}

} }

/** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here ArrayList arr = new ArrayList(); ArrayList arr2 = new ArrayList<>();

arr.add("d"); arr.add("f"); arr.add("a"); arr.add("e"); arr.add("b"); arr.add("g"); arr.add("c");

arr2.addAll(arr); System.out.println("Array 1");

System.out.println("Before");

for (int i = 0; i < arr.size(); i++) { System.out.print(arr.get(i) + " ");

} System.out.println(); bubbleSort(arr);

System.out.println("After");

for (int i = 0; i < arr.size(); i++) { System.out.print(arr.get(i) + " ");

} System.out.println(); System.out.println("Array 2"); System.out.println("Before"); for (int i = 0; i < arr2.size(); i++) { System.out.print(arr2.get(i)+" "); } System.out.println(); SelectionSort(arr2); System.out.println("After"); for (int i = 0; i < arr2.size(); i++) { System.out.print(arr2.get(i)+" "); }

}

}

output

Array 1 Before d f a e b g c After a b c d e f g Array 2 Before d f a e b g c After c f a e b g d BUILD SUCCESSFUL (total time: 0 seconds)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

1844804526, 978-1844804528

More Books

Students also viewed these Databases questions

Question

2. What are the potential strengths of group discussion?

Answered: 1 week ago

Question

What is the purpose of the Capability Maturity Model ( CMM ) ?

Answered: 1 week ago

Question

=+employee to take on the international assignment?

Answered: 1 week ago

Question

=+differences in home- and host-country costs of living?

Answered: 1 week ago