package sort; /** * @author Administrator * @version created: April 12, 2018 9:32:48 AM * @ClassName class name * @Description class description */ public class Sort { private int[] nums= {2,4,6,7,98,0,2,6,9,1}; //private int[] nums= {5,4,3,2,1}; public void swap(int[] nums,int i,int j){//Exchange elements in sets such as arrays, pass by reference, not by value int temp; temp =nums[i]; nums[i]=nums[j]; nums[j]=temp; } public void BubbleSort0(int[] nums,int n){//Bubble sort primary version: the simplest exchange sort int i,j; for(i=0;i<n-1;i++){ for(j=i+1;j<n;j++){ if(nums[i]>nums[j]) swap(nums,i,j); } } } public void BubbleSort(int[] nums,int n){//Bubble sort int i,j; for(i=0;i<n;i++){//For each round of comparison, put the smallest one at the top for(j=n-1;j>i;j--){//Exchange from the end to i in pairs, the smaller one is placed in the front, that is, "bubble", note that j is a loop from back to front if(nums[j-1]>nums[j]) swap(nums,j-1,j); } } } /*Bubble sort optimization, increase the mark to reduce the number of sorting, and avoid meaningless loop judgment when it is already ordered. */ public void BubbleSort2(int[] nums,int n){ int i,j,flag=1;//flag is used to mark whether there is a swap operation for(i=0;i<n && flag==1;i++){//If the flag is 0, it means that there is no exchange, then exit the loop flag = 0; //0 means no swap for(j=n-1;j>i;j--){ if(nums[j-1]>nums[j]){ swap(nums,j-1,j); flag = 1;//If an exchange occurs, the flag is set to 1 } } } } //Selection sort: find the minimum value in the sequence, exchange the minimum value with the value of the current position public void SelectSort(int[] nums,int n){ int i, j, min; for(i=0;i<n-1;i++){ min=i; //Define the current subscript as the minimum subscript for(j=i+1;j<n;j++){ //Data after loop if(nums[j]<nums[min]){//If there is a keyword less than the current minimum value, assign the subscript of this keyword to min min = j; } if(i!=min) //If min is not equal to i, the minimum value is found and exchange swap(nums,i,min); } } } /* Insertion sort * The basic idea is that in the process of traversing the array, it is assumed that the elements before the serial number i (i>=1), that is, [0..i-1], have been sorted. * This time, you need to find the correct position k of the element x corresponding to i, and in the process of finding this position k, move the compared elements back one by one, * "Make a position" for the element x, and finally assign the element value corresponding to k as x */ public void InsertSort(int[] nums,int n){ int i,j,temp; for(i=1;i<n;i++){ //Only when the current position i element is less than the largest element nums[i-1] in the already ordered [0,i-1], it needs to be inserted if(nums[i]<nums[i-1]){ temp=nums[i];//Store the element at the current position i, where [0,i-1] is already ordered for(j=i;j>0&&nums[j-1]>temp;j--){ nums[j]=nums[j-1];// Traverse from i-1 to 0, and shift the number larger than temp by one place } nums[j]=temp;//Insert into the appropriate position } } } /* Hill sort */ public void ShellSort(int[] nums,int n){ int i,j,temp; int h = 1; /* Regarding the step size, there is no uniform standard for the value, it must be less than size, and the last step size must be 1 */ for(h = 1;h < n/3;h= 3*h+1);//Calculate the first step for(;h>0;h=h/3){ for(i=h;i<n;i++){ //Only when the current position i element is less than the largest element nums[ih] in the already ordered [0,i-1], it needs to be inserted if(nums[i]<nums[i-h]){ temp=nums[i];//Store the element at the current position i, where [0,i-1] is already ordered for(j=i;j>h-1&&nums[j-h]>temp;j=j-h){ nums[j]=nums[jh];// Traverse from ih to h-1, and shift h bits after the number larger than temp } nums[j]=temp;//Insert into the appropriate position } } } } /* Adjust the big top heap (just the adjustment process, based on the large top heap has been built), * That is, the keywords recorded in [s..m] meet the definition of heap except [s]; * s represents the start subscript of the range to be adjusted, m represents the end subscript, * That is [s,...,m], the total number is m-s+1, m can be taken */ private void HeapAdjust(int[] nums,int s,int m){ int i,temp; temp = nums[s];//First take out the current element s for(i=2*s+1;i<=m;i=i*2+1){//Starting from the left child node of the s node, that is, starting at 2s+1, along the keyword is larger The child nodes of the filter down if(i<m && nums[i]<nums[i+1]){//i is the subscript of the record with the larger keyword in the child node; i<m ensures that the right child exists i++; } if(temp<nums[i]){ //If the child node is greater than the parent node, assign the child node value to the parent node (without swapping) nums[s]=nums[i]; s=i; }else break; } nums[s]=temp;//Insert the current element to the final position } // heap sort public void HeapSort(int[] nums,int n){ int i; //1, build a large top heap for(i=n/2-1;i>=0;i--){ //Adjust the structure from bottom to top and right to left from the first non-leaf node HeapAdjust(nums,i,n-1);//The length is n, then the subscript of the last element is n-1 } //2. Adjust the heap structure + swap the top and end elements of the heap for(i=n-1;i>=0;i--){ swap(nums,0,i);//Swap the top element of the heap with the last element HeapAdjust(nums,0,i-1);//Re-adjust the heap } } // merge sort public void MergeSort(int[] nums,int n){ int[] temp = new int[n];//Before sorting, build a temporary array whose length is equal to the length of the original array to avoid frequent opening of space in recursion MSort(nums,temp,0,n-1); } private void MSort(int[] nums,int[] temp,int left,int right){ int mid; //if(left==right) temp[left]=nums[right]; if(left<right){ mid = (left+right)/2; MSort(nums,temp,left,mid);//Left merge sort, so that the left subsequence is ordered MSort(nums,temp,mid+1,right);//Merge sort on the right, so that the right subsequence is ordered Merge(nums,temp,left,mid,right);//Merge two ordered subarrays } } //right is the index of the last element of the sequence private void Merge(int[] nums,int[] temp,int left,int mid,int right){ int i=left; //left sequence pointer int j=mid+1; //Right sequence pointer int k=0; //temporary array pointer while(i<=mid && j<=right){ if(nums[i]<nums[j]){ temp[k++]=nums[i++]; }else{ temp[k++]=nums[j++]; } } while(i<=mid){ //fill the remaining elements on the left into temp temp[k++]=nums[i++]; } while(j<=right){ //fill the remaining elements of the right sequence into temp temp[k++]=nums[j++]; } k = 0; while(left<=right){ //Copy all elements in temp to the original array nums[left++]=temp[k++]; } } //quick sort public void QuickSort(int[] nums,int n){ QSort(nums,0,n-1); } public void QSort(int[] nums,int low,int high){ int pivot; if(low<high){ pivot=Partition(nums,low,high);/* Divide L->r[low..high] into two, */ /* Calculate the pivot value pivot */ QSort(nums,low,pivot-1); QSort(nums,pivot+1,high); } } public int Partition(int[] nums,int low,int high){ int pivotkey=nums[low]; while(low<high){ while(low<high && nums[high]>=pivotkey){ high--; } swap(nums,low,high); while(low<high && nums[low]<=pivotkey){ low++; } swap(nums,low,high); } return low; } //binary sort tree class Node{//The node class of the binary sorting tree public int data; public Node left; public Node right; public Node(int data){ this.data = data; this.left = null; this.right = null; } } private Node root=null; static int ii =0;//Pointer, put the ordered tree of recursive in-order traversal into the nums array public void BSTSort(int[] nums,int n){ CreateBST(nums,n);//Generate binary sorting tree according to the array to be sorted MidOrderTraverse(root,nums); } public void InsertNode(int data){ Node newNode = new Node(data); if(root == null){ root = newNode; }else{ Node current = root; Node parent = current; while(current != null){ parent = current; if(data < current.data){ current = current.left; }else{ current = current.right; } } if(data < parent.data){ parent.left = newNode; }else{ parent.right = newNode; } } } public void CreateBST(int[] nums,int n){ for(int i = 0;i<n;i++){ InsertNode(nums[i]); } } //Inorder traversal of binary tree public void MidOrderTraverse(Node root,int[] nums){ if(root != null){ MidOrderTraverse(root.left,nums); nums[ii++]=root.data; MidOrderTraverse(root.right,nums); } } public static void main(String[] args) { // TODO auto-generated method stub Sort ss = new Sort(); ss.BSTSort(ss.nums, ss.nums.length); for(int i = 0;i<ss.nums.length;i++) System.out.println(ss.nums[i]); } }
Java implementation of various sorting
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324644201&siteId=291194637
Recommended
Ranking