Various sorting algorithms implemented by Java (including bubbling, quick-row, etc.)

Source: Internet
Author: User

Heap Sort unstable import Java.util.arrays;public class Heapsort {public static void main (string[] args) {int[] a={49,        38,65,97,76,13,27,49,78,34,12,64};          int arraylength=a.length;              Loop build heap for (int i=0;i<arraylength-1;i++) {//Jian Yu buildmaxheap (a,arraylength-1-i);              Swap heap top and last element swap (a,0,arraylength-1-i);          System.out.println (Arrays.tostring (a)); }}//To the data array from 0 to LastIndex build a large top heap public static void Buildmaxheap (int[] data, int lastIndex) {//from LastIndex            The parent node of the node (the last node) begins the for (int i= (lastIndex-1)/2;i>=0;i--) {//k Saves the node being judged int k=i; If the child node of the current K node exists while (K*2+1<=lastindex) {//k The index of the left child node of the node int biggerindex=2*k+                1; If Biggerindex is less than lastIndex, that is, the right child node of the K node represented by biggerindex+1 exists if (biggerindex<lastindex) {/ /If the value of the right child node is larger if (data[biggerindEx]<data[biggerindex+1]) {//biggerindex always records the index biggerindex++ of the larger child nodes; }}//If the value of the K-node is less than the value of its larger child node if (data[k]<data[biggeri                      Ndex]) {//swap their swap (DATA,K,BIGGERINDEX);                  The Biggerindex is given a K, starting the next loop of the while loop, and re-guaranteeing that the value of the K node is greater than the value of its left and right child nodes k=biggerindex;                  }else{break;          }}}}//exchange private static void Swap (int[] data, int i, int j) {int tmp=data[i];          DATA[I]=DATA[J];      data[j]=tmp; }}//Insert sort algorithm stable public class Insertsort {public static void main (string[] args) {int[] a={49,38,65,97,76,13,27,        49,78,34,12,64,1};        System.out.println ("Before sorting:");        for (int i = 0; i < a.length; i++) {System.out.print (a[i]+ ""); }//Insert sort directly for (int i = 1; i < A.Length            i++) {//To insert element int temp = A[i];            Int J;            /*for (j = i-1; j>=0 && a[j]>temp; j--) {//will move backwards than temp one a[j+1] = a[j];                    }*/for (j = i-1; j>=0; j--) {//will move backwards greater than temp one if (a[j]>temp) {                A[J+1] = A[j];                }else{break;        }} a[j+1] = temp;        } System.out.println ();        System.out.println ("After sorting:");        for (int i = 0; i < a.length; i++) {System.out.print (a[i]+ ""); }}}//Merge sort stable public class Mergsort {public static void main (string[] args) {int[] a={49,38,65,97,76,13,27        , 49,78,34,12,64,1,8};        System.out.println ("Before sorting:");        for (int i = 0; i < a.length; i++) {System.out.print (a[i]+ "");        }//merge sort mergesort (a,0,a.length-1);        System.out.println (); System. OUT.PRINTLN ("After sorting:");        for (int i = 0; i < a.length; i++) {System.out.print (a[i]+ ""); }} private static void MergeSort (int[] A, int left, int. right) {if (left<right) {int middle =            (left+right)/2;            Recursive MergeSort (A, left, middle) on the right side;            Recursive MergeSort (A, middle+1, right) to the left;        Merging merge (A,left,middle,right);        }} private static void merge (int[] A, int left, int middle, int. right) {int[] Tmparr = new Int[a.length]; int mid = middle+1;        Start position on right int tmp = left;        int third = left;                while (Left<=middle && mid<=right) {//Select a smaller number from two arrays into the intermediate array if (A[left]<=a[mid]) {            tmparr[third++] = a[left++];            }else{tmparr[third++] = a[mid++];        }}//Put the remaining parts into the middle array while (Left<=middle) {tmparr[third++] = a[left++];    }    while (Mid<=right) {tmparr[third++] = a[mid++];        }//Copy the intermediate array back to the original array while (Tmp<=right) {a[tmp] = tmparr[tmp++]; }}}//Bubble sort stable public class MP {public static void main (string[] args) {int[] a={49,38,65,97,76,13,27,49,78        , 34,12,64,1,8};        System.out.println ("Before sorting:");        for (int i = 0; i < a.length; i++) {System.out.print (a[i]+ "");                }//bubble sort for (int i = 0; i < a.length; i++) {for (int j = 0; j<a.length-i-1; j + +) {                    Here-I is mainly each traversal of the maximum number of I sank to the bottom, there is no need to replace if (a[j]>a[j+1]) {int temp = a[j];                    A[J] = a[j+1];                A[J+1] = temp;        }}} System.out.println ();        System.out.println ("After sorting:");        for (int i = 0; i < a.length; i++) {System.out.print (a[i]+ ""); }}}//fast row unstable public class QuickSort {public static voiD main (string[] args) {int[] a={49,38,65,97,76,13,27,49,78,34,12,64,1,8};        System.out.println ("Before sorting:");        for (int i = 0; i < a.length; i++) {System.out.print (a[i]+ "");        }//Fast sort quick (a);        System.out.println ();        System.out.println ("After sorting:");        for (int i = 0; i < a.length; i++) {System.out.print (a[i]+ "");        }} private static void Quick (int[] a) {if (a.length>0) {quickSort (a,0,a.length-1);            }} private static void QuickSort (int[] A, int low, int. high) {if (Low

  

Various sorting algorithms implemented by Java (including bubbling, quick-row, etc.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.