Created
January 3, 2016 16:31
-
-
Save pedrofurtado/3919eaf64f8fcb3e3644 to your computer and use it in GitHub Desktop.
Implementation of BubbleSort in Java.
This file contains 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
/** | |
* @file | |
* Bubble sort class. | |
*/ | |
public class BubbleSort { | |
/** | |
* Printing vector method. | |
* | |
* Prints on screen the elements of the vector, from first to last element. | |
* | |
* @param int[] a | |
* Vector of integers. | |
* @return void | |
*/ | |
public static void print_vector(int[] a) { | |
if (a == null) { | |
System.out.println("Null vector."); | |
return; | |
} | |
System.out.println(); | |
System.out.print("< "); | |
for (int i = 0; i < a.length; i++) { | |
System.out.print(a[i] + ", "); | |
} | |
System.out.print(" >"); | |
System.out.println(); | |
} | |
/** | |
* Bubble sort. | |
* | |
* Iterative version. | |
* | |
* Sort order: ascending. | |
* | |
* @param int[] a | |
* Vector of integers. | |
* @return int[] | |
* Arranged vector in ascending order. | |
*/ | |
public static int[] bubble_sort_iterative_ascending(int[] a) { | |
if (a == null) return null; | |
for(int i = 0; i < a.length; i++) { | |
boolean swapped = false; | |
for (int j = 1; j < a.length - i; j++) { | |
if (a[j - 1] > a[j]) { | |
int aux = a[j - 1]; | |
a[j - 1] = a[j]; | |
a[j] = aux; | |
swapped = true; | |
} | |
} | |
if (!swapped) break; | |
} | |
return a; | |
} | |
/** | |
* Bubble sort. | |
* | |
* Iterative version. | |
* | |
* Sort order: descending. | |
* | |
* @param int[] a | |
* Vector of integers. | |
* @return int[] | |
* Arranged vector in descending order. | |
*/ | |
public static int[] bubble_sort_iterative_descending(int[] a) { | |
if (a == null) return null; | |
for(int i = 0; i < a.length; i++) { | |
boolean swapped = false; | |
for (int j = 1; j < a.length - i; j++) { | |
if (a[j - 1] < a[j]) { | |
int aux = a[j - 1]; | |
a[j - 1] = a[j]; | |
a[j] = aux; | |
swapped = true; | |
} | |
} | |
if (!swapped) break; | |
} | |
return a; | |
} | |
/** | |
* Bubble sort. | |
* | |
* Recursive version. | |
* | |
* Sort order: ascending. | |
* | |
* @param int[] a | |
* Vector of integers. | |
* @return int[] | |
* Arranged vector in ascending order. | |
*/ | |
public static int[] bubble_sort_recursive_ascending(int[] a, int i, int j, boolean swapped) { | |
if (a == null) return null; | |
if (i >= a.length) { | |
return a; | |
} | |
if (j < a.length - i) { | |
if (a[j - 1] > a[j]) { | |
int aux = a[j - 1]; | |
a[j - 1] = a[j]; | |
a[j] = aux; | |
swapped = true; | |
} | |
return bubble_sort_recursive_ascending(a, i, j + 1, swapped); | |
} | |
if(!swapped) return a; | |
return bubble_sort_recursive_ascending(a, i + 1, 1, false); | |
} | |
/** | |
* Overloading of bubble_sort_recursive_ascending() method. | |
* | |
* Simplifies the call of the method. | |
*/ | |
public static int[] bubble_sort_recursive_ascending(int[] a) { | |
return bubble_sort_recursive_ascending(a, 0, 1, false); | |
} | |
/** | |
* Bubble sort. | |
* | |
* Recursive version. | |
* | |
* Sort order: descending. | |
* | |
* @param int[] a | |
* Vector of integers. | |
* @return int[] | |
* Arranged vector in descending order. | |
*/ | |
public static int[] bubble_sort_recursive_descending(int[] a, int i, int j, boolean swapped) { | |
if (a == null) return null; | |
if (i >= a.length) { | |
return a; | |
} | |
if (j < a.length - i) { | |
if (a[j - 1] < a[j]) { | |
int aux = a[j - 1]; | |
a[j - 1] = a[j]; | |
a[j] = aux; | |
swapped = true; | |
} | |
return bubble_sort_recursive_descending(a, i, j + 1, swapped); | |
} | |
if(!swapped) return a; | |
return bubble_sort_recursive_descending(a, i + 1, 1, false); | |
} | |
/** | |
* Overloading of bubble_sort_recursive_descending() method. | |
* | |
* Simplifies the call of the method. | |
*/ | |
public static int[] bubble_sort_recursive_descending(int[] a) { | |
return bubble_sort_recursive_descending(a, 0, 1, false); | |
} | |
/** | |
* Examples of use. | |
*/ | |
public static void main(String args[]) { | |
int[] a = { 24, 4, 67, 4, 3, 5, 6, 6, 7, 34, 12, 12, 24, 35, 2, 24, 45, 56, 78 }; | |
BubbleSort.bubble_sort_iterative_ascending(a); | |
System.out.println("Initial vector:"); | |
print_vector(a); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment