Extract common sort logic to SortUtils class
This commit is contained in:
parent
036d2f400c
commit
ea4f641ae2
4 changed files with 48 additions and 37 deletions
|
@ -1,5 +1,7 @@
|
|||
package com.wbrawner.algorithms.sorting;
|
||||
|
||||
import static com.wbrawner.algorithms.sorting.SortUtils.swap;
|
||||
|
||||
/**
|
||||
* The Bubble Sort algorithm modifies an array in place, continuously
|
||||
* looping through the array and swapping elements that are unsorted until no
|
||||
|
@ -23,9 +25,7 @@ public class BubbleSort {
|
|||
swapsMade = false;
|
||||
for (int i = 1; i < array.length; i++) {
|
||||
if (array[i] < array[i - 1]) {
|
||||
int currentValue = array[i];
|
||||
array[i] = array[i - 1];
|
||||
array[i - 1] = currentValue;
|
||||
swap(array, i, i - 1);
|
||||
swapsMade = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@ package com.wbrawner.algorithms.sorting;
|
|||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static com.wbrawner.algorithms.sorting.SortUtils.merge;
|
||||
|
||||
/**
|
||||
* The Merge Sort algorithm works by the "divide and conquer" strategy. The
|
||||
* given array of data is recursively split into two smaller arrays that are
|
||||
|
@ -31,35 +33,4 @@ public class MergeSort {
|
|||
sort(second)
|
||||
);
|
||||
}
|
||||
|
||||
private static int[] merge(int[] first, int[] second) {
|
||||
// Create a new array that can hold both of the given arrays
|
||||
int[] sorted = new int[first.length + second.length];
|
||||
// Three values are used here:
|
||||
// i - keeps track of the sorted array's index
|
||||
// j - keeps track of the first array's index
|
||||
// k - keeps track of the second array's index
|
||||
int i, j = 0, k = 0;
|
||||
for (i = 0; i < sorted.length; i++) {
|
||||
// If we still have values from the first array...
|
||||
if (j < first.length
|
||||
// ... and we either don't have values from the second
|
||||
// array or the value at the current index of the first
|
||||
// array is less than the value at the current index of
|
||||
// the second array...
|
||||
&& (k >= second.length || first[j] < second[k])
|
||||
) {
|
||||
// ... then we append the value at the current index of the
|
||||
// first array to the sorted array.
|
||||
sorted[i] = first[j];
|
||||
j++;
|
||||
} else if (k < second.length) {
|
||||
// In any other case, we should add the value at the current
|
||||
// index of the second array
|
||||
sorted[i] = second[k];
|
||||
k++;
|
||||
}
|
||||
}
|
||||
return sorted;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.wbrawner.algorithms.sorting;
|
||||
|
||||
import static com.wbrawner.algorithms.sorting.SortUtils.swap;
|
||||
|
||||
/**
|
||||
* The Selection Sort algorithm modifies an array in place, finding the
|
||||
* minimum value that hasn't been sorted, and swapping it with the first
|
||||
|
@ -25,9 +27,7 @@ public class SelectionSort {
|
|||
minPos = j;
|
||||
}
|
||||
}
|
||||
int min = array[minPos];
|
||||
array[minPos] = array[i];
|
||||
array[i] = min;
|
||||
swap(array, minPos, i);
|
||||
}
|
||||
|
||||
return array;
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package com.wbrawner.algorithms.sorting;
|
||||
|
||||
public class SortUtils {
|
||||
public static int[] merge(int[] first, int[] second) {
|
||||
// Create a new array that can hold both of the given arrays
|
||||
int[] sorted = new int[first.length + second.length];
|
||||
// Three values are used here:
|
||||
// i - keeps track of the sorted array's index
|
||||
// j - keeps track of the first array's index
|
||||
// k - keeps track of the second array's index
|
||||
int i, j = 0, k = 0;
|
||||
for (i = 0; i < sorted.length; i++) {
|
||||
// If we still have values from the first array...
|
||||
if (j < first.length
|
||||
// ... and we either don't have values from the second
|
||||
// array or the value at the current index of the first
|
||||
// array is less than the value at the current index of
|
||||
// the second array...
|
||||
&& (k >= second.length || first[j] < second[k])
|
||||
) {
|
||||
// ... then we append the value at the current index of the
|
||||
// first array to the sorted array.
|
||||
sorted[i] = first[j];
|
||||
j++;
|
||||
} else if (k < second.length) {
|
||||
// In any other case, we should add the value at the current
|
||||
// index of the second array
|
||||
sorted[i] = second[k];
|
||||
k++;
|
||||
}
|
||||
}
|
||||
return sorted;
|
||||
}
|
||||
|
||||
public static void swap(int[] array, int firstPos, int secondPos) {
|
||||
int firstValue = array[firstPos];
|
||||
array[firstPos] = array[secondPos];
|
||||
array[secondPos] = firstValue;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue