Sort--1

Source: Internet
Author: User

Sort, today only looked at bubbling, inserting, fast three sort algorithms for three groups of data {3,15,7,20,9,3,12,17} respectively; {3,5,7,9,15,23,30,45}; {45,30,23,15,9,7,5,3}; processing.

# #插入排序

 Packagesort; Public classInsertsort { Public Static voidMain (string[] args) {LongBegin =System.nanotime (); int[] arr={3,15,7,20,9,3,12,17};  for(inti=1;i<arr.length;i++){            intt=i;  for(intj=i-1;j>=0;j--){                if(arr[t]<=Arr[j]) {                    inttemp=Arr[t]; Arr[t]=Arr[j]; ARR[J]=temp; T--; }                Else                     Break; }        }        LongEnd =System.nanotime ();        OutPut (arr); System.out.print ("\ n"); System.out.print (End-begin) + "ns"); }    Private Static voidOutPut (int[] a) {         for(inti:a) System.out.print (i+ "\ T"); }}

Insert sort processing time results for three groups of data:

# #冒泡排序

 Packagesort; Public classBubblesort { Public Static voidMain (string[] args) {LongBegin =System.nanotime (); int[] arr={3,15,7,20,9,3,12,17};        Bubblesort (arr); LongEnd =System.nanotime ();        OutPut (arr); System.out.print ("\ n"); System.out.print (End-begin) + "ns"); }     Public Static voidBubblesort (int[] a) {         for(inti=0;i<a.length;i++)             for(intj=i;j<a.length;j++)                if(a[i]>A[j]) {                inttemp=A[i]; A[i]=A[j]; A[J]=temp; }    }        Private Static voidOutPut (int[] a) {         for(inti:a) System.out.print (i+ "\ T"); }}

The result of the processing time of the bubble sort on three groups of data:

# #快速排序

 Packagesort; Public classQuickSort { Public Static voidMain (string[] args) {int[] arr={3,15,7,20,9,3,12,17}; LongBegin =System.nanotime (); Quicksort (arr,0,arr.length-1); LongEnd =System.nanotime ();        OutPut (arr); System.out.print ("\ n"); System.out.print (End-begin) + "ns"); }        Private Static voidQuicksortint[] A,intLeftintRight ) {        intDP; if(left<Right ) {DP=partition (A,left,right); Quicksort (A,LEFT,DP-1); Quicksort (A,DP+1, right); }    }    Private Static intPartitionint[] A,intLeftintRight ) {        intpivot=A[left];  while(left<Right ) {             while(left<right&&a[right]>=pivot) right--; if(left<Right ) A[left++]=A[right];  while(left<right&&a[left]<=pivot) left++; if(left<Right ) A[right--]=A[left]; } A[left]=pivot; returnLeft ; }        Private Static voidOutPut (int[] a) {         for(inti:a) System.out.print (i+ "\ T"); }}

Quick Order Processing time results for three groups of data:

Comparison of time complexity:

Sort--1

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.