Skip to content

Instantly share code, notes, and snippets.

@MafaldaLandeiro
Created May 1, 2016 21:06
Show Gist options
  • Select an option

  • Save MafaldaLandeiro/0db1c8d95263b28e0c6fd0c7f26a0f44 to your computer and use it in GitHub Desktop.

Select an option

Save MafaldaLandeiro/0db1c8d95263b28e0c6fd0c7f26a0f44 to your computer and use it in GitHub Desktop.
Bubble sort
package strategyPattern.strategy;
import java.util.List;
/**
* Implementation of bubble sort
*
* http://www.kriblog.com/j2se/util/various-bubble-sort-example-in-java-using-string-array-arraylist-linked-list-recursive.html
*
*/
public class BubbleSort implements SortAlgorithm {
@Override
public void sort(List<Integer> unsorted) {
for (int i = unsorted.size() - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
if (unsorted.get(j) > unsorted.get(j + 1)) {
int temp = unsorted.get(j);
unsorted.set(j, unsorted.get(j + 1));
unsorted.set(j + 1, temp);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment