Multiple sorting algorithms

Source: Internet
Author: User

Insert Sort

There is already a well-ordered sequence in which to place the new element in the proper position of the sequence.

int *insertsorta (int *r,n) {
int i,j; for (j=1; j<n;j++) {i=j-1; TMP=r[j]; while (i>=0&&R[i]<tmp) { r[i+1]=r[i]; I--; } R[i+1]=tmp; }
return R;}

Each of the above algorithms to determine whether I is less than 0, you can let the first element of the array is a very small value, so that the r[0]<min{ri},i is 1 to n, the entire array length is n+1. When judging r[i]<tmp, it will end, because r[0] must be less than r[i].

int *INSERTSORTB (int *r,n) {  int  i,j;      for (j=2; j++;j<=N) {i=j-1; TMP=r[j];  while    (r[i]<tmp) {r[i+1]=r[i];    I--;   } r[i+1]=tmp; }
return R;}


Shell sort

The main idea of the shell is that the elements are grouped by a certain increment of the subscript, using the direct insert sort for each group. As the increments gradually decrease, each group contains more and more records. When the increment value is reduced to 1 o'clock, the entire file is split into a group, and the algorithm terminates.

Multiple sorting algorithms

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.