文章目录
- 一、优化后的冒泡排序
- 二、选择排序
- 三、插入排序
- 四、希尔排序
- 五、快速排序
- 六、随机化快速排序
- 七、归并排序
- 八、可处理负数的基数排序
一、优化后的冒泡排序
package com.yzh.sort;
@SuppressWarnings({"all"})
public class BubbleSort {
public static void BubbleSort(int[]a){
for (int i = 0; i <a.length-1 ; i++) {
boolean flag=true;
for (int j = 0; j <a.length-i-1 ; j++) {
if(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=false;
}
}
if(flag) break;
}
}
}
二、选择排序
package com.yzh.sort;
@SuppressWarnings({"all"})
public class SelectSort {
public static void SelectSort(int[]a) {
for (int i = 0; i < a.length - 1; i++) {
int index = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[index]) {
index = j;
}
}
int temp = a[index];
a[index] = a[i];
a[i] = temp;
}
}
}
三、插入排序
package com.yzh.sort;
@SuppressWarnings({"all"})
public class InsertSort {
public static void InsertSort(int[]a){
for (int i = 0; i < a.length; i++) {
int insertValue=a[i];
int insertIndex=i-1;
while (insertIndex>=0 && insertValue <a[insertIndex]) {
a[insertIndex+1]=a[insertIndex];
insertIndex--;
}
a[insertIndex+1]=insertValue;
}
}
}
四、希尔排序
package com.yzh.sort;
@SuppressWarnings({"all"})
public class ShellSort {
public static void ShellSort(int[] a){
for (int gap=a.length / 2; gap > 0; gap = gap / 2) {
for (int i = gap; i < a.length; i++) {
for (int j = i - gap; j>=0; j=j-gap) {
if (a[j]>a[j+gap]) {
int temp=a[j];
a[j]=a[j+gap];
a[j+gap]=temp;
}
}
}
}
}
}
五、快速排序
package com.yzh.sort;
@SuppressWarnings({"all"})
public class QuickSort {
public static void QuickSort(int[] arr,int low,int high){
int i,j,temp,t;
if(low>=high){
return;
}
i=low;
j=high;
temp = arr[low];
while (i<j) {
while (temp<=arr[j]&&i<j) {
j--;
}
while (temp>=arr[i]&&i<j) {
i++;
}
if (i<j) {
t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
arr[low] = arr[i];
arr[i] = temp;
QuickSort(arr, low, j-1);
QuickSort(arr, j+1, high);
}
}
六、随机化快速排序
package com.yzh.sort;
import java.util.Random;
import java.util.Scanner;
@SuppressWarnings({"all"})
public class RandQuickSort {
public static void randsort(int[] arr, int left, int right) {
if(left>=right)
return;
Random random = new Random();
int randIndex = random.nextInt(right-left)+left;
int temp = arr[randIndex];
arr[randIndex] = arr[right];
arr[right] = temp;
int i = left-1;
for(int j = left;j<=right;j++) {
if(arr[j]<arr[right]) {
i++;
int temp1 = arr[i];
arr[i] = arr[j];
arr[j] = temp1;
}
}
int temp2 = arr[i+1];
arr[i+1] = arr[right];
arr[right] = temp2;
randsort(arr,left,i);
randsort(arr,i+2,right);
}
}
七、归并排序
package com.yzh.sort;
@SuppressWarnings({"all"})
public class MergeSort {
private static void mergesort(int[] a, int left, int right, int[] temp) {
if (left<right) {
int mid=((right-left)>>1)+left;
mergesort(a, left, mid, temp);
mergesort(a, mid+1, right, temp);
merge(a,left,right,mid,temp);
}
}
private static void merge(int[] a, int left, int right, int mid, int[] temp) {
int i=left;
int j=mid+1;
int t=0;
while (i<=mid && j<=right) {
if (a[i]<=a[j]) {
temp[t++]=a[i++];
}else {
temp[t++]=a[j++];
}
}
while (i<=mid) {
temp[t++]=a[i++];
}
while (j<=right) {
temp[t++]=a[j++];
}
t=0;
int tempLeft=left;
while (tempLeft<=right) {
a[tempLeft++]=temp[t++];
}
}
}
八、可处理负数的基数排序
package com.yzh.sort;
public class RadixSort{
public static void main(String[] args) {
int[]a={-2,-1,-6,3,5,1,2,88,-1,99,100,21};
RadixSort(a);
for (int x : a) {
System.out.print(x+" ");
}
System.out.println();
}
public static int[] RadixSort(int[] arr){
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
max = Math.max(max, arr[i]);
min = Math.min(min, arr[i]);
}
if (min<0) {
for (int i = 0; i < arr.length; i++) {
arr[i] -= min;
}
max -= min;
}
int maxLength = (max+"").length();
int[][] bucket = new int[10][arr.length];
int[] bucketElementCount = new int[10];
for (int i = 0 ,n = 1 ; i < maxLength ; i++,n*=10) {
for (int j = 0; j < arr.length ; j++) {
int value = arr[j]/n % 10;
bucket[value][bucketElementCount[value]] = arr[j];
bucketElementCount[value]++;
}
int index = 0;
for (int j = 0; j < bucketElementCount.length ; j++) {
if (bucketElementCount[j]!=0){
for (int k = 0; k < bucketElementCount[j]; k++) {
arr[index] = bucket[j][k];
index++;
}
}
bucketElementCount[j] = 0;
}
}
if (min<0){
for (int i = 0; i < arr.length ; i++) {
arr[i] += min;
}
}
return arr;
}
}