public class InlineSort {
public static void sort(int[] array){
int i,j;
for(i=2;i<array.length;i++){
//为了提高查找效率,下标为0的位置不存放有用的数据,
//而是存放给定的值,这个位置称为监视哨(图中小括号的位置)
array[0] = array[i];
j = i-1;
while(array[0]<array[j]){
array[j+1] = array[j];
j--;
}
array[j+1] = array[0];
}
}
public static void main(String[] args) {
//下标为0的位置不存放有用的数据,所以我用了0来代替
int[] array = {0,53,27,36,15,69,42};
sort(array);
//把原数组的第0个位去掉,得到最后的新数组
int[] newArray = Arrays.copyOfRange(array, 1, array.length);
//打印数组
System.out.println(Arrays.toString(newArray));
}
}
public class HillSort {
public static void sort(int[] array){
int i,j;
for(int gap=array.length/2;gap>0;gap/=2){
for(i=gap+1;i<array.length;i++){
if(array[i]<array[i-gap]){
//其实,这一部分跟直接插入排序是差不多的
array[0] = array[i]; //array[0] 这里不是监视哨
j = i - gap;
while(j>0&&array[0]<array[j]){ //这里注意j>0要在&&的前面,否则会报错
array[j+gap] = array[j];
j = j - gap;
}
array[j+gap] = array[0];
}
}
}
}
public static void main(String[] args) {
//这里数组下标为0的位置是没有意义的,所以我用了0来表示
int[] array = {0,9,1,2,5,7,4,8,6,3,5};
sort(array);
//把原数组的第0个位去掉,得到最后的新数组
int[] newArray = Arrays.copyOfRange(array, 1, array.length);
//打印数组
System.out.println(Arrays.toString(newArray));
}
}
public class BubbleSort {
public static void sort(int[] array){
for(int i=0;i<array.length-1;i++){
for(int j=0;j<array.length-1-i;j++){
if(array[j]>array[j+1]){
//不借助中间变量,就可以达到两数的交换
array[j] = array[j]+array[j+1];
array[j+1] = array[j] - array[j+1];
array[j] = array[j] - array[j+1];
}
}
}
}
public static void main(String[] args) {
int[] array = {3,6,4,2,11,10,5};
sort(array);
//打印数组
System.out.println(Arrays.toString(array));
}
}
public class QuickSort {
public static int pass(int[] array,int i,int j){
array[0] = array[i];
while(i<j){
while(i<j && array[0]<=array[j]){
j--;
}
if(i<j){
array[i] = array[j];
i++; //这里是为了下面少循环一次,不做重复判断
}
while(i<j && array[0]>=array[i]){
i++;
}
if(i<j){
array[j] = array[i];
j--;
}
}
array[i] = array[0];
return i;
}
public static void sort(int[] array,int i,int j){
int k;
if(i<j){
k = pass(array,i,j);
sort(array,i,k-1);
sort(array,k+1,j);
}
}
public static void main(String[] args) {
int[] array = {0,26,5,37,8,63,12,59,15,48,19};
sort(array,1,array.length-1);
//把原数组的第0个位去掉,得到最后的新数组
int[] newArray = Arrays.copyOfRange(array, 1, array.length);
//打印数组
System.out.println(Arrays.toString(newArray));
}
}
public class SelectSort {
public static void sort(int[] array){
int temp;
for(int i=0;i<array.length-1;i++){
temp = i;
for(int j=i+1;j<array.length;j++){
if(array[temp]>array[j]){
temp = j; //找到没有排好序中的最小值的下标
}
}
//减少重复的步骤,也就是当第一个是最小时,就不需要交换了;
if(temp != i){
array[i] = array[i] + array[temp];
array[temp] = array[i] - array[temp];
array[i] = array[i] - array[temp];
}
}
}
public static void main(String[] args) {
int[] array = {4,1,3,6,2,5};
sort(array);
System.out.println(Arrays.toString(array));
}
}
public class HeapSort {
public static void HeapSort(int[] array){
BuildHeap(array,array.length);
for(int i=array.length-1;i>=0;i--){
//交换堆顶元素与末尾元素
swap(array,i,0);
//再将最大的值放在堆顶,但不包括刚刚将堆顶元素放在末尾的在内
sort(array,i,0);
}
}
//构建堆,所有的父节点要大于子节点
public static void BuildHeap(int[] array,int length){
for(int i=(array.length-1)/2;i>=0;i--){
sort(array,array.length,i);
}
}
//父节点要大于子节点
public static void sort(int[] array,int length,int n){
if(n >= length){
return;
}
int left = n*2+1;
int right = n*2+2;
int max = n;
if(left < length && array[left] > array[max]){
max = left;
}
if(right < length && array[right] > array[max]){
max = right;
}
if(max != n){
swap(array,max,n);
sort(array,length,max);
}
}
public static void swap(int[] array,int a ,int b){
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
public static void main(String[] args) {
int[] array = {49,38,65,97,76,13,27,49};
HeapSort(array);
System.out.println(Arrays.toString(array));
}
}
public class MergeSort {
public static void merge(int[] array,int low,int mid,int high){
int[] temp = new int[high - low + 1];
int i = low; //左指针
int j = mid + 1; //右指针
int k = 0; //新数组的指针
while(i <= mid && j <= high){
temp[k++] = array[i] <= array[j] ? array[i++] : array[j++];
}
//左指针部分还有剩余的,直接加到temp的后面
while(i <= mid){
temp[k++] = array[i++];
}
//右指针同理
while(j <= high){
temp[k++] = array[j++];
}
//将temp覆盖array数组
for(int n=0;n<temp.length;n++){
array[low + n] = temp[n];
}
}
public static void sort(int[] array,int low,int high){
if(low<high){
int mid = (low+high)/2;
sort(array,low,mid);
sort(array,mid+1,high);
merge(array,low,mid,high);
}
}
public static void main(String[] args) {
int[] array = {49,38,65,97,76,13,27};
sort(array,0,array.length-1);
System.out.println(Arrays.toString(array));
}
}
public class RadixSort {
public static int[] sort(int[] array,int maxDigit){
int[] result = new int[array.length];
//它的每一位的范围,0-9
int[] count = new int[10];
for(int i=0;i<maxDigit;i++){
int division = (int)Math.pow(10,i);
for(int n=0;n<array.length;n++){
//求每一位的数值,进行求余
int num = array[n]/division%10;
//将这个每一位的数值作为count的下标,再计算其出现多少次
count[num]++;
}
//进行累加,从下标值为1开始进行累加
for(int m=1;m<count.length;m++){
count[m] = count[m] + count[m-1];
}
//将原来的数组进行倒叙,通过累加后的count数组,
//获取count数组每个下标所对应的值就是下标的最后一个下标出现在result的位置减一,
//然后进行递减,这样做是稳定的,最后得到的数组的按顺序排列的
for(int j=array.length-1;j>=0;j--){
int num = array[j]/division%10;
result[--count[num]] = array[j];
}
//将array的数组里面的值复制为了result
System.arraycopy(result, 0, array, 0, array.length);
//填充count数组中的每个元素都是0,清空值
Arrays.fill(count,0);
}
return result;
}
//获取最高的位数
public static int getMaxDigit(int[] array){
int num=0;
//获取数组中最大的值
int maxValue = Arrays.stream(array).max().getAsInt();
//获取它的位数
while(maxValue!=0){
maxValue/=10;
num++;
}
return num;
}
public static void main(String[] args) {
int[] array = {312,236,187,330,222,457,113};
int[] result = sort(array,getMaxDigit(array));
System.out.println(Arrays.toString(result));
}
}
注:以上的所有图片来源于百度图片搜索