算法
冒泡排序
package argorithm;
import java.util.Arrays;
public class Bubble {
public static void main(String[] args) {
int[] xx=new int[]{10,2,3,44,32,34,24,65,787,23};
Bubble.bubble(xx);
}
public static void bubble(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
for (int j = nums.length - 1 ; j > i ; j --) {
if (nums[j] < nums[j - 1]) {
int temp = nums[j];
nums[j] = nums[j - 1];
nums[j - 1] = temp;
}
}
}
System.out.println(Arrays.toString(nums));
}
}
插入排序
package argorithm;
import java.util.Arrays;
public class InsertSort {
public static void main(String[] args) {
insertSort(new int[]{321, 3214, 235, 3, 6234, 345, 45, 623, 4456, 123});
}
public static void insertSort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
int temp = nums[i];
int j = i;
while (j > 0 && nums[j-1] >= temp) {
nums[j] = nums[j - 1];
j--;
}
nums[j] = temp;
}
System.out.println(Arrays.toString(nums));
}
}
package argorithm;
import java.util.Arrays;
public class StraightSectionSort {
public static void main(String[] args) {
sectionSort(new int[]{100,3,4,59,235,56,234,45,234,456123,4546,34});
}
public static void sectionSort(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
int k=i;
for (int j = i +1; j < nums.length; j++) {
if (nums[k] < nums[j]) {
k = j;
}
}
int temp = nums[i];
nums[i]=nums[k];
nums[k]=temp;
}
System.out.println(Arrays.toString(nums));
}
}