Insert Sort, merge sort, heap sort, and quick sort

Source: Internet
Author: User

1*Insert Sort2*time Complexity O (n2)3*@param array in-situ sorting algorithm4*/5  Public voidInsertsort (int[] Array) {6      for(inti = 1; i < Array.Length; i++) {7       intPresent =Array[i];8       intPosition =i;9        while(Position > 0 &;&; array[position-1] > present) {//Move RightTenArray[position] = array[position-1]; Oneposition--; A         } -Array[position] =present; -     } the } -  -   -  + /** -  + * Merge Sort A * O (NLOGN) at * @paramArray - * @paramLeft First Index - * @paramRight the last index - */ -  Public voidMergeSort (int[]array,intLeftintRight ) { -     if(left<Right ) { in       intMiddle= (left+right)/2; -       mergesort (array,left,middle); toMergeSort (array,middle+1, right); +       merge (array,left,middle,right); -     } the } *  $  Public voidMergeint[]array,intLeftintMiddle,intRight ) {Panax Notoginseng     int[] array1=New int[Middle-left+1]; -     int[] array2=New int[right-Middle]; the      for(inti=0;i<array1.length;i++){ +array1[i]=array[left+i]; A     } the      for(inti=0;i<array2.length;i++){ +Array2[i]=array[middle+i+1]; -     } $     intl=0,r=0,k=Left ; $      for(; k<=right&;&;l<array1.length&;&;r<array2.length;k++){ -       if(array1[l]>Array2[r]) { -array[k]=Array2[r]; ther++; -}Else {Wuyiarray[k]=Array1[l]; thel++; -       } Wu     } -      while(l<array1.length) { Aboutarray[k]=Array1[l]; $l++; -k++; -       } -  while(r<array2.length) { Aarray[k]=Array2[r]; +r++; thek++; -      } $ } the  the   the  the /** - * Heap Sorting in * In-situ sequencing and O (NLOGN) the * @paramArray the */ About  Public voidHeapsort (int[] Array) { the     buildheap (array); the      for(inti=array.length-1;i>0;i--){ the       intK=array[0]; +array[0]=Array[i]; -array[i]=K; theHeapify (Array, 0, i);Bayi     } the } the /** - * Build Maximum heap - * @paramArray the */ the  Public voidBuildheap (int[] Array) { the      for(inti=array.length/2-1;i>-1;i--){ the       heapify (array,i,array.length); -     } the } the  the /**94 *  the * @paramArray Arrays the * @paramindex in the index array the * @paramnumber of elements in the length tree98 */ About  Public voidHeapify (int[] Array,intIndexintlength) { -     intPresent=index;//Current Index101     intValue=Array[index];102     intlargest=Array[index];103     intlargest_index=index;104      while((2*present+1) <length) {//judge whether there is a son the       if(array[2*present+1]>largest) {106Largest=array[2*present+1];107Largest_index=2*present+1;108     }109     if((2*present+2) <length&;&;array[2*present+2]>largest) { theLargest=array[2*present+2];111Largest_index=2*present+2; the     }113     if(largest_index!=present) { thearray[present]=largest; thePresent=Largest_index; thelargest=value;117}Else{118        Break;119     } -     }121array[present]=value;122}123 124   the 126 /**127 * Worst time O (N2)----occurs when the array is already sorted - * O (NLOGN)129 * @paramArray the * @paramP131 * @paramR the */133  Public voidQuickSort (int[]array,intPintR) {134     if(p<R) {135       intq=partition (array,p,r);136QuickSort (array,p,q-1);137QuickSort (array,q+1, R);138     }139 } $ 141  Public intPartitionint[]array,intPintR) {142Random random=NewRandom ();143Exchange (Array,r,random.nextint (r-p+1) +p);//Random Fetch number144     intx=Array[r];145     intI=p-1;146      for(intj=p;j<r;j++){147       if(array[j]<=x) {148I=i+1;149       Exchange (ARRAY,I,J); Max     }151 } theExchange (array,i+1, R);153     returnI+1;154 }155 156  Public voidExchangeint[]array,intPintq) {157     intk=Array[p];158array[p]=Array[q];159array[q]=K; the}

Insert Sort, merge sort, heap sort, and quick sort

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.