
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to sort a given list of numbers in ascending order using Bubble sort
In C programming language, bubble sort is the simplest sorting technique and is also called as an exchange sort.
Procedure for bubble sort
Compare the first element with the remaining elements in the list and exchange(swap) them, if they are not in order.
Repeat the same for other elements in the list until all the elements gets sorted.
Algorithm
Given below is an algorithm to sort a given list of numbers in an ascending order by using the bubble sort technique −
Step 1 − Start
Step 2 − Take 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].
printlist(list,num)
1. for j =0 to num 2. write list[j].
bub_sort(list,num)
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
Following is the C program to sort a given list of numbers in an ascending order by using the bubble sort technique −
#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("
Enter the elements:
"); 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
"); scanf("%d",&num); readlist(list,num); printf("
Elements in the list before sorting are:
"); printlist(list,num); bub_sort(list,num); printf("
Elements in the list after sorting are:
"); printlist(list,num); }
Output
When the above program is executed, it produces the following result −
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
- Related Articles
- 8085 Program to perform bubble sort in ascending order
- Program to perform bubble sort in ascending order in 8085 Microprocessor
- Program to sort a given linked list into ascending order in python
- Golang Program To Sort An Array In Ascending Order Using Insertion Sort
- Java Program to Sort Array list in an Ascending Order
- C program to sort an array in an ascending order
- Bubble Sort program in C#
- 8085 Program to perform bubble sort in descending order
- Write a Golang program to sort an array using Bubble Sort
- C++ Program to Implement Bubble Sort
- C++ Program to Sort the Elements of an Array in Ascending Order
- Java program to sort words of sentence in ascending order
- 8085 Program to perform selection sort in ascending order
- Program to sort each diagonal elements in ascending order of a matrix in C++
- C program to sort an array of ten elements in an ascending order
