Created
January 3, 2016 16:32
-
-
Save pedrofurtado/f9589fa2e9348ecdf272 to your computer and use it in GitHub Desktop.
Implementation of Insertion Sort 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 | |
* Insertion sort class. | |
*/ | |
public class InsertionSort { | |
/** | |
* 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(); | |
} | |
/** | |
* Insertion sort. | |
* | |
* Iterative version. | |
* | |
* Sort order: ascending. | |
* | |
* @param int[] a | |
* Vector of integers. | |
* @return int[] | |
* Arranged vector in ascending order. | |
*/ | |
public static int[] insertion_sort_iterative_ascending(int[] a) { | |
if (a == null) return null; | |
for (int i = 1; i < a.length; i++) { | |
int pivot = a[i]; | |
int j = i; | |
while ((j > 0) && (a[j - 1] > pivot)) { | |
a[j] = a[j - 1]; | |
j = j - 1; | |
} | |
a[j] = pivot; | |
} | |
return a; | |
} | |
/** | |
* Insertion sort. | |
* | |
* Iterative version. | |
* | |
* Sort order: descending. | |
* | |
* @param int[] a | |
* Vector of integers. | |
* @return int[] | |
* Arranged vector in descending order. | |
*/ | |
public static int[] insertion_sort_iterative_descending(int[] a) { | |
if (a == null) return null; | |
for (int i = 1; i < a.length; i++) { | |
int pivot = a[i]; | |
int j = i; | |
while ((j > 0) && (a[j - 1] < pivot)) { | |
a[j] = a[j - 1]; | |
j = j - 1; | |
} | |
a[j] = pivot; | |
} | |
return a; | |
} | |
/** | |
* Swapper method. | |
* | |
* Auxiliar method used in insertion_sort_recursive_ascending() to swap | |
* all vector elements bigger than a determined pivot. | |
* | |
* @param int[] a | |
* Vector of integers. | |
* @param int j | |
* Initial index to start the swapping between elements. | |
* @param int pivot | |
* Number used to be compared in the swap. | |
* @return | |
* Index of vector that contains the element that is less or equal than pivot. | |
*/ | |
public static int swapper(int[] a, int j, int pivot) { | |
if ((j <= 0) || (a[j - 1] <= pivot)) { | |
return j; | |
} | |
a[j] = a[j - 1]; | |
return swapper(a, j - 1, pivot); | |
} | |
/** | |
* Swapper 2 method. | |
* | |
* Auxiliar method used in insertion_sort_recursive_descending() to swap | |
* all vector elements less than a determined pivot. | |
* | |
* @param int[] a | |
* Vector of integers. | |
* @param int j | |
* Initial index to start the swapping between elements. | |
* @param int pivot | |
* Number used to be compared in the swap. | |
* @return | |
* Index of vector that contains the element that is bigger or equal than pivot. | |
*/ | |
public static int swapper2(int[] a, int j, int pivot) { | |
if ((j <= 0) || (a[j - 1] >= pivot)) { | |
return j; | |
} | |
a[j] = a[j - 1]; | |
return swapper2(a, j - 1, pivot); | |
} | |
/** | |
* Insertion sort. | |
* | |
* Recursive version. | |
* | |
* Sort order: ascending. | |
* | |
* @param int[] a | |
* Vector of integers. | |
* @return int[] | |
* Arranged vector in ascending order. | |
*/ | |
public static int[] insertion_sort_recursive_ascending(int[] a, int i, int j, int pivot) { | |
if (a == null) return null; | |
if (i >= a.length) { | |
return a; | |
} | |
pivot = a[i]; | |
j = swapper(a, j, pivot); | |
a[j] = pivot; | |
return insertion_sort_recursive_ascending(a, i + 1, i + 1, pivot); | |
} | |
/** | |
* Overloading of insertion_sort_recursive_ascending() method. | |
* | |
* Simplifies the call of the method. | |
*/ | |
public static int[] insertion_sort_recursive_ascending(int[] a) { | |
return insertion_sort_recursive_ascending(a, 1, 1, 0); | |
} | |
/** | |
* Insertion sort. | |
* | |
* Recursive version. | |
* | |
* Sort order: descending. | |
* | |
* @param int[] a | |
* Vector of integers. | |
* @return int[] | |
* Arranged vector in descending order. | |
*/ | |
public static int[] insertion_sort_recursive_descending(int[] a, int i, int j, int pivot) { | |
if (a == null) return null; | |
if (i >= a.length) { | |
return a; | |
} | |
pivot = a[i]; | |
j = swapper2(a, j, pivot); | |
a[j] = pivot; | |
return insertion_sort_recursive_descending(a, i + 1, i + 1, pivot); | |
} | |
/** | |
* Overloading of insertion_sort_recursive_descending() method. | |
* | |
* Simplifies the call of the method. | |
*/ | |
public static int[] insertion_sort_recursive_descending(int[] a) { | |
return insertion_sort_recursive_descending(a, 1, 1, 0); | |
} | |
/** | |
* Examples of use. | |
*/ | |
public static void main(String args[]) { | |
int[] a = { 12, 24, 4, 67, 4, 3, 5, 6, 6, 7, 34, 35, 2, 45, 12, 56, 78 }; | |
insertion_sort_recursive_descending(a); | |
print_vector(a); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment