Basic sorting algorithm, Java implementation (fast, bubbling, selection, heap sort, insert)

Source: Internet
Author: User

1. Bubble Sort:

(1) compare adjacent elements. If the first one is bigger than the second one, swap them both.

(2) A loop will be set outside.

Algorithm complexity: O (N2) not verbose, on the code:

Bubble sort (22 interchange, plus an outer loop) public static void Bubblesort (int sortdata[]) {int I,j,temp;int len = sortdata.length;for (i=0;i< len-1;i++) {for (j=1;j<len-i;j++) {if (sortdata[j-1] > Sortdata[j]) {swap (SORTDATA,J-1,J);}}}

  

2. Select sort

(1) Each time the smallest element is selected from the remaining disorder, the exchange

Algorithm complexity: O (N2) not verbose, on the code:

    //Select Sort (from the rest of the selection the largest swap)     Public Static voidSelectionsort (intsortdata[]) {        inti,j,min,index,temp; intLen =sortdata.length;  for(i=0;i<len;i++) {min=Sortdata[i]; Index=i;  for(j=i+1;j<len;j++){                if(Sortdata[j] <min) {min=Sortdata[j]; Index=J;        }} swap (Sortdata,i,index); }    }

3. Insert Sort:

(1) The basic operation is to insert a data into the ordered data that has been sequenced:

Insert sort, move large back to public static void Insertsort (int sortdata[]) {int I,j,temp;int len = sortdata.length;for (i=1;i<len;i+ +) {temp = Sortdata[i];for (j = i-1;j>=0 && Temp < sortdata[j];j--) {sortdata[j+1] = sortdata[j];} SORTDATA[J+1] = temp;}}

  

3. Hill Sort:

is an improved version of the insert sort, mainly hill by introducing increments, which strengthens the power of the insertion sort. As the increment decreases, it is ordered.

    //Hill Sort     Public Static voidShellsort (intsortdata[]) {        inti,j,temp; intLen =sortdata.length; intGap = LEN/2;  while(gap>=1){             for(i=gap;i<len;i++) {Temp=Sortdata[i];  for(j=i-gap;j>=0&&temp<sortdata[j];j=j-Gap) {Sortdata[j+GAP] =Sortdata[j]; } sortdata[j+GAP] =temp; } Gap= GAP/2; }    }

4. Heap Sequencing

(1) Initial heap (2) adjust to the minimum heap or the maximum heap format (3), the root position and the last position to change position, then adjust.

public static void Heapsort (int sortData []) {buildmaxheap (SortData), for (int i=sortdata.length-1;i>=1;i--) {swap ( Sortdata,0,i); Maxheap (sortdata,i,0);}} public static void Buildmaxheap (int sortdata[]) {int half = sortdata.length/2;for (int i=half;i>=0;i--) {maxheap ( sortdata,sortdata.length,i);}}  public static void Maxheap (Int. sortdata[],int Heapsize,int index) {int left = 2*index+1;int right = 2*index+2;int largest = Index;if (Left < heapsize && Sortdata[left]>sortdata[index]) {largest = left;} if (Right < heapsize && Sortdata[right]>sortdata[index]) {largest = right;} if (Index! = largest) {swap (sortdata,index,largest); Maxheap (sortdata,heapsize,largest);}}

5. Quick sort:

In fact, it is the dichotomy, in general, the first position as the demarcation point, the small one put to the front of the demarcation point, large put to the back. The two parts are recursively recursive.

Quick sort public static void QuickSort (int sortdata[],int start,int end) {if (start >=end) return;int I=start,j=end,value = Sortdata[i];boolean flag = True;while (i!=j) {if (flag) {if (Value > Sortdata[j]) {swap (sortdata,i,j); flag=false;} else{j--;}} Else{if (Value<sortdata[i]) {swap (sortdata,i,j); flag=true;} else{i++;}}} Printvalue (SortData); QuickSort (sortdata,start,j-1); QuickSort (sortdata,i+1,end);}

  

All the code that can be run:

//Gjs Public classSort { Public Static voidPrintvalue (intsortdata[]) {         for(inti = 0;i<sortdata.length;i++) {System.out.print (Sortdata[i]+" ");    } System.out.println (); }         Public Static voidSwapint[] Data,intPOS1,intPos2) {        inttemp =DATA[POS1]; DATA[POS1]=Data[pos2]; DATA[POS2]=temp; }    //Bubble Sort (22 swap, plus one outer loop)     Public Static voidBubblesort (intsortdata[]) {        inti,j,temp; intLen =sortdata.length;  for(i=0;i<len-1;i++){             for(j=1;j<len-i;j++){                    if(Sortdata[j-1] >Sortdata[j]) {Swap (Sortdata,j-1, J); }            }        }    }            //Select Sort (from the rest of the selection the largest swap)     Public Static voidSelectionsort (intsortdata[]) {        inti,j,min,index,temp; intLen =sortdata.length;  for(i=0;i<len;i++) {min=Sortdata[i]; Index=i;  for(j=i+1;j<len;j++){                if(Sortdata[j] <min) {min=Sortdata[j]; Index=J;        }} swap (Sortdata,i,index); }    }        //Insert the sort and move the big backwards     Public Static voidInsertsort (intsortdata[]) {        inti,j,temp; intLen =sortdata.length;  for(i=1;i<len;i++) {Temp=Sortdata[i];  for(j = i-1;j>=0 && Temp < sortdata[j];j--) {sortdata[j+1] =Sortdata[j]; } sortdata[j+1] =temp; }    }        //Hill Sort     Public Static voidShellsort (intsortdata[]) {        inti,j,temp; intLen =sortdata.length; intGap = LEN/2;  while(gap>=1){             for(i=gap;i<len;i++) {Temp=Sortdata[i];  for(j=i-gap;j>=0&&temp<sortdata[j];j=j-Gap) {Sortdata[j+GAP] =Sortdata[j]; } sortdata[j+GAP] =temp; } Gap= GAP/2; }    }             Public Static voidHeapsort (intSortData [])        {buildmaxheap (sortData);  for(inti=sortdata.length-1;i>=1;i--) {swap (SortData,0, i); Maxheap (Sortdata,i,0); }    }         Public Static voidBuildmaxheap (intsortdata[]) {            intHalf = SORTDATA.LENGTH/2;  for(inti=half;i>=0;i--) {maxheap (sortdata,sortdata.length,i); }    }         Public Static voidMaxheap (intSortdata[],intHeapSize,intindex) {        intleft = 2*index+1; intright = 2*index+2; intlargest =index; if(Left < heapsize && sortdata[left]>Sortdata[index]) {Largest=Left ; }        if(Right < HeapSize && sortdata[right]>Sortdata[index]) {Largest=Right ; }        if(Index! =largest)            {swap (sortdata,index,largest);        Maxheap (sortdata,heapsize,largest); }        }        //Quick Sort     Public Static voidQuickSort (intSortdata[],intStartintend) {        if(Start >=end)return; intI=start,j=end,value =Sortdata[i]; BooleanFlag =true;  while(i!=j) {            if(flag) {if(Value >Sortdata[j])                    {swap (SORTDATA,I,J); Flag=false; }Else{J--; }            }Else{                if(value<Sortdata[i])                    {swap (SORTDATA,I,J); Flag=true; }Else{i++; }            }        }        //Printvalue (sortData);QuickSort (sortdata,start,j-1); QuickSort (Sortdata,i+1, end); }         Public Static voidMain (string[] args) {int[] a={2,4,1,5,8,9,43,3,7,8,9,0,3};        Printvalue (a); //Bubblesort (a); //Selectionsort (a); //Insertsort (a); //Heapsort (a); //Shellsort (a);QuickSort (a,0,a.length-1);            Printvalue (a); }}

Basic sorting algorithm, Java implementation (fast, bubbling, selection, heap sort, insert)

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.