C # Implementing bubbling Sorting

Source: Internet
Author: User

First, the principle of the algorithm

1, compare the adjacent elements. If the first one is bigger than the second one, swap them both.

2, for each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair. At this point, the last element should be the maximum number.

3. Repeat the above steps for all elements except the last one.

4. Repeat the above steps each time for less and fewer elements until there are no pairs of numbers to compare.

Second, the algorithm analysis

Average time complexity: The best bubble sort is O (n), the worst is O (n²), and the average time complexity is O (n²)

Space complexity: O (1) (for Exchange)

Third, algorithm stability

The bubble sort is to move the small element forward or the large element back. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements. So, if the two elements are equal, I think you will not be bored to exchange them again, if the two equal elements are not adjacent, then even through the preceding 22 exchange two adjacent together, this time will not be exchanged, so the same elements of the order has not changed, so bubble sort is a stable sorting algorithm.

Four, bubble sort has two obvious advantages

1. "Programming complexity" is very low, it is easy to write code;

2. Stability, where the relative order of the same elements in the original sequence is still maintained to the ordered sequence, and the heap sorting, fast ordering are not stable.

Five, C # bubble sorting algorithm

 C # code replication
Bubble sort
void Bubblesort (int array[],int N)
{int i=0;     int j=0;    int temp=0;    int flag = 0; for (i=0;i<n-1; i++) /* the total number of round-robin control sorts */{flag = 0; /* before this sequencing begins, the swap flag should be false */for (J=n-1;j > i;j--) /* inside loop control a trip sort of proceed */{if (Array[j] < array[j-1]) /* adjacent elements to compare, if reverse exchange */{temp =array[j]; Array[j] = Array[j-1]; ARRAY[J-1] = temp; flag = 1; /* has exchanged, so the Exchange flag is set to True */}} if (flag = = 0) /* This tour sort has not occurred interchange, early termination algorithm */break; /* printf (" Order%d results: \\n ", i+1); PrintArray (Array,n); */ }}

Application of the algorithm

 C # code replication
Print array
void PrintArray (int  array[], int n)
{  int i;  for (i=0;i<n;i++)   printf ("%d", Array[i]);  printf ("\\n");} void Testbubblesort (){    int array[8] ={38,20,46,38,74,91,12,25};    Bubblesort (array,8);    PrintArray (array,8);} 

Output form

1th, 2nd, 3rd, 4th, 5th, 6th, 7th.

12 12 12 12 12 12 12

38 20 20 20 20 20 20

20 38 25 25 25 25 25

46 25 38 38 38 38 38

38 46 38 38 38 38 38

74 38 46 46 46 46 46

91 74 74 74 74 74 74

25 91 91 91 91 91 91

C # Implementing bubbling Sorting

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.