Bubble sort algorithm example in C# and C (Ascending and Descending order)

Lionsure 2020-07-06 Original by the website

It is often necessary to sort arrays and lists(List, IList, etc.) in the process of writing programs. There are many methods for sorting, and bubble sort is one of them. The reason why it is called bubble sort is because the sorting process is very similar to bubbling. After reading the basic idea of the bubble sort algorithm, it is easier to understand.

 

1. The basic idea of bubble sort algorithm

The first loop selects the smallest number from the array(or list) to the first place; the second loop selects the smallest number from the remaining elements of array to the second place; and so on, sort all the array elements directly, the whole process is very much like bubbling.

 

2. Bubble sort algorithm example C# (Ascending order)

/// <summary>
       /// Bubble sort
       /// </summary>
       /// <param name="arr">Array to be sorted</param>

       public void BubbleSort(int[] arr)
       {
              int temp;
              for (int i = 0; i < arr.Length - 1; i++)
              {
                     for (int j = 0; j < arr.Length - 1 - i; j++)
                     {
                            if (arr[j] > arr[j + 1])
                            {
                                   temp = arr[j];
                                   arr[j] = arr[j + 1];
                                   arr[j + 1] = temp;
                            }
                     }
              }
       }

Call:

int[] a = new int[] { 36, 7, 120, 89, 3, 56, 23, 45 };
       BubbleSort(a);

foreach (int n in a)
       label.Text += n.ToString() + " ";

Output result: 3 7 23 36 45 56 89 120

 

 

3. Bubble sort algorithm example C# (Descending order)

The descending order is basically the same as the ascending order, except that changed > to < in the "if (arr[j] > arr[j + 1])", the code is as follows:

/// <summary>
       /// Array to be sorted
       /// </summary>
       /// <param name="arr">Array to be sorted</param>

       public void BubbleSort(int[] arr)
       {
              int temp;
              for (int i = 0; i < arr.Length - 1; i++)
              {
                     for (int j = 0; j < arr.Length - 1 - i; j++)
                     {
                            if (arr[j] < arr[j + 1])
                            {
                                   temp = arr[j];
                                   arr[j] = arr[j + 1];
                                   arr[j + 1] = temp;
                            }
                     }
              }
       }

Call:

int[] a = new int[] { 36, 7, 120, 89, 3, 56, 23, 45 };
       BubbleSort(a);

foreach (int n in a)
       label.Text += n.ToString() + " ";

Output result: 120 89 56 45 36 23 7 3

 

 

4. Bubble sort algorithm in C with example (Ascending order)

#include <stdio.h>
       #include <stdlib.h>
       #include <string.h>

void bubblesort(int a[], int n)
       {
              int i,j,temp;
              for (i = 0; i < n - 1; i++)
              {
                     for (j = 0; j < n - 1 - i; j++)
                     {
                            if (a[j] > a[j + 1])
                            {
                                   temp = a[j];
                                   a[j] = a[j+1];
                                   a[j+1] = temp;
                            }
                     }
              }

}

void main ()
       {
              int a[8] = {36, 7, 120, 89, 3, 56, 23, 45};
              bubblesort(a, 8);

       int i;
              for (i = 0; i < 8; i++)
                     printf("%d,",a[i]);
              printf("\n");
       }

Output result: 3,7,23,36,45,56,89,120

 

 

5. Bubble sort algorithm in C with example (Descending order)

#include <stdio.h>
       #include <stdlib.h>
       #include <string.h>

void bubblesort(int a[], int n)
       {
              int i,j,temp;
              for (i = 0; i < n - 1; i++)
              {
                     for (j = 0; j < n - 1 - i; j++)
                     {
                            if (a[j] < a[j + 1])
                            {
                                   temp = a[j];
                                   a[j] = a[j+1];
                                   a[j+1] = temp;
                            }
                     }
              }

}

void main ()
       {
              int a[8] = {36, 7, 120, 89, 3, 56, 23, 45};
              bubblesort(a, 8);

       int i;
              for (i = 0; i < 8; i++)
                     printf("%d,",a[i]);
              printf("\n");
       }

Output result: 120,89,56,45,36,23,7,3