一、选择排序
public static void main(String[] args) {
int[] nums = {1,2,8,4,6,7,3,6,4,9};
for (int i=0; i<nums.length; i++) {
for (int j=i+1; j<nums.length; j++) {
if (nums[i] > nums[j]) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
for (int i=0; i<nums.length; i++) {
System.out.print(nums[i] + " ");
}
}
二、冒泡排序
public static void main(String[] args) {
int[] nums = {1,2,8,4,6,7,3,6,4,9};
for (int i=1; i<nums.length; i++) {
for (int j=0; j<nums.length-i; j++) {
if (nums[j] > nums[j+1]) {
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
for (int i=0; i<nums.length; i++) {
System.out.print(nums[i] + " ");
}
}
三、插入排序
public static void main(String[] args) {
int[] nums = {1,2,8,4,6,7,3,6,4,9};
for (int i=1; i<nums.length; i++) {
for (int j=i; nums[j]<nums[j-1]; j--) {
int temp = nums[j];
nums[j] = nums[j-1];
nums[j-1] = temp;
}
}
for (int i=0; i<nums.length; i++) {
System.out.print(nums[i] + " ");
}
}