冒泡排序
public class BubbleSorting20230704 {public static void main(String[] args) {int[] numbers = new int[]{2,3,1,5,4};for(int i=0;i<numbers.length;i++){for (int j=0;j<numbers.length-1-i;j++){if(numbers[j]>numbers[j+1]){int temp = numbers[j];numbers[j] = numbers[j+1];numbers[j+1] =temp;}}}for(int num:numbers){System.out.println(num);}}
}
插入排序
public class InsertSort {public static void main(String[] args) {int[] nums = new int[]{1,8,2,7,3,4,5};for(int i = 0;i<nums.length-1;i++){for(int j=i+1;j>0;j--){int prev = j-1;if(nums[j]<nums[prev]){int temp = nums[prev];nums[prev] = nums[j];nums[j] = temp;}}}for(int num:nums){System.out.println(num);}}
}
选择排序
public class SelectionSorting20230704 {public static void main(String[] args) {int[] numbers = new int[]{2,3,1,5,4,15};for(int i=0;i<numbers.length;i++){int minIndex = i;int minNumber = numbers[i];for(int j=i;j<numbers.length;j++){if(minNumber>numbers[j]){minIndex = j;}}if(minIndex != i){minNumber = numbers[minIndex];numbers[minIndex] = numbers[i];numbers[i] = minNumber;}}for(int num:numbers){System.out.println(num);}}
}
归并排序
public class MergeSorting20230705 {public static void main(String[] args) {int[] nums = new int[]{300,3,1,5,4,400};sort(nums,0,nums.length-1);for(int num:nums){System.out.println(num);}}public static void sort(int[] sortingNumbers,int beginIndex,int endIndex){if(beginIndex>=endIndex){return;}int middle = beginIndex+(endIndex-beginIndex)/2;sort(sortingNumbers,beginIndex,middle);sort(sortingNumbers,middle+1,endIndex);merge(sortingNumbers,beginIndex,middle,endIndex);}public static void merge(int[] sortingNumbers,int startIndex,int middleIndex,int lastIndex) {int i = startIndex;int j = middleIndex + 1;int k = 0;int[] tem = new int[lastIndex - startIndex + 1];while (i <= middleIndex && j <= lastIndex) {if (sortingNumbers[i] > sortingNumbers[j]) {tem[k++] = sortingNumbers[j++];} else {tem[k++] = sortingNumbers[i++];}}int moveBegin = i;int moveEnd = middleIndex;if(i>middleIndex){moveBegin = j;moveEnd = lastIndex;}while(moveBegin<=moveEnd){tem[k++] = sortingNumbers[moveBegin++];}for(int record=0;record<tem.length;record++){sortingNumbers[startIndex+record] = tem[record];}}
}
快速排序:
public class QuickSort20230522 {public static void main(String[] args) {int[] nums = new int[]{2,3,1,5,4};quickStorting(nums,0,nums.length-1);for(int n:nums){System.out.println(n);}}static void quickStorting(int[] numbers,int beginIndex,int endIndex){if(beginIndex>=endIndex){return;}int povit = getPovit(numbers,beginIndex,endIndex);quickStorting(numbers,beginIndex,povit-1);quickStorting(numbers,povit+1,endIndex);}static int getPovit(int[] numbers,int begin,int end){int index =begin;int numberUnderIndex = numbers[end];for(int i=begin;i<end;i++){if(numbers[i]<numberUnderIndex){int number = numbers[i];numbers[i] = numbers[index];numbers[index] = number;index = index +1;}}numbers[end] = numbers[index];numbers[index] = numberUnderIndex;return index;}
}
我是知识星球上约有3万人的AI破局俱乐部初创合伙人,我的微信号是zhaoseaside
,欢迎大家加我,相互学习AI知识和个人IP知识,毕竟这是未来两大风口,加我备注“数字人”,有我制作简单数字人的经验贴。