Sort algorithm Java

Source: Internet
Author: User

 //1. Bubble sort     Public voidBubblesort (int[] arr) {         for(inti=0;i<arr.length;i++){             for(intj=0;j<arr.length-i-1;j++){                if(Arr[j] > arr[j+1]){                    inttemp =Arr[j]; ARR[J]= Arr[j+1]; Arr[j+1] =temp;    }}} System.out.println (arr); }    //2. Quick-line     Public voidQuickint[] arr) {QuickSort (arr,0,arr.length-1);    System.out.println (); }    Private voidQuickSort (int[] arr,intLowintHigh) {//each time you select a datum element, it is smaller than the element on the left, small on the right, divided into two parts, and then recursively .        if(Low <High ) {            intPrivotloc = Quicksorthelper (arr, low, high);//Split a table in SplitQuickSort (arr, Low, privotLoc-1);//Recursive ordering of low sub-tables recursivelyQuickSort (arr, Privotloc + 1, high);//Recursive ordering of Gaozi recursively        }    }    Private intQuicksorthelper (int[] arr,intLowintHigh ) {        intPrivotkey =Arr[low];  while(Low <High ) {             while(Low < High && Arr[high] >= privotkey) high--;//search forward from the point of high, up to the low+1 position. Swap smaller than the base element to the low end            inttemp =Arr[low]; Arr[low]=Arr[high]; Arr[high]=temp;  while(Low < High && Arr[low] <= privotkey) + +Low ; Temp=Arr[low]; Arr[low]=Arr[high]; Arr[high]=temp; }        returnLow//return to split point    }    //3. Merge sort     Public voidMergeSort (int[] arr) {//divides the array into two parts, recursively until each part has only one element, and then merges the adjacent two partsSplit (arr,0,arr.length-1);    System.out.println (); }    Private voidSplitint[] arr,intStartintend) {        if(start = = end)//split to each single element as a group            return; intMid = (start + end)/2;        Split (Arr,start,mid); Split (Arr,mid+1, end); Merge (Arr,start,mid,mid+1, end); }    Private voidMergeint[] arr,intLowstart,intLowend,intHighstart,inthighend) {        int[] temp =New int[(Lowend-lowstart) + (Highend-highstart) +2]; intI=lowstart,j=highstart,k=0;  while(i<=lowend&&j<=highend) {            if(Arr[i] <Arr[j]) {Temp[k++] =Arr[i]; I++; }Else{temp[k++] =Arr[j]; J++; }        }         while(i<=lowend) {Temp[k+ +] = arr[i++]; }         while(j<=highend) {Temp[k+ +] = arr[j++]; }         for(intm=0;m<temp.length;m++) {Arr[lowstart+ M] =Temp[m]; }    }    //4. Direct Insert Sort     Public voidInsertsort (int[] arr) {//Insert directly sequentially        if(arr.length==0)return;  for(inti=1;i<arr.length;i++) {            if(Arr[i] < arr[i-1]){                intj= i-1; intx = Arr[i];//Copy as Sentinel, which stores the elements to be sortedArr[i] = arr[i-1];//move one element at a succession                 while(j>=0 && x < arr[j]) {//find the insertion position in an ordered tableARR[J+1] =Arr[j]; J--;//element Move back} arr[j+1] =x;    }} System.out.println (); }    //5, simple selection of sorting    /*** * In the group of numbers to be sorted, select the minimum (or maximum) number to exchange with the number of the 1th position, and then find the minimum (or maximum) number of the 2nd position in the remaining number, and so on,     Until the first N-1 element (the penultimate number) and the nth element (the last number) are compared. * @paramarr Array*/     Public voidSimpleselectionsort (int[] arr) {         for(inti=0;i<arr.length;i++){            intMinindex =i;  for(intJ=i + 1;j<arr.length;j++){                if(Arr[j] <Arr[minindex]) {Minindex=J; }            }            inttemp =Arr[minindex]; Arr[minindex]=Arr[i]; Arr[i]=temp;    } System.out.println (); }    //6. Heap sequencing    /*** 1. How to build a heap of n backlog; 2. After the top element of the heap is output, how to adjust the remaining n-1 elements to become a new heap. * @paramarr*/     Public voidHeapsort (int[] arr)        {buildheap (arr);  for(inti = arr.length-1; i > 0; --i) {//swap heap top element h[0] and the last element in the heap            inttemp =Arr[i]; Arr[i]= Arr[0]; arr[0] =temp; //The heap is adjusted each time you swap the top element of the heap and the last element in the heapAdjustheap (arr,0, i); }    }    Private voidBuildheap (int[] arr) {        //position of the last node with children i= (length-1)/2         for(inti = (arr.length-1)/2; I >= 0; --i) adjustheap (arr,i,arr.length); }    Private voidAdjustheap (int[] arr,intIndexintlength) {        intChild = 2*index+1;//the location of the left child's knot point. (I+1 is the location of the right child node for the current adjustment node)        inttemp =Arr[index];  while(Child <length) {            if(child+1 <length && arr[child]<arr[child+1]) {//If the right child is larger than the left child (find a child node that is larger than the current adjustment node)++Child ; }            if(Arr[child] > Arr[index]) {////if the smaller child nodes are larger than the parent nodeArr[index] = Arr[child];//then move the smaller sub-node up, replacing its parent node .index = child;//reset S, which is the position of the next node to be adjustedChild = 2*index+1; }Else                 Break; Arr[index]=temp; }    }    //7. Hill sort-Insert Sort//8. Base Order

Sort algorithm Java

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.