Question
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
}
} 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
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.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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started