A detailed explanation of the C language bubble sorting algorithm and the example _c language

Source: Internet
Author: User
Tags int size

C Language Bubble Sort algorithm

Bubble sort (Bubble sort) is a simple sort algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if their order is wrong. The task of visiting the series is repeated until no further exchange is needed, which means that the sequence has been sorted. The algorithm is named because the smaller elements float slowly through the exchange to the top of the sequence.

The number of O (N2) comparisons is required for n items by the bubble sort and can be sorted in situ. Although this algorithm is one of the simplest algorithms for understanding and implementation, it is inefficient for ordering sequences outside a few elements.

The bubble sort is the same execution time as the insert sort, but the two methods vary greatly in the number of times they need to be exchanged. In the worst-case scenario, the bubble sort requires an O (N2) Secondary exchange, while the insertion order is as long as the maximum O (n) is exchanged. The implementation of the bubble sort (like the following) typically performs poorly on the sorted array (O (N2)), and the insertion Order requires only O (n) operations in this example. So many modern algorithms textbooks avoid using bubble sort instead of insert sort. Bubble sort if the internal loop is executed for the first time, it is possible to use a flag to indicate that there is a need for exchange, or to reduce the best complexity to O (n). In this case, there is no need for an exchange in the sorted series. You can also slightly improve efficiency by reversing the order of visits and the size of the comparison when you visit a series. Sometimes called a round trip, because the algorithm shuttles from one end of the sequence to the other.

The process of using bubble sort to sort a column of numbers

The bubble sort algorithm works as follows:

    1. Compare the adjacent elements. If the first one is bigger than the second one, swap them both.
    2. Do the same work for each pair of adjacent elements, from the first pair to the end of the last couple. At this point, the final element should be the largest number.
    3. Repeat the above steps for all elements except the last one.
    4. Continue to repeat the previous steps for less and fewer elements until no pair of digits need to be compared.

Because of its simplicity, bubble sorting is often used to introduce the concept of algorithms to students getting started with programming.

The algorithm of bubble sort realizes many online, this paper adopts the C-language generic realization:

#include <stdio.h> #include <stdlib.h> void swap (void *vp1, void *vp2, int size) {char buffer[size]; 
   memcpy (buffer, VP1, size); 
   memcpy (VP1, VP2, size); 
memcpy (VP2, buffer, size); 
int cmp_int (const void *a, const void *b) {return * (int *) A-* (int *) b; 
int cmp_double (const void *a, const void *b) {return * (double *) a > * (double *) b 1:1;
  } void Bubblesort (void *base, int n, int elemsize, int (*cmp) (const void *, const void *)) {char *q = (char *) base;
  Char *p = (char *) base + n * elemsize;  while (p > Q) {for (; q!= p-elemsize; q + elemsize) {if (CMP (q, q + elemsize) > 0) {swap (q, q
      + elemsize, elemsize);
    } q = (char *) base;
  P-= elemsize;
  int main (void) {//test data int arr1[] = {5, 4, 1, 3, 6, 12, 8, 22, 34,76};
  Bubble sort Bubblesort (arr1, sizeof (int), cmp_int);
  Print sort result int i;
  for (i = 0; i < i++) printf ("%d", arr1[i]);
  printf ("\ n"); Double Arr2[] = {5.4, 4.8, 1.2, 3.4, 6.7, 12.12, 8.6, 22.12, 34.5, 76.3};
  Bubblesort (arr2, sizeof (double), cmp_double);
  for (i = 0; i < i++) printf ("%.2f", Arr2[i]);
printf ("\ n");

 }

The results of the operation are as follows:

The general implementation is as follows (C language):

#include <stdio.h>

void bubblesort (int arr[], int count)
{
  int i = count, J;
  int temp;
  while (i > 0) {for
    (j = 0; J < I-1; J +) {
      if (Arr[j] > arr[j + 1]) {  
        temp = arr[j];
        ARR[J] = arr[j + 1];
        Arr[j + 1] = temp;
      }
    }
    i--
  }
 
}
int main ()
{
  //test data
  int arr[] = {5, 4, 1, 3, 6};
  Bubble sort
  Bubblesort (arr, 5);
  Print sort results for
  (int i = 0; i < 5; i++)
    printf ("%4d", Arr[i]);


Use the bubble sort of flags

If the given column is basically orderly, a sign can be used to reduce unnecessary judgment and improve efficiency

void Bubblesort (int d[], int size)//assumes that the 22 interchange occurs at the last two locations of the array
{
  int exchange = size-1;
  while (Exchange) {
    //Where the data exchange occurs under the record
    int bound = Exchange;
    Exchange = 0;  There is no data exchange for this trip
    (int i = 0; i < bound i++) {
      if (D[i] > d[i + 1]) {
        swap (&d[i), &d[i+1]); 
   exchange = i}}}}






Thank you for reading, I hope to help you, thank you for your support for this site!

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.