I have a project for school but I need some hints.
I have an ArrayList that looks like such:
[0] Document 1
[1] Document 1
[2] Document 2
[3] Document 3
[4] Document 3
[5] Document 3
[6] Document 4
I need to use Bubblesort to get a distinct list that looks like this (sorted by the number of occurences each document had in the original list):
[0] Document 3
[1] Document 1
[2] Document 2
[3] Document 4
I'm free to create a new ArrayList or use one external for-loop for finishing it up - but it needs to be sorted by Bubblesort.
I've created other implementations that create a distinct lists based on certain properties of each document - but now when faced with the number of times each document occcurs in the list I'm at a loss to a clean solution.
Edit: Here's the implementation I used for the other parts of the assignment ("attribute" is similar to "document" in my question above)
int r = attributes.size() - 1;
boolean swapped = true;
while(swapped && r >= 0) {
swapped = false;
for(int i = 0; i < r; i++) {
Attributes current = attributes.get(i);
Attributes next = attributes.get(i + 1);
if(current.occurrence > next.occurrence) {
swapped = true;
attributes.set(i, next);
attributes.set(i + 1, current);
}
}
r--;
}
To give you a boilerplate example, you can use something like this.
List<Integer> arrList = Arrays.asList( 5, 0, 0, 2 );
int freq = Collections.frequency(arrList, "0);
System.out.println("Frequency of '0' is: "+freq);
}