Bubble sort (Bubble Method)

Source: Internet
Author: User
The simplest sorting method is the Bubble sorting method. The basic idea of this method is to regard the elements to be sorted as vertical "Bubbles", and the smaller elements are relatively light, so as to move up and down. In the Bubble sorting algorithm, we need to process the "bubble" sequence several times. The so-called one-time processing is to check the sequence from the bottom up, and always pay attention to whether the order of two adjacent elements is correct. If the order of two adjacent elements is incorrect, that is, the "light" elements are located below, and their positions are exchanged. Obviously, after processing it again, the "lightest" element floated to the highest position; after processing it twice, the "minor" element floated to the lower position. During the second processing, you do not have to check because the element at the highest position is already the lightest element. In general, the I-th processing does not have to check the elements above the I-th high position, because they are sorted correctly after the processing of the previous I-1 times. This algorithm can be implemented as follows. # Include "stdafx. H"
# Include "iostream. H"

Template <class T>
Class doit ...{
PRIVATE:
Int X, Y;
T temp;
Public:
Doit (T * In, int count)
...{
For (y = 0; y <count-1; y ++)
...{
For (x = 1; x <count-y; X ++)
...{
If (* (in + x)> (* (in + x-1 )))
...{
Temp = (* (in + x-1 ));
(* (In + x-1) = (* (in + x ));
(* (In + x) = temp;
}
}
}
}
};

Int main ()
...{
Double A [4] =... {1.1, 1.3, 1.9, 2.2 };
Doit <double> D (A, 4 );
For (INT I = 0; I <4; I ++)
...{
Cout <A [I] <Endl;
}
Return 0;
}

 

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.