0% found this document useful (0 votes)
50 views10 pages

Department of Electrical Engineering: Riphah International University, Islamabad, Pakistan

The document is a lab report from the Department of Electrical Engineering at Riphah International University. It details 5 tasks completed as part of an experiment on modeling bubble sort algorithms using C programs. The tasks include implementing bubble sort and optimized bubble sort to sort arrays of random numbers, applying bubble sort to additional arrays, improving bubble sort to reduce unnecessary swaps, implementing a standard bubble sort approach, and exploring a recursive version of bubble sort. Code solutions are provided for each task.

Uploaded by

Noman Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views10 pages

Department of Electrical Engineering: Riphah International University, Islamabad, Pakistan

The document is a lab report from the Department of Electrical Engineering at Riphah International University. It details 5 tasks completed as part of an experiment on modeling bubble sort algorithms using C programs. The tasks include implementing bubble sort and optimized bubble sort to sort arrays of random numbers, applying bubble sort to additional arrays, improving bubble sort to reduce unnecessary swaps, implementing a standard bubble sort approach, and exploring a recursive version of bubble sort. Code solutions are provided for each task.

Uploaded by

Noman Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Department of Electrical Engineering

Riphah International University, Islamabad, Pakistan

Program: B.Sc. Electrical Engineering


Semester: III A
Subject: SEL-201 Data Structure & Algorithm Date: …………….
Experiment 5: Model bubble sort algorithm by executing C programs. CLO3 (P2)

OBJECTIVES:

To implement the bubble sorting algorithm and Optimized bubble sort algorithm in C language.

Name: Muhammad Noman Roll No: 12575

Performance Lab Report

Description Total Marks Description Total Marks


Marks Obtained Marks Obtained
Ability to conduct 5 Organization/Structure 5
Experiment &
Tool usage
Implementation 5 5
and Results Data Presentation
Total Marks obtained

Remarks (if any): ………………………………….

Name & Signature of faculty: …………………………………


Introduction
In this lab experiment we learnt the sorting algorithm that replace the random values of array in a
sorting manner from less value to greater values from left to right respectively.

Objectives
To implement the bubble sorting algorithm and Optimized bubble sort algorithm in C language.

Lab tasks

Lab task 01
Rearrange the following numbers using Bubble sort. 42, 12, 18, 98, 67, 83, 8, 10, 71
Solution :
#include <stdio.h>
void bubbleSort(int arr[], int n)
{
int i, j, temp, flag=0;
for(i = 0; i < n; i++)
{
for(j = 0; j < n-i-1; j++)
{
// introducing a flag to monitor swapping
if( arr[j] > arr[j+1])
{
// swap the elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
// if swapping happens update flag to 1
flag = 1;
}
}
// if value of flag is zero after all the iterations of inner loop
// then break out
if(flag==0)
{
break;
}
}
// print the sorted array
printf("Sorted Array: ");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[]={42, 12, 18, 98, 67, 83, 8, 10, 71};

bubbleSort(arr, 9);

printf("\n");
return 0;
}
Lab task 02
Apply the Bubble sort on the following elements 21,11,5,78,49, 54,72,88
Solution
#include <stdio.h>
void bubbleSort(int arr[], int n)
{
n=8;
int i, j, temp;
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8-i-1; j++)
{
if( arr[j] > arr[j+1])
{
// swap the elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
// print the sorted array
printf("Sorted Array: ");
for(i = 0; i < 8; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[]={21,11,5,78,49, 54,72,88};

// call the function bubbleSort


bubbleSort(arr, 8);
return 0; }
___________________________________________________________________________

Lab task 03
Bubble sort First Improvement We could avoid so many swaps.
template
void bubble( Type *const array, int const n )
{
for ( int i = n - 1; i > 0; --i )
{
Type max = array[0]; // assume a[0] is the max
for ( int j = 1; j <= i; ++j )
{
if ( array[j] < max )
{
array[j - 1] = array[j]; // move
}
else { array[j – 1] = max; // store the old max
max = array[j]; // get the new max
}
array[i] = max; // store the max
}
Solution :
The correct program :
#include <stdio.h>
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void bubble (int a[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (a[j] > a[j+1])
swap(&a[j], &a[j+1]);
}
void print (int a[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d \n",a[i]);
}

int main() {
int a[] = {4, 8, 1, 3, 10, 9, 2, 11, 5, 6};
bubble(a, 10);

//printing array
int i;
for(i=0; i<10; i++) {
printf("%d ",a[i]);
}
printf("\n");
return 0;
}

Conclusion
In this lab I have come to know about the complete concept of Bubble sort
algorithms in C language as well as implementation of bubble sort algorithms and also optimize.I
also perform a different task that our respected Sir given to us. It is very interesting and these
tasks are very helpful in future. And after this lab ,I will be able to implement the programs in C
language

Lab Task: 04
Bubble sort Implementation Starting with the first item, assume that it is the largest
Compare it with the second item: –If the first is larger, swap the two –Otherwise, assume
that the second item is the largest Continue up the array, either swapping or redefining the
largest item.
Solution
#include <stdio.h>

// function to swap values of two variables


void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}

void bubble_sort(int a[], int size) {


int i, j;
for(i=0; i<size; i++) {
for(j=0; j<size-i-1; j++) {
if (a[j] > a[j+1]) {
swap(&a[j], &a[j+1]);
}
}
}
}

int main() {
int a[] = {4, 8, 1, 3, 10, 9, 2, 11, 5, 6};
bubble_sort(a, 10);

//printing array
int i;
for(i=0; i<10; i++) {
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
______________________________________________________________________________

Lab Task 05

Recursive Bubble sort has no additional performance/implementation advantages but can


be used to understand recursion and sorting concepts better.

Solution:
#include<stdio.h>
void BubbleSortRecursion(int a[],int num);
main()
{
int i,j,num,temp;
printf("Enter number of elements\n");
scanf("%d",&num);
int a[num];
printf("Enter numbers\n");
for(i=0;i<num;i++)
{
scanf("%d",&a[i]);
}
BubbleSortRecursion(a,num);
printf("Given numbers in Ascending order \n");
for(i=0;i<num;i++)
{
printf("%d\n",a[i]);
}
}
void BubbleSortRecursion(int a[],int num)
{
int i,j,temp;
i=num;
if(i>0)
{
for(j=0;j<num-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
BubbleSortRecursion(a,num-1);
}
else
{
return;
}
}
______________________________________________________________________________
______________________________________________________________________________

You might also like