[6] Algorithm path-bidirectional bubbling sort of shaker

Source: Internet
Author: User

Shaker sequence – algorithm

1. bubbles are sorted in both directions, and the bubble sort is left-to-right first. Let's make the bubble sort move from right to left, so that a sort order is completed.

2. Use Left with the Right Two flags to record the position of the ordered elements on the left and right sides.

An example of a sort is seen below:

Before sorting: 45 19 77 81 13 28 18 1977 11

Sort right: 19 45 77 13 28 18 19 7711 [81]

Sort left: [11] 19 45 77 13 28 1819 77 [81]

Sort right: [11] 19 45 13 28 18 19[77 77 81]

Sort left: [11 13] 19 45 18 28 19[77 77 81]

Sort right: [11 13] 19 18 28 19 [4577 77 81]

Sort left: [11 13 18] 19 19 28 [4577 77 81]

Sort right: [11 13 18] 19 19 [28 4577 77 81]

Sort left: [11 13 18 19 19] [28 4577 77 81]

As you can see above, the brackets indicate that the left and right sides have been sorted, and when you > right. The sorting is complete.


Sourcecodes

int shakersort (int a[],int lens) {int left=0,right=lens-1,shift;int i;while (left<right) {for (i=left;i<right;i++ {if (a[i]>a[i+1]) {swaper (a[i],a[i+1]); shift=i;}} right = Shift;for (i=right-1;i>=left;i--) {if (A[i+1]<a[i]) {swaper (a[i],a[i+1]); shift=i+1;}} Left=shift;} return  0;}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

[6] Algorithm path-bidirectional bubbling sort of shaker

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.