这段时间刷题总结的排序算法,由于自己是练习,基本没有注释
package com.mzz;
public class Sort {
public static void main(String[] args) {
int[] nums=new int[]{11,4,28,6,32,42,34,15,65,23,56,3,87,95,34,1};
//冒泡排序
//Main.bubbleSort(nums);
//选择排序
//Main.selectSort(nums);
//插入排序
//Main.insertSort(nums);
//归并排序
//希尔排序
//Main.shellSort(nums);
//归并排序
//Mian.mergeSort(nums, 0, nums.length);
//快排
//Main.quickSort(nums, 0, nums.length-1);
//Main.showNums(nums);
//二叉树元素插入
// Node root=new Node(10);
// for(int i=0;inums[j+1]){
int temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
return;
}
public static void selectSort(int[] nums){
for(int i=nums.length-1;i>=0;i--){
int index=0;
for(int j=0;j<=i;j++){
if(nums[index]=0;j-=1){
if(nums[j]>temp)nums[j+1]=nums[j];
else break;
}
nums[j+1]=temp;
}
}
public static void shellSort(int[] nums) {
int h=1;
while(h0){
for(int i=h;i=0;j-=h){
if(nums[j]>temp){
nums[j+h]=nums[j];
}else{
break;
}
}
nums[j+h]=temp;
}
h=(h-1)/3;
}
}
public static void mergeSort(int[] nums, int low,int high){
if(high-low==1)return;
int mid=(high+low)/2;
mergeSort(nums, low, mid);
mergeSort(nums, mid, high);
merge(nums, low, mid, high);
}
public static void merge(int[] nums,int low,int mid,int high){
int left=low;
int right=mid;
int index=0;
int[] temp=new int[high-low];
while(left=right) return;
int pivot=nums[rightBound];
while(left=pivot)right--;
if(left>=right)break;
else{
int temp=nums[left];
nums[left]=nums[right];
nums[right]=temp;
}
}
nums[rightBound]=nums[left];
nums[left]=pivot;
quickSort(nums, leftBound, left-1);
quickSort(nums, left+1, rightBound);
}
public static void insert(Node root,int value){
Node newNode=new Node(value);
if(root==null){
root=newNode;
return;
}
Node currentNode=root;
Node parent;
while(true){
parent=currentNode;
if(currentNode.value0){
int parentIndex=(index-1)/2;
if(heapArray[index].value>heapArray[parentIndex].value){
int temp=heapArray[index].value;
heapArray[index].value=heapArray[parentIndex].value;
heapArray[parentIndex].value=temp;
}else{
return;
}
index=parentIndex;
}
}
public static void heapSort(int[] nums){
for(int i=nums.length/2-1;i>=0;i--){//从第一个非叶子节点,依次向首元素方向前进,开始构建大顶堆
adjustHeap(nums,i,nums.length);
}//所有循环完毕,说明每个节点的值都大于等于其左右子节点值
for(int j=nums.length-1;j>=0;j--){
int temp=nums[j];//将构建的大顶堆的根节点与最后一个节点调换位置
nums[j]=nums[0];
nums[0]=temp;
adjustHeap(nums, 0,j);//由于此时从跟节点到已经排好序节点前的节点中,根节点不满足大顶堆条件,因此需要依次调整
}
//每循环完一次,都会将最大元素放到其排完序的最后位置
}
private static void adjustHeap(int[] nums, int i,int length) {
int temp=nums[i];
for(int j=i*2+1;jtemp){//如果两个子节点最大值大于其父节点
nums[i]=nums[j];//则将子节点值赋给父节点,相当于最大值子节点向上移
i=j;//此时最大值子节点变为下一次循环的父节点
}else{//如果父节点大于子节点,就跳出循环,跳出后将原始开始节点的值方法最后的父节点上
break;
}
}
nums[i]=temp;//将原始开始节点值放入最后父节点位置。
//本次循环完,相当于开始的i节点存储的是该节点及左右子树节点中最大值。
}
}
class Node{
public int value;
public Node leftNode;
public Node rightNode;
public Node(int value){
this.value=value;
}
}



