排序算法 一
1.冒泡排序
public void bubbleSort(int[] nums) {
if (nums == null){
return;
}
for (int i = 0; i < nums.length-1; i++) {
for (int j = 0; j < nums.length-1-i; j++) {
if (nums[j+1]<nums[j]){
swap(nums,j,j+1);
}
}
}
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
int[] nums = new int[]{3, 2, 1, 5, 6, 4, 22, 35, 44, 45,5, 3, 1, 2, 3, 4, 5, 19, 33, 44, 65, 66};
mySort.bubbleSort(nums);
int[] nums = {1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 6, 19, 22, 33, 35, 44, 44, 45, 65, 66};
2.快速排序(经典写法)
public void quickSort(int[] nums, int low, int high) {
if (low>high){
return;
}
int i = low;
int j = high;
int temp = nums[low];
while (i<j){
while (i<j && temp<= nums[j]){
j--;
}
while (i<j && temp >= nums[i]){
i++;
}
if (i<j){
swap(nums,i,j);
}
}
nums[low] = nums[i];
nums[i] = temp;
quickSort(nums,low,j-1);
quickSort(nums,j+1,high);
}
mySort.quickSort(nums,0,nums.length-1);