Implementation of bubble sorting C ++ and PHP in sorting algorithms

Source: Internet
Author: User
In the sort algorithm, C ++ and PHP implement the bubble sort, that is, the small elements are adjusted forward or the large elements are adjusted backward. The comparison is an adjacent comparison between two elements, and the exchange also occurs between these two elements. Is the slowest sorting algorithm. In practice, it is the most efficient algorithm.

Time complexity:

The worst time complexity is O (n ^ 2). The best time complexity of bubble sorting is O (n ^ 2), and the average time complexity is O (n ^ 2)

Algorithm principle:

1. compare adjacent elements. If the first is bigger than the second, exchange the two of them.

2. perform the same operation on each adjacent element, starting from the first pair to the last one. At this point, the final element should be the largest number.

3. repeat the preceding steps for all elements except the last one.

4. continue to repeat the above steps for fewer and fewer elements until there is no need to compare any number.

Algorithm stability:

Bubble sorting is to adjust small elements forward or large elements backward. The comparison is an adjacent comparison between two elements, and the exchange also occurs between these two elements. Therefore, if the two elements are equal, I think you will not be bored to exchange them. if the two equal elements are not adjacent, even if the two are adjacent through the previous two exchanges, at this time, the sequence of the same elements is not changed, so the bubble sorting is a stable sorting algorithm.


Php code:

 "; Bubble_sort ($ data, $ n); // Generate a random number consisting of a non-repeating array function createData ($ n, $ min, $ max) {$ bigarr = range ($ min, $ max); $ arrkey = array_rand ($ bigarr, $ n); // return key name for ($ I = 0; $ I
 ";}Echo" sorting result: "; print_r ($ data) ;}?>

Running result:



C ++ code:

# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; int createData (int arr [], int n, int min, int max); void bubble_sort (int * a, int n); int main () {int arr [100]; int n = 6; int min = 2; int max = 12; createData (arr, n, min, max); for (int I = 0; I
    
     
A [I]) {int tmp = a [I]; a [I] = a [i-1]; a [i-1] = tmp; flag = true ;}} k --;}}
    
   
  
 

Running result:

Related Article

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.