You are on page 1of 7

Pak-Austria Fachhochschule Institute of Applied

Sciences and Technology, Haripur, Pakistan

DEPARTMENT OF ELECTRICAL ENGINEERING


Data structure and Algorithm Lab Manual
LAB 4

Submitted by:
Name: Muhammad Suleman
Reg# B20F0404EE026

Submitted to:
Name: Rafi Ullah
Designation: LAB Engineer
Date:
October 20, 2021
LAB NO 4
Sorting Algorithms: Bubble Sort and Selection Sort
OBJECTIVE:
• To understand & implement the working of sorting algorithms using arrays in C++.

APPARATUS:
The apparatus we used in this lab includes
Operating system (Windows)
DEVC++
Many compilers like IDE are present but we used DEVC++ it is efficient and easy to use.

INTRODUCTION:
It is often necessary to arrange the elements in an array in numerical order from highest to
lowest values i.e.; from ascending to descending and vice versa.
In this lab we will learn that about sorting types. Bubble sort makes multiple passes through a
list. It compares adjacent items and exchanges those that are out of order. Each pass through
the list places the next largest value in its proper place. In essence, each item “bubbles” up to
the location where it belongs.
The selection sort algorithm sorts an array by repeatedly finding the minimum element
(considering ascending order) from unsorted part and putting it at the beginning. The
algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted. 
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending order) from
the unsorted subarray is picked and moved to the sorted subarray. 

LAB TASK 1:
Write a code to sort the following arrays using selection sort method. [10, 34, 2, 56,7,67, 88,
42].

CODE:
#include <iostream>
using namespace std;
#define MAX 8
int intArray[MAX] = {10,34,2,56,7,67,88,42};
void display() {
int i;
cout<<"[ ";

for(i = 0;i < MAX;i++)


{ cout<< intArray[i]<<" ";
}
cout<<"]\n";
}
void selectionSort() {
int indexMin,i,j;

for(i = 0; i < MAX-1; i++) {


indexMin = i;

for(j = i+1;j < MAX;j++) {


if(intArray[j] < intArray[indexMin])
{ indexMin = j;
}
}
if(indexMin != i)
{
cout<<"Items swapped: ["<<intArray[i]<<","<<intArray[indexMin]<<" ]\n";

// swap the numbers


int temp = intArray[indexMin];
intArray[indexMin] = intArray[i];
intArray[i] = temp;
}
cout<<"Iteration "<<i+1<<" #:";
display();
}
}
int main()
{
cout<<"Input Array: ";
display();
selectionSort();
cout<<"Output Array: ";
display();
return 0; }

OUTPUT:
LAB TASK 2:
Write a code to sort the following arrays using bubble sort method. [10, 34, 2, 56, 7, 67, 88, 42].
CODE:
#include<iostream>
#define MAX 8
using
namespace std;

int list[MAX] = {10,34,2,56,7,67,88,42};


void display() {
int i;
cout<<"[";
for(i =0; i < MAX; i++)
{ cout<<list[i]<<" ";
}
cout<<"]\n";
}
void bubbleSort() {
int temp; int i,j; bool swapped = false;

for(i = 0; i < MAX; i++)


{ swapped = false;
for(j = 0; j < MAX-1; j++)
{ cout<<"Items compared: ["<<list[j]<<","<<list[j+1]<<"]";
if(list[j] > list[j+1]) {
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
swapped = true;
cout<<"=>swapped["<<list[j]<<","<<list[j+1]<<"]\n";
}
else {
cout<<" => not swapped\n";
}

}
if(!swapped) {
break;
}
cout<<"Iteration "<<i+1<<" #";
display();
}
}
int main() {
cout<<"Input Array: ";
display();
cout<<"\n";

bubbleSort();
cout<<"\nOutput Array: ";
display();
return 0;
}
OUTPUT:

ANALYSIS AND RESULTS / CONCUSION:


All the tasks have been performed and results are prepared.

FIRST TASK:
In the first task an unsorted array is sorted by selection sort. In the process array has been
declared and then for loop is used and minimum variable has been declared, this loop will
function n-1 times, to search the minimum number, and another for loop is used. In this loop
will start from i+1 to compare the minimum number with elements of array and swap condition
is used to swap if condition become true. Then function has been called and sorted array has
been displayed on the screen.

SECOND TASK:
In a second task an unsorted array has been sorted by using bubble sort method. In this method
array has been declared and for loop has been used up to n times and this loop is used for
passes and another for loop is used up to n-1 times for comparing and swapping numbers index
with greater numbers move to right side with passes and iterations such that ascending order
has been followed and output of sorted arrays has been displayed on the screen.

You might also like