经典排序算法:
利用leetcode题目学习排序算法,原题地址:https://leetcode-cn.com/problems/sort-colors/
class Solution {
public void sortColors(int[] nums) {
// int len = nums.length;
// int zero = 0, one = 0, two = len - 1;
// while(one <= two){
// if(nums[one] == 2){
// swap(nums, one, two--);
// }else if(nums[one] == 0){
// swap(nums, one++, zero++);
// }else{
// one++;
// }
// }
//maoSort(nums);
//quickSort1(nums, 0, nums.length);
//selectSort(nums);
//insertSort(nums);
//shellSort(nums);
//mergeSort(nums, 0, nums.length);
heapSort(nums);
}
//计数排序和桶排序
/*
整体思想:利用要排序的值,将其作为index映射到另一个数组中,再按序取出,
桶排序则是分几个桶装,在合并
*/
//堆排序
public void heapSort(int[] nums){
int len = nums.length - 1;
int begin = (len - 1) >> 1;
for(int i = begin; i >= 0; i--){
adjustHeap(nums, i, len);
}
for(int i = len; i > 0; i--){
swap(nums, 0, i);
adjustHeap(nums, 0, i - 1);
}
}
public void adjustHeap(int[] nums, int begin, int end){
int l = (begin << 1) + 1;
int r = l + 1;
int mmax = l;
if(l > end)
return;
if(r <= end && nums[r] > nums[l]){
mmax = r;
}
if(nums[mmax] > nums[begin]){
swap(nums, mmax, begin);
adjustHeap(nums, mmax, end);
}
}
//递归快速排序
public void quickSort1(int[] nums, int begin, int end){
Stack<Integer> st = new Stack<Integer>();
st.push(begin);
st.push(end);
int index = 0;
while(!st.empty()){
end = st.peek();st.pop();
begin = st.peek();st.pop();
index = partition(nums, begin, end);
if(begin < index){
st.push(begin);
st.push(index);
}
if(index + 1 < end){
st.push(index + 1);
st.push(end);
}
}
}
//快速排序
public void quickSort(int[] nums, int begin, int end){
if(end <= begin + 1)
return;
int index = partition(nums, begin, end);
quickSort(nums, begin, index);
quickSort(nums, index + 1, end);
}
public int partition(int[] nums, int begin, int end){
int key = nums[begin];
int i = begin, j = end - 1;
while(i < j){
while(i < j && nums[j] >= key){
j = j - 1;
}
if(i < j)
nums[i++] = nums[j];
while(i < j && nums[i] <= key){
i = i + 1;
}
if(i < j)
nums[j--] = nums[i];
}
nums[i] = key;
return i;
}
//归并排序
public void mergeSort(int[] nums, int begin, int end){
if(end <= begin + 1)
return ;
int mid = begin + (end - begin)/2;
mergeSort(nums, begin, mid);
mergeSort(nums, mid, end);
merge(nums, begin, mid, end);
}
public void merge(int[] nums, int begin, int mid, int end){
int[] res = new int[end - begin];
int pos = 0, t = begin;
int begin2 = mid;
while(begin < mid || begin2 < end){
if(begin2 == end){
res[pos++] = nums[begin++];
}else if(begin == mid){
res[pos++] = nums[begin2++];
}else if(nums[begin] <= nums[begin2]){
res[pos++] = nums[begin++];
}else{
res[pos++] = nums[begin2++];
}
}
for(int i = t; i < end; i++){
nums[i] = res[i - t];
}
}
//希尔排序
public void shellSort(int[] nums){
int len = nums.length, i, temp, j;
int gap = len/2;
while(gap > 0){
for(i = gap; i < len; i++){
temp = nums[i];
for(j = i - gap; j >= 0 ; j = j - gap){
if(temp < nums[j]){
nums[j + gap] = nums[j];
}else{
break;
}
}
nums[j + gap] = temp;
}
gap = gap/2;
}
}
//插入排序
public void insertSort(int[] nums){
int len = nums.length, i, j=0;
for(i = 1; i < len; i++){
int temp = nums[i];
for(j = i - 1; j >= 0 ; j--){
if(temp < nums[j]){
nums[j + 1] = nums[j];
}else{
break;
}
}
nums[j + 1] = temp;
}
}
//冒泡排序
public void maoSort(int[] nums){
int len = nums.length;
for(int i = 0; i < len; i++){
for(int j = i + 1; j < len; j++){
if(nums[i] > nums[j]){
swap(nums, i, j);
}
}
}
}
//选择排序
public void selectSort(int[] nums){
int len = nums.length;
int pos;
for(int i = 0; i < len; i++){
pos = i;
for(int j = i + 1; j < len; j++){
if(nums[j] < nums[pos]){
pos = j;
}
}
swap(nums, i, pos);
}
}
public void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}