Department of Electrical Engineering: Riphah International University, Islamabad, Pakistan
Department of Electrical Engineering: Riphah International University, Islamabad, Pakistan
OBJECTIVES:
To implement the bubble sorting algorithm and Optimized bubble sort algorithm in C language.
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};
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>
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
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;
}
}
______________________________________________________________________________
______________________________________________________________________________