标题1.冒泡排序
import java.util.Arrays;
public class Main {
static int temp;
public static void main(String[] args) {
int[] nums = {12,15,13,46,87,164,454,22,45,65};
Bubbing(nums);
}
public static void Bubbing(int[] nums){
System.out.println("排序前数组序列:");
System.out.println(Arrays.toString(nums));
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length-i-1; j++) {
if(nums[j]>nums[j+1]){
temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
System.out.println("排序后数组序列:");
System.out.println(Arrays.toString(nums));
}
}
2.选择排序
import java.util.Arrays;
public class Main {
static int temp;
public static void main(String[] args) {
int[] nums = {12,15,13,46,87,164,454,22,45,65};
Select(nums, nums.length);
}
public static void Select(int [] nums,int len){
for (int i = 0; i < len-1; i++) {
int index =i;
int j;
for (j = i+1; j < len; j++) {
if(nums[j] < nums[index]){
index = j;
}
}
int temp = nums[index];
nums[index] = nums[i];
nums[i] = temp;
System.out.println(Arrays.toString(nums));
}
}
}
3.插入排序
import java.util.Arrays;
public class InsertSort {
public static void main(String[] args) {
int[] num = {45,12,3,56,78,13,456,123,2};
insertionSort(num);
System.out.println(Arrays.toString(num));
}
public static int[] insertionSort(int []arr) {
int len = arr.length;
int preIndex, current;
for (int i = 1; i < len; i++) {
preIndex = i - 1;
current = arr[i];
while (preIndex >= 0 && arr[preIndex] > current) {
arr[preIndex + 1] = arr[preIndex];
preIndex--;
}
arr[preIndex + 1] = current;
}
return arr;
}
}
[12, 45, 3, 56, 78, 13, 456, 123, 2]
[3, 12, 45, 56, 78, 13, 456, 123, 2]
[3, 12, 45, 56, 78, 13, 456, 123, 2]
[3, 12, 45, 56, 78, 13, 456, 123, 2]
[3, 12, 13, 45, 56, 78, 456, 123, 2]
[3, 12, 13, 45, 56, 78, 456, 123, 2]
[3, 12, 13, 45, 56, 78, 123, 456, 2]
[2, 3, 12, 13, 45, 56, 78, 123, 456]
4.快速排序
public static void main(String[] args) {
int[] arr = new int[]{45,12,3,56,78,13,456,123,2};
quickSort(arr,0,arr.length-1);
printArr(arr);
}
public static void quickSort(int[] arr,int left,int right){
if (left < right){
int mid = get_mid(arr, left, right);
quickSort(arr,left,mid-1);
quickSort(arr,mid+1,right);
}
}
public static int get_mid(int[] arr,int left,int right){
int pivot = arr[left];
while (left < right){
while (arr[right] >= pivot && left < right)right--;
arr[left] = arr[right];
while (arr[left] <= pivot && left < right)left++;
arr[right] = arr[left];
}
arr[left] = pivot;
return left;
}
private static void printArr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
5.希尔排序
public class Test3 {
public static void main(String[] args) {
int[] arr = new int[]{11,2,54,55,122,1,4,54,5,12};
shellSort(arr);
}
private static void shellSort(int[] arr){
for (int gap = arr.length/2; gap > 0; gap /= 2) {
for (int i = gap; i < arr.length; i++) {
int j = i;
int temp = arr[i];
if (arr[j] < arr[j-gap]) {
while (j-gap >= 0 && temp < arr[j-gap]) {
arr[j] = arr[j-gap];
j -= gap;
}
arr[j] = temp;
}
}
System.out.println(Arrays.toString(arr));
}
}
}
6.归并排序
public class Test3 {
public static void main(String[] args) {
int[] array=new int[]{8,3,2,1,7,4,6,5};
printResult(array);
sort(array,0,array.length-1);
printResult(array);
}
private static void sort(int[] array,int i,int j) {
if(i<j)
{
int middle=(i+j)/2;
sort(array,i,middle);
sort(array,middle+1,j);
merge(array,i,middle,j);
}
}
private static void merge(int[] array, int i, int middle, int j) {
int[] temp=new int[array.length];
int m=i;
int n=middle+1;
int k=i;
while(m<=middle&&n<=j)
{
if(array[m]<array[n])
temp[k++]=array[m++];
else
temp[k++]=array[n++];
}
while(m<=middle)
{
temp[k++]=array[m++];
}
while(n<=j)
{
temp[k++]=array[n++];
}
while(i<=j)
{
array[i]=temp[i++];
}
}
private static void printResult(int[] array) {
for(int value:array)
System.out.print(" "+value+" ");
System.out.println();
}
}
7.堆排序
public static void main(String[] args) {
int[] array = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};
heapSort(array);
System.out.println(Arrays.toString(array));
}
public static void heapSort(int[] array) {
if (array == null || array.length <= 1) {
return;
}
int length = array.length;
for (int i = length / 2 - 1; i >= 0; i--) {
adjustHeap(array, i, length);
}
for (int j = length - 1; j > 0; j--) {
swap(array, 0, j);
adjustHeap(array, 0, j);
}
}
private static void adjustHeap(int[] array, int i, int length) {
int temp = array[i];
for (int k = i * 2 + 1; k < length; k = k * 2 + 1) {
if (k + 1 < length && array[k] < array[k + 1]) {
k++;
}
if (array[k] > temp) {
array[i] = array[k];
i = k;
} else {
break;
}
}
array[i] = temp;
}
private static void swap(int[] array, int a, int b) {
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
8.计数排序
public static void countingSort(int[] array) {
if (array == null || array.length <= 1) {
return;
}
int length = array.length;
int max = array[0];
int min = array[0];
for (int i = 0; i < length; i++) {
if (max < array[i]) {
max = array[i];
}
if (min > array[i]) {
min = array[i];
}
}
int offset = max - min + 1;
int[] count = new int[offset + 1];
for (int i = 0; i < length; i++) {
count[array[i] - min + 1]++;
}
for (int i = 0; i < offset; i++) {
count[i + 1] += count[i];
}
int[] aux = new int[length];
for (int i = 0; i < length; i++) {
aux[count[array[i] - min]++] = array[i];
}
for (int i = 0; i < length; i++) {
array[i] = aux[i];
}
}
public static void main(String[] args) {
int[] array = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};
countingSort(array);
System.out.println(Arrays.toString(array));
}
9.桶排序
public static void bucketSort(int[] array) {
if (array == null || array.length <= 1) {
return;
}
int length = array.length;
LinkedList<Integer>[] bucket = (LinkedList<Integer>[]) new LinkedList[length];
int maxValue = Arrays.stream(array).max().getAsInt();
for (int i = 0; i < array.length; i++) {
int index = toBucketIndex(array[i], maxValue, length);
if (bucket[index] == null) {
bucket[index] = new LinkedList<>();
}
bucket[index].add(array[i]);
}
List<Integer> temp = new ArrayList<>();
for (int i = 0; i < length; i++) {
if (bucket[i] != null) {
Collections.sort(bucket[i]);
temp.addAll(bucket[i]);
}
}
for (int i = 0; i < length; i++) {
array[i] = temp.get(i);
}
}
private static int toBucketIndex(int value, int maxValue, int length) {
return (value * length) / (maxValue + 1);
}
public static void main(String[] args) {
int[] array = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};
bucketSort(array);
System.out.println(Arrays.toString(array));
}
10.基数排序
public static void radixSort(int[] array) {
if (array == null || array.length <= 1) {
return;
}
int length = array.length;
int radix = 10;
int[] aux = new int[length];
int[] count = new int[radix + 1];
int x = Arrays.stream(array).map(s -> String.valueOf(s).length()).max().getAsInt();
for (int d = 0; d < x; d++) {
for (int i = 0; i < length; i++) {
count[digitAt(array[i], d) + 1]++;
}
for (int i = 0; i < radix; i++) {
count[i + 1] += count[i];
}
for (int i = 0; i < length; i++) {
aux[count[digitAt(array[i], d)]++] = array[i];
}
for (int i = 0; i < length; i++) {
array[i] = aux[i];
}
for (int i = 0; i < count.length; i++) {
count[i] = 0;
}
}
}
private static int digitAt(int value, int d) {
return (value / (int) Math.pow(10, d)) % 10;
}
public static void main(String[] args) {
int[] array = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};
radixSort(array);
System.out.println(Arrays.toString(array));
}