Created
May 1, 2016 21:06
-
-
Save MafaldaLandeiro/0db1c8d95263b28e0c6fd0c7f26a0f44 to your computer and use it in GitHub Desktop.
Bubble sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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