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