"Two-way bubble sort" of OJ brush problem

Source: Internet
Author: User

Title DescriptionNote: Only need to submit the code to fill in the section, please followC + +Language method submission.

bidirectional bubbling from small to large sort algorithm Description:
(1) from the current sequence.1element, the adjacent elements are compared to the previous 22, and the conditions (from small to large) are exchanged, until the end of the sequence. At this point finally1the maximum value of the element.
(2) from the penultimate part of the current sequence2element, the adjacent elements are compared from forward 22, and the conditions are exchanged, until the sequence begins. This section1element is the minimum value.
(3) The first2element as the beginning of a new sequence, the countdown2elements as the end of a new sequence, repeating (1) ~ (2) until the new sequence has no elements.
improved bidirectional bubbling from small to large sort algorithm Description:
(a) in the above algorithm (1) step, recording each Exchange position, making Highindicates the last1Secondary Exchange Location,If the comparison process does not occur, the algorithm ends;
(b) in the algorithm section (2) step, you only need to HighCompare, record each exchange position in the comparison process, make Lowindicates the last1The second Exchange position, if the comparison process does not occur, then the algorithm ends;
(C) in the algorithm section (3) step to make the new sequence the start position Low, the end position is High, repeat (a) ~ (b) until the new sequence has no elements.

#include <iostream>
using namespace Std;
int main ()
{
int a[100],i,n;
cin>>n;
for (i=0; i<n; i++)
Cin>> A[i];
int low, high,lastswappos,temp,cnt;
Low = 0;
High = n-1;
while (Low < high)
{
Lastswappos = high; //sets the last element position of an unordered sequence
for (I=low; i<lastswappos; i++)
{
cnt++;
if (a[i]>a[i+1])
{
temp = A[i];
A[i] = a[i+1];
A[I+1] = temp;
High = i; //Record Interchange Location
}
}
if (Lastswappos = = high)//If the swap operation is not performed, the ordering is complete
break;

        Lastswappos = low;
       /*
         Please fill in the missing code in this section
         */
        if (Lastswappos = = low)//
             break;
   }

for (i = 0; i<n; i++)
cout<<a[i]<< "";
cout<<endl;
return 0;
}

Input

N and the N an integer

Output

From small to large sequence

Sample input
6
21 45 85 47 3 15
Sample output
3 15 21 45 47 85

The code is as follows:

#include <iostream>using namespace Std;int main () {int a[100],i,n;    cin>>n;    for (i=0; i<n; i++) cin>> a[i];    int low, high,lastswappos,temp,cnt;    Low = 0;    High = n-1;        while (Low < high) {Lastswappos = high;//sets the last element position of the unordered sequence for (i=low; i<lastswappos; i++)            {cnt++;                if (a[i]>a[i+1]) {temp = A[i];                A[i] = a[i+1];                A[I+1] = temp;       High = i;        Record interchange Position}} if (Lastswappos = = high)//If no swap operation is performed, the sorting is completed by break; Lastswappos = low;                Sets the first element position of the unordered sequence for (I=high; i>lastswappos; i--) {if (A[i]<a[i-1]) {                temp = A[i];                A[i] = a[i-1];                A[I-1] = temp;       low = i;    Record interchange Position}} if (Lastswappos = = low)//If no swap operation is performed, the sorting has finished break; } for (i = 0; i<N    i++) cout<<a[i]<< "";    cout<<endl; return 0;}

Operation Result:



The morning up on the project has been plagued by more than half an hour, no matter how to modify the need to add the code is not the correct results, with a single-step debugging to suddenly find the problem ... Originally I changed the wrong place at some time before, the original code in the loop to change ... Crying drunk ...

What does not understand is that CNT has what function, did not initialize, also appeared the cnt++, the later supplementary Code also did not use to it




"Two-way bubble sort" of OJ brush problem

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.