Home  >  Article  >  Backend Development  >  C program to sort a given list of numbers in ascending order using bubble sort algorithm

C program to sort a given list of numbers in ascending order using bubble sort algorithm

王林
王林forward
2023-09-23 13:01:021120browse

C program to sort a given list of numbers in ascending order using bubble sort algorithm

In the C programming language, bubble sort is the simplest sorting technique, also known as exchange sort.

Bubble sort process

  • Compares the first element with the rest of the elements in the list and swaps (swaps) them if they are not in order.

  • Repeat the same list of operations for other elements in the list until all elements are sorted.

Algorithm

Given below is an algorithm by using bubble sorting technique -

Step 1 - Start

Step 2 - Get list (array), num

Step 3− readlist(list,num)

Step 4− printlist(list,num)

Step 5 - bub_sort(list,num)

Step 6 - printlist(list,num)

readlist (list, num)

Step 7 − Stop

1. for j = 0 to num
2. read list[j].

Print list(list, number)

1. for j =0 to num
2. write list[j].

bub_sort(list, number)

1. for i = 0 to num
2. for j =0 to (num – i)
3. if( list[j] > list[j+1])
4. swapList( address of list[j], address of list[j+1])

swapList(address of list[j], address of list[j 1])

1. temp = value at list[j]
2. value at list[j] = value at list[j+1]
3. value at list[j+1] = temp

Example

The following is the use of C program to sort a given list of numbers in ascending order using bubble sorting technique

Demonstration

#include <stdio.h>
#define MAX 10
void swapList(int *m,int *n){
   int temp;
   temp = *m;
   *m = *n;
   *n = temp;
}
/* Function for Bubble Sort */
void bub_sort(int list[], int n){
   int i,j;
   for(i=0;i<(n-1);i++)
      for(j=0;j<(n-(i+1));j++)
         if(list[j] > list[j+1])
            swapList(&list[j],&list[j+1]);
   }
   void readlist(int list[],int n){
   int j;
   printf("</p><p>Enter the elements: </p><p>");
   for(j=0;j<n;j++)
      scanf("%d",&list[j]);
   }
   /* Showing the contents of the list */
   void printlist(int list[],int n){
      int j;
   for(j=0;j<n;j++)
      printf("%d\t",list[j]);
}
void main(){
   int list[MAX], num;
   printf(" Enter the number of elements </p><p>");
   scanf("%d",&num);
   readlist(list,num);
   printf("</p><p></p><p>Elements in the list before sorting are:</p><p>");
   printlist(list,num);
   bub_sort(list,num);
   printf("</p><p></p><p>Elements in the list after sorting are:</p><p>");
   printlist(list,num);
}

Output

##When executing the above program, the following results will be produced-

Enter the number of elements
10

Enter the elements:
11
23
45
1
3

6
35
69
10
22


Elements in the list before sorting are:
11 23 45 1 3 6 35 69 10 22

Elements in the list after sorting are:
1 3 6 10 11 22 23 35 45 69

The above is the detailed content of C program to sort a given list of numbers in ascending order using bubble sort algorithm. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete