Professional Documents
Culture Documents
Lab Report
Course No: CSE-282
Course Title: Data structure and algorithm sessional.
Experiment No: 07
Date of Experiment: 01-05-19
Date of Submission: 08-05-19
Submitted by Submitted to
Name: Ahatesham Bhuiyan
ID: 1708002
Batch: 17
Level: 2 Tahmina Khanam
Lecturer, dept. of CSE
Term: 1
Dept. of ETE
Objective:
To get familiar with Bubble Sort in c programming.
Required software:
▪ Windows 10
▪ Codeblocks
Problem:
Write a C program to implement Bubble sort .
Descriptiption:
Bubble sort is a simple algorithm which is used to sort a given set of n elements
provided in form of an array with n number of elements. Bubble Sort compares
all the element one by one and sort them based on their values.
Steps:
➢ Starting with the first element(index = 0), compare the current element
with the next element of the array.
➢ If the current element is greater than the next element of the array, swap
them.
➢ If the current element is less than the next element, move to the next
element.
➢ Repeat Step 1.
Source Code:
#include<stdio.h>
int main(){
int n, temp, i, j, number[30];
printf("Enter numbers : ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&number[i]);
for(i=n-2;i>=0;i--){
for(j=0;j<=i;j++){
if(number[j]>number[j+1]){
temp=number[j];
number[j]=number[j+1];
number[j+1]=temp;
}
}
}
return 0;
}
Sample input & output:
Discussion:
In this programming, an array is used to get input data elements.To implement
bubble sort , for loop is used for logical expression.If we take n elements, then n-
1 pass will be needed to implement sort. Sorting can be expressed in both
ascending or descending order.C is case sensetive language, so syntax must be
taken properly.