莫得job,刷刷算法~
首先是回顾一下最原始的C来写……改成java吧。。。
冒泡和选择排序感觉没什么必要写
插入排序
//插入排序
class A {
public static void testInsert(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int temp = arr[i];
int j = i;
while (j > 0 && temp < arr[j - 1]) {
arr[j] = arr[j - 1];
j--;
}
if (j != i) {
arr[j] = temp;
}
}
}
}
希尔排序
//希尔排序
class B {
public static void testXier(int[] arr) {
for (int d = arr.length/2; d > 0; d /= 2) {
for (int i = d; i < arr.length; i++) {
for (int j = i - d;j >= 0; j -=d) {
if (arr[j] > arr[j + d]) {
int temp = arr[j];
arr[j] = arr[j + d];
arr[j + d] = temp;
}
}
}
}
}
}
归并排序
//归并排序
class C {
static void testMergeSort(int[] arr, int left, int right) {
if (left < right) {
int middle = (left + right) / 2;
testMergeSort(arr, left, middle);
testMergeSort(arr, middle + 1, right);
merge(arr,left,middle,right);
}
}
static void merge(int[] arr, int left, int middle, int right) {
int[] result = new int[arr.length];
int i = left;
int leftStart = left;
int rightStart = middle + 1;
while (leftStart <= middle && rightStart <= right) {
if (arr[leftStart] <= arr[rightStart]) {
result[i++] = arr[leftStart];
leftStart++;
} else {
result[i++] = arr[rightStart];
rightStart++;
}
}
while (leftStart <= middle) {
result[i++] = arr[leftStart];
leftStart++;
}
while (rightStart <= right) {
result[i++] = arr[rightStart];
rightStart++;
}
//copy
while (left <= right) {
arr[left] = result[left++];
}
}
}
快排
//快排
class D {
public static void testQuickSort (int[] arr, int begin, int end) {
if (begin > end) {
return;
}
int i = begin;
int j = end;
while (i < j) {
while (i < j && arr[j] >= arr[begin]) {
j--;
}
while (i < j && arr[i] <= arr[begin]) {
i++;
}
if (i < j) {
swap(arr,i,j);
}
}
swap(arr, begin, i);
testQuickSort(arr, begin, i - 1);
testQuickSort(arr, i + 1, end);
}
private static void swap(int[] arr,int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
堆排序(没有用java的PriorityQueue,模拟原始大顶堆)
//堆排序
class E {
public static void heapSort(int[] arr) {
if (arr == null || arr.length == 0) {
return;
}
int len = arr.length;
buildMaxHeap(arr, len);
for (int i = len - 1; i >= 0 ; i--) {
swap(arr, 0, i);
len --;
// toHeap(arr, 0, len);
buildMaxHeap(arr,len);
}
}
private static void buildMaxHeap(int[] arr, int len) {
for (int i = len / 2 - 1;i >= 0;i--) {
toHeap(arr, i ,len);
}
}
private static void toHeap(int[] arr, int i, int len) {
int left = 2 * i + 1;
int right = 2 * i + 2;
int maxIndex = i;
if (left < len && arr[left] > arr[maxIndex]) {
maxIndex = left;
}
if (right < len && arr[right] > arr[maxIndex]) {
maxIndex = right;
}
if (maxIndex != i) {
swap(arr, i, maxIndex);
toHeap(arr, maxIndex, len);
}
}
private static void swap(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
计数排序
class F {
public static void countSort(int[] arr) {
if (arr == null || arr.length == 0) {
return;
}
int max = arr[0];
int min = arr[0];
for (int i = 0; i < arr.length; i++) {
if (max < arr[i]) {
max = arr[i];
}
if (min > arr[i]) {
min = arr[i];
}
}
int[] count = new int[max + 1];
for (int i = 0; i < arr.length; i++) {
// 使用加1后的索引,有重复的该位置就自增
count[arr[i] - min]++;
}
// 创建结果数组的起始索引
int index = 0;
// 遍历计数数组,将计数数组的索引填充到结果数组中
for (int i=0; i<count.length; i++) {
while (count[i] > 0) {
// 再将减去的最小值补上
arr[index++] = i+min;
count[i]--;
}
}
}
}
桶排序
class G {
public static void BucketSort(int[] arr, int bucketSize) {
if (arr == null || arr.length == 0) {
return;
}
int minValue = arr[0];
int maxValue = arr[0];
for (int i = 1; i < arr.length; i++) {
if(minValue > arr[i])
minValue = arr[i];
if(maxValue < arr[i])
maxValue = arr[i];
}
int bucketCount = (maxValue - minValue) / bucketSize + 1;
ArrayList<LinkedList<Integer>> buckets = new ArrayList<>(bucketCount);
for (int i = 0; i < bucketCount; i++) {
buckets.add(new LinkedList<>());
}
// 利用映射函数将数据分配到各个桶中
for (int i = 0; i < arr.length; i++) {
int index = (arr[i] - minValue) / bucketSize;
buckets.get(index).add(arr[i]);
}
//对桶内的元素进行排序,我这里采用系统自带的排序工具
for (int i = 0; i < bucketCount; i++) {
Collections.sort(buckets.get(i));
}
int k = 0;
for (int j = 0; j < bucketCount; j++) {
for (Integer t : buckets.get(j)) {
arr[k++] = t;
}
}
}
}
基数排序
class H {
public static void RadixSort(int[] arr) {
if (arr == null || arr.length == 0) {
return;
}
// 获取最大值
int maxValue = arr[0];
for (int value : arr) {
if (maxValue < value) {
maxValue = value;
}
}
// 获取最大值的最高位数
int lenght = 0;
if (maxValue == 0) {
lenght = 1;
}
for (long temp = maxValue; temp != 0; temp /= 10) {
lenght++;
}
// 创建10个桶
ArrayList<LinkedList<Integer>> bucketList = new ArrayList<>(10);
//初始化桶
for (int i = 0; i < 10; i++) {
bucketList.add(new LinkedList<Integer>());
}
// 进行每一趟的排序,从个位数开始排
for (int i = 1; i <= lenght; i++) {
for (int j = 0; j < arr.length; j++) {
// 获取每个数最后第 i 位是数组
int index = (arr[j] / (int)Math.pow(10,i-1)) % 10;
//放进对应的桶里
bucketList.get(index).add(arr[j]);
}
//合并放回原数组
int k = 0;
for (int j = 0; j < 10; j++) {
for (Integer t : bucketList.get(j)) {
arr[k++] = t;
}
//取出来合并了之后把桶清光数据
bucketList.get(j).clear();
}
}
}
}