冒泡排序
package org.gallant.algorithm;
import static org.gallant.algorithm.SortUtil.println;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class BubbleSort {
public static void bubbleSort(int[] data) {
int size = data.length;
while (size > 1) {
for (int i = 0; i < size - 1; i++) {
int left = data[i];
int right = data[i + 1];
if (left > right) {
data[i] = right;
data[i + 1] = left;
}
}
size--;
}
}
public static void main(String[] args) {
int[] array = SortUtil.randomArray(9);
println(array);
bubbleSort(array);
println(array);
}
}
快速排序
package org.gallant.algorithm;
import static org.gallant.algorithm.SortUtil.println;
public class QuickSort {
public static void quickSort(int[] data, int start, int end) {
int pivotIndex = partitioning(data, start, end);
if (pivotIndex > -1) {
quickSort(data, start, pivotIndex);
quickSort(data, pivotIndex, end);
}
}
public static int partitioning(int[] data, int start, int end) {
int pivot = data[start];
int i = end, j = start + 1;
while (i >= j) {
int right = data[i];
int left = data[j];
boolean rightLesserPivot = right < pivot;
boolean leftGreaterPivot = left >= pivot;
if (!rightLesserPivot) {
i--;
} else {
if (!leftGreaterPivot) {
j++;
}
}
if (leftGreaterPivot && rightLesserPivot) {
swap(data, left, j, right, i);
i--;
j++;
}
if (i == j) {
boolean isLesserPivot = data[i] < pivot;
if (isLesserPivot) {
swap(data, pivot, start, data[i], i);
}
println(data);
return i;
}
}
return -1;
}
public static void swap(int[] data, int leftValue, int leftIndex, int rigthValue, int rightIndex){
data[rightIndex] = leftValue;
data[leftIndex] = rigthValue;
}
public static void main(String[] args) {
int[] array = SortUtil.randomArray(15);
println(array);
quickSort(array, 0, array.length - 1);
println(array);
}
}
计数排序
package org.gallant.algorithm;
import static org.gallant.algorithm.SortUtil.print;
import static org.gallant.algorithm.SortUtil.println;
public class CountingSort {
public static void countingSort(int[] data) {
int max = Integer.MIN_VALUE,min = Integer.MAX_VALUE;
for (int i = 0; i < data.length; i++) {
int currentValue = data[i];
if (currentValue > max) {
max = currentValue;
}
if (currentValue < min) {
min = currentValue;
}
}
println("max:"+max+",min:"+min);
int[] count = new int[max - min + 1];
for (int i = 0; i < data.length; i++) {
int currentValue = data[i];
print(currentValue+",");
count[currentValue - min] += 1;
}
println("");
println(count);
int currentDataIndex = 0;
for (int i = 0; i < count.length; i++) {
int cnt = count[i];
if (cnt > 0) {
for (int j = 0; j < cnt; j++) {
data[currentDataIndex] = min + i;
currentDataIndex++;
}
}
}
}
public static void main(String[] args) {
int[] array = SortUtil.randomArray(11);
println(array);
countingSort(array);
println(array);
}
}
桶排序
package org.gallant.algorithm;
import static org.gallant.algorithm.SortUtil.println;
import java.util.Arrays;
import java.util.Objects;
public class BucketSort {
public static void bucketSort(int[] array) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int currentValue : array) {
int len = getIntegerLength(currentValue);
if (len > max) {
max = len;
}
if (len < min) {
min = len;
}
}
println("max:"+max+",min:"+min);
Integer[][] count = new Integer[max - min + 1][array.length];
for (int i = 0; i < array.length; i++) {
int currentValue = array[i];
Integer[] bucket = count[getIntegerLength(currentValue) - min];
for (int j = 0; j < bucket.length; j++) {
if (bucket[j] == null) {
bucket[j] = currentValue;
break;
}
}
}
println(count);
int currentDataIndex = 0;
for (int i = 0; i < count.length; i++) {
Integer[] bucketObj = count[i];
if (bucketObj.length > 0) {
int[] bucket = Arrays.stream(bucketObj).filter(Objects::nonNull).mapToInt(Integer::intValue).toArray();
Sorts.insertSort(bucket);
for (int j = 0; j < bucket.length; j++) {
array[currentDataIndex] = bucket[j];
currentDataIndex++;
}
}
}
}
public static int getIntegerLength(int value) {
return Integer.toString(value).length();
}
public static void main(String[] args) {
int[] array = SortUtil.randomArray2(10);
println(array);
bucketSort(array);
println(array);
}
}
基数排序
package org.gallant.algorithm;
import static org.gallant.algorithm.SortUtil.println;
import java.util.Arrays;
import java.util.Objects;
public class RadixSort {
public static void radixSort(int[] array) {
int maxLen = 0;
for (int i = 0; i < array.length; i++) {
int currentValue = array[i];
int len = Integer.toString(currentValue).length();
if (len > maxLen) {
maxLen = len;
}
}
for (int i = 0; i < maxLen; i++) {
Integer[][] buckets = new Integer[10][array.length];
double divisor = Math.pow(10, i);
for (int j = 0; j < array.length; j++) {
int currentValue = array[j];
int index = (int) ((currentValue / divisor) % 10);
Integer[] bucket = buckets[index];
for (int k = 0; k < bucket.length; k++) {
if (bucket[k] == null) {
bucket[k] = currentValue;
break;
}
}
}
int currentDataIndex = 0;
for (int l = 0; l < buckets.length; l++) {
Integer[] bucketObj = buckets[l];
if (bucketObj.length > 0) {
int[] bucket = Arrays.stream(bucketObj).filter(Objects::nonNull).mapToInt(Integer::intValue).toArray();
Sorts.insertSort(bucket);
for (int m = 0; m < bucket.length; m++) {
array[currentDataIndex] = bucket[m];
currentDataIndex++;
}
}
}
}
}
public static void main(String[] args) {
int[] array = SortUtil.randomArray2(9);
println(array);
radixSort(array);
println(array);
}
}
堆排序
package org.gallant.algorithm;
import static org.gallant.algorithm.SortUtil.println;
public class HeapSort {
public static void heapSort(int[] array) {
int heapSize = array.length;
for (int i = heapSize / 2 - 1; i >= 0; i--) {
heapify(array, heapSize, i);
}
for (int i = array.length - 1; i >= 0 ; i--) {
swap(array, i, 0);
heapify(array, i, 0);
}
}
public static void heapify(int[] array, int heapSize, int largestNodeIndex) {
int largest = largestNodeIndex;
int leftChild = 2 * largestNodeIndex + 1;
int rightChild = 2 * largestNodeIndex + 2;
if (leftChild < heapSize && array[leftChild] > array[largest]) {
largest = leftChild;
}
if (rightChild < heapSize && array[rightChild] > array[largest]) {
largest = rightChild;
}
if (largest != largestNodeIndex) {
swap(array, largest, largestNodeIndex);
heapify(array, heapSize, largest);
}
}
public static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String[] args) {
int[] array = SortUtil.randomArray(9);
println(array);
heapSort(array);
println(array);
}
}