C/C ++ algorithm learning notes (7)-bidirectional bubbling

Source: Internet
Author: User

Original address: bidirectional bubbling


Generally, the bubble is unidirectional, and here it is bidirectional, that is, reverse work is required.

CodeIt looks complicated. After careful consideration, we can see that it is a method of round-trip fluctuation.

The author of this Code thinks this can reduce some exchanges on the basis of bubbling (I don't think so, maybe I am wrong ).

I think this is an interesting piece of code.

 
# Include <iostream. h> void bubble2sort (int * pdata, int count) {int itemp; int left = 1; int right = count-1; int T; do {// positive part for (INT I = right; I> = left; I --) {If (pdata [I] <pdata [I-1]) {itemp = pdata [I]; pdata [I] = pdata [I-1]; pdata [I-1] = itemp; t = I ;}} left = t + 1; // reverse part for (I = left; I <right + 1; I ++) {If (pdata [I] <pdata [I-1]) {itemp = pdata [I]; pdata [I] = pdata [I-1]; pdata [I-1] = itemp; t = I ;}} right = T-1 ;} while (left <= right);} void main () {int data [] = {10, 9, 8, 7, 6, 5, 4}; bubble2sort (data, 7 ); for (INT I = 0; I <7; I ++) cout <data [I] <""; cout <"/N ";}

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.