1.冒泡排序
算法复杂度:O(N^2)
时间复杂度:O(1)
public class BubbleSort{
public static void main(String[] args){
int[] nums = new int[]{1,3,7,6,4,10,8,2};
bubbleSort(nums);
for(int num : nums){
System.out.println(num);
}
}
public static void bubbleSOrt(int[] nums){
if(nums == null || nums.length < 2){
return;
}
for(int i = nums.length; i > 0; i--){
for(int j = 0; j < i-1; j++){
if(nums[j] > nums[j+1]){
swap(nums,j,j+1);
}
}
}
}
public static void swap(int[] nums,int i,int j){
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
2.插入排序
算法复杂度:O(N^2)
时间复杂度:O(1)
public class InsertleSort{
public static void main(String[] args){
int[] nums = new int[]{1,3,7,6,4,10,8,2};
insertSort(nums);
for(int num : nums){
System.out.println(num);
}
}
public static void (int[] nums){
}
public static void swap(int[] nums,int i,int j){
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
3.选择排序
算法复杂度:O(N^2)
时间复杂度:O(1)
public class SelectSort{
public static void main(String[] args){
int[] nums = new int[]{1,3,7,6,4,10,8,2};
selectSort(nums);
for(int num : nums){
System.out.println(num);
}
}
public static void (int[] nums){
}
}
4.快速排序
5.桶排序
6.归并排序
7.堆排序
|