Develop a NetBeans application that illustrates the applicability of one sorting algorithm on a collection of 10 items. The algorithm should sort the items in descending order. The algorithm is Bubble Sort algorithm. The collection of 10 items is defined and populated in the application code. You can decide the type and values of items stored in the collection. The algorithm must be implemented as part of a class called MyArrayList that extends the ArrayList class functionality with your sorting algorithm. [20 marks]
时间: 2024-02-13 21:06:57 浏览: 104
Here is an implementation of the requested NetBeans application:
```java
import java.util.ArrayList;
public class MyArrayList<T extends Comparable<T>> extends ArrayList<T> {
public void bubbleSortDescending() {
int n = this.size();
boolean swapped;
do {
swapped = false;
for (int i = 1; i < n; i++) {
if (this.get(i - 1).compareTo(this.get(i)) < 0) {
T temp = this.get(i - 1);
this.set(i - 1, this.get(i));
this.set(i, temp);
swapped = true;
}
}
n--;
} while (swapped);
}
public static void main(String[] args) {
MyArrayList<Integer> list = new MyArrayList<Integer>();
list.add(7);
list.add(2);
list.add(9);
list.add(4);
list.add(5);
list.add(1);
list.add(6);
list.add(8);
list.add(3);
list.add(10);
System.out.println("Original list: " + list);
list.bubbleSortDescending();
System.out.println("Sorted list: " + list);
}
}
```
In this implementation, we define a class called `MyArrayList` that extends the `ArrayList` class and adds our bubble sort algorithm to it. The `bubbleSortDescending` method sorts the list in descending order using the bubble sort algorithm. The `main` method creates a list of 10 integers and then calls the `bubbleSortDescending` method to sort the list in descending order. Finally, it prints out the original and sorted lists for verification.
Note that we use the `Comparable` interface to ensure that the items in the list can be compared with each other. This allows us to use the `compareTo` method to determine the order of the items in the list.
阅读全文