- Write a insertion or selection sort program that sorts an ArrayList in decreasing order so that the largest country is at the beginning of the array (Create your own Country class with size). Use a Comparator
- Test if two arraylists contain the same elements in reverse order
- Remove every other element from an arraylist
- Bonus Hack
public void drawLine(int stars) {
for (int i = 1; i <= stars; i++) {
System.out.print("*");
}
if(stars>0){
System.out.println();
drawLine(stars - 1);
}
}
drawLine(10);
public class Country implements Comparable<Country> {
private int size;
private int population;
public Country(int size, int population) {
this.size = size;
this.population = population;
}
public int getSize() {
return size;
}
public int getPopulation() {
return population;
}
@Override
public int compareTo(Country o) {
return this.size - o.size;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Country) {
Country other = (Country) obj;
return this.size == other.size && this.population == other.population;
}
return false;
}
@Override
public String toString() {
return "Country{" +
"size=" + size +
", population=" + population +
'}';
}
}
public class Country {
int size;
String countryName;
public Country(String countryName, int size){
this.size = size;
this.countryName = countryName;
}
public int getSize(){
return this.size;
}
// compare country population sizes
public int compareCountry(Country country) {
if (this.getSize() < country.getSize())
{
return 0;
}
else if (this.getSize() > country.getSize()){
return 1;
}
else{
return -1;
}
}
public String toString() {
String string = "country: " + this.countryName + " Size: " + this.size;
return string;
}
}
public class SelectionSort {
public static void sort(Country[] arr) {
for (int i = 0; i < arr.length-1; i++) {
int min_idx = i;
for (int j = i+1; j < arr.length; j++) {
// use compare country, only set min_idx if -1 (smaller pop)
if (arr[j].compareCountry(arr[min_idx]) == 1)
min_idx = j;
}
// assign temp country to swap
Country temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
public static void main(String[] args) {
Country[] arr = {new Country("Argentina", 2780400 ), new Country("India", 3287260), new Country("Australia", 7741220 ), new Country("Brazil", 8515770 ), new Country("US", 9525067)};
SelectionSort.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
SelectionSort.main(null);
public static void main(String []args){
ArrayList<String> list1=new ArrayList<String>();
list1.add("Ronaldo");//Adding object in arraylist
list1.add("Messi");
list1.add("Neymar");
System.out.println(list1);
ArrayList<String> list2=new ArrayList<String>();
list2.add("Kian");
list2.add("Brian");
list2.add("Arman");
System.out.println(list2);
if(list1.equals(list2)) {
System.out.println("is equal");
}
else{
System.out.println("not equal");
}
}
main(null);
public static void main(String []args){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
ArrayList<Integer> remove = new ArrayList<Integer>();
int count = 2;
for (int i = 0; i < list.size(); i++) {
if (!(i % count == 0)) {
remove.add(list.get(i));
}
}
list.removeAll(remove);
System.out.print(list);
}
main(null);
import java.util.ArrayList;
import java.util.Comparator;
public class Country {
private String name;
private int size;
public Country(String name, int size) {
this.name = name;
this.size = size;
}
public String getName() {
return name;
}
public int getSize() {
return size;
}
public String toString() {
return name + " (" + size + ")";
}
public static void heapSort(ArrayList<Country> countries, Comparator<Country> comparator) {
int n = countries.size();
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--)
heapify(countries, n, i, comparator);
// Extract elements from heap in decreasing order
for (int i = n - 1; i > 0; i--) {
// Move current root to end
Country temp = countries.get(0);
countries.set(0, countries.get(i));
countries.set(i, temp);
// Heapify reduced heap
heapify(countries, i, 0, comparator);
}
}
public static void heapify(ArrayList<Country> countries, int n, int i, Comparator<Country> comparator) {
int largest = i; // Initialize largest as root
int left = 2 * i + 1;
int right = 2 * i + 2;
// If left child is larger than root
if (left < n && comparator.compare(countries.get(left), countries.get(largest)) > 0)
largest = left;
// If right child is larger than largest so far
if (right < n && comparator.compare(countries.get(right), countries.get(largest)) > 0)
largest = right;
// If largest is not root
if (largest != i) {
Country swap = countries.get(i);
countries.set(i, countries.get(largest));
countries.set(largest, swap);
// Recursively heapify the affected sub-tree
heapify(countries, n, largest, comparator);
}
}
public static void main(String[] args) {
ArrayList<Country> countries = new ArrayList<>();
countries.add(new Country("USA", 9833520));
countries.add(new Country("China", 9596961));
countries.add(new Country("India", 3287263));
countries.add(new Country("Brazil", 8515767));
countries.add(new Country("Australia", 7692024));
System.out.println("Before sorting:");
System.out.println(countries);
heapSort(countries, Comparator.comparing(Country::getSize).reversed());
System.out.println("After sorting:");
System.out.println(countries);
}
}