Ordering of Java Data structures

Source: Internet
Author: User

1. Bubble Sort: Time complexity is O (n2)

The hypothesis is a small to large sort: a comparison between two adjacent numbers, and a larger number at the back. The largest number in the last row after a comparison

such as: 40, 8, 15, 18, 121 times after the order is: 8, 15, 18, 12, 40 in turn until from small to big pat good

 for (int i = 0;i < n-i; i++) {   for (int j = 0;j <n-1-i;j++) {//The last few are already lined up There is no need to sort       the if(A[j] > a[j+1]) {           k= a[j]           ; = A[j+1];           A[j+1] = k;}}  }

2. Select Sort: Time complexity O (n2)

The hypothesis is a sort of small to large: each time the smallest number in a sort order is placed first

such as: 3, 4, 1, 5, 21 times after sorting: 1, 4, 3, 5, 2

 for (int i=0;i<n-1;i++) {    for (int j=i+1;j<n;j++) {        if (a[i]>a[j]) {           k= a[i];            = A[j];            = k;}}    }    

3. Insert Sort:

The assumption is a small-to-large sort: each time a number is compared with the previous number, if it is less than the preceding number

 for (int I =1;i < n; i++) {    int j = i-1;     int k = a[i];      while (A[i] < a[j]&&j >= 0) {         a[j+1] = A[j];
j--; } = k;}

Ordering of Java Data structures

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.