Implementation and simple analysis of several sort algorithms in Java _java

Source: Internet
Author: User

This example describes the implementation and simple analysis of several Java sorting algorithms. Share to everyone for your reference. as follows:

Package test; public Class A/* normal insert sort/public void Insertsort (int[] list) {int i, j; list[0] =-999;//= set up a surveillance sentry, don't judge if it crosses the line.
However, the array is required to start storing for (i = 1; i < list.length; i++) {j = i, while (List[j] < list[j-1], starting with the second number (I=1 = list[j);
LIST[J] = list[j-1];
LIST[J-1] = temp;
j = j-1; }}/* Binary INSERT, on a direct insert basis, add two fork Lookup/public void Bininsertsort (int[] r, int low, int high) {for (int i = low + 1; I <= high ;  i++) {int temp = R[i];//save to insert element int hi = i-1 int lo = low;//Set initial interval while (lo <= hi) {//binary determine insert position int mid = (lo
+ hi)/2;
if (temp < R[mid]) Hi = mid-1;
else lo = mid + 1; for (int j = i-1 j > Hi; j--) r[j + 1] = R[j]; Move element R[hi + 1] = temp; Insert Element}/* Hill sort or shell/public void Shellsort (int[] r, int low, int high, int[] delta) {for (int k=0;k<delta.length
k++) Shellinsert (R, Low, High, delta[k]); private void Shellinsert (int[] r, int low, int high, int deltak) {for (int i=low+deltak; i<=high; i++) if (r[i]<r[ I-deltAK]) {int temp = r[i]; int j = I-deltak; for (; j>=low&&temp<r[j]; j=j-deltak) R[j+deltak] = r[j]; R[j+deltak
] = temp; 
}/* Simple select exchange/public void Selectsort (int[] r, int low, int high) {for (int k = low; k < high-1; k++) {//Make n-1 trip selection
int min = k;
for (int i = min + 1; I <= i++)//Select the element with the smallest keyword if (R[i] < r[min]) min = i; if (k!= min) {int temp = r[k];///keywords smallest element with element R[k] Exchange R[k] = R[min]; r[min] = temp;} End of If}}/* Heap sort-large top heap/public void Heapsort (int[] r) {int n = r.length-1; for (int i=n/2; i>=1; i--) Heapadjust
(R,i,n);
for (int i=n; i>1; i--) {int temp = r[1]; r[1] = R[i]; r[i] = temp; Heapadjust (r,1,i-1);}} Adjust heap private void Heapadjust (int[] r, int low, int high) {int temp = R[low]; for (int j = 2 * low; J <= High; j = J * 2) {if (J < high && R[j] < R[j + 1]) J + +: if (temp > R[j]) break, r[low] = r[j]; low = j;} R[low] = tem
P public static void Main (string[] args) {Alpha fs = new (); int[] A = {100, 9,8, 9, 9, 7, 7, 0, 0, 99, 55, 7, 6, 5, 4, 3, 2, 1};
Int[] k={5,3,1};
Fs.insertsort (a);
Fs.bininsertsort (A, 0, a.length-1);
Fs.shellsort (A, 0,a.length-1,k);
Fs.selectsort (A, 0, a.length-1);
Fs.heapsort (a);
for (int i = 0; i < a.length i++) {System.out.println (a[i]);} }

The sorting method of inserting sort, exchanging sort, choosing sort, merging sort, all have a common characteristic, that is, they are to determine the relative position between elements by comparing the size of elements, that is, the sorting method above is based on the comparison. Below, we compare and summarize the sorting method based on comparison.
We mainly compare the algorithm's average time complexity, the worst time complexity, the space complexity and the stability of the order.

The stability of the space complexity of the worst time complexity of the average time complexity of the ranking method
Direct Insert sort 0 (n2) 0 (n2) 0 (1) stable
Foaming sort 0 (n2) 0 (n2) 0 (1) stable
Quick Sort 0 (n log n) 0 (n2) 0 (log n) unstable
Simple choice of sort 0 (n2) 0 (n2) 0 (1) unstable
Heap sort 0 (n log n) 0 (n log n) 0 (1) unstable
Merge sort 0 (n log n) 0 (n log n) 0 (n) stable

In terms of time performance, fast sorting is the best real performance in all sorting algorithms, whereas fast sorting is inferior to heap sorting and merge sorting in the worst-case scenario. This can be avoided by improving the quick sort, a random, fast ordering of the pivot elements randomly, which makes the odds of a worst-case scenario very small and can be considered non-existent in practical use. In the comparison of heap sort and merge sort, when n is large, the time required for merge ordering is less, however, it needs more auxiliary storage space.

Judging from the stability of the method, most of the time complexity of 0 (N2) is a stable sorting method, in addition to the simple selection of sorting. But most of the time performance of the ranking methods, such as fast sorting, heap sorting, hill sort are unstable. In general, the comparison in the sort process is a stable method of sorting between adjacent two elements.

Furthermore, the stability of the sorting method is determined by the method itself, and an unstable instance can always be found for the unstable sorting method regardless of its descriptive form.

To sum up, none of the sorting methods discussed above are absolutely optimal, and in the actual use process, the appropriate sorting method should be chosen according to different circumstances.

I hope this article will help you with your Java programming.

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.