LAB2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

LABORATORY MANUAL

Subject Name: CS09204 Data Structures and Algorithms


Student Name Rahhim Adeem
Reg. No. 70129477
Date 24th –Feb-2023

MAPPING OF LAB TO CLOs & PLOs


CLO 3: Implement commonly used data structures and PLO 5: Modern Tool Usage
algorithms using programming software. Cognitive Domain: C3

Rubric for Modern Tool Usage


Criteria Attainment Score
Excellent Very good Good Fair Poor
(100-85%) (84-71%) (70-61%) (60-50%) (49-0%)
Understanding Demonstrates Demonstrates a Demonstrates Demonstrates Demonstrates
of Engineering skillful ability to very good ability good ability to some ability to minimal or no
Tools describe and to describe and describe and/or describe ability to describe
explain the explain the explain the and/or explain and/or explain the
principles behind principles behind principles behind the principles principles behind
and applicability and applicability and applicability behind and and applicability
of engineering of engineering of engineering applicability of engineering
tools. tools. tools. of engineering tools.
tools.
Ability to Demonstrates Demonstrates a Demonstrates Demonstrates Demonstrates
perform skillful ability to very good good ability to some ability to minimal or no
experiment identify and use ability to identify identify and use identify or use ability to identify
using the most relevant and use relevant tools for an tools for an or use tools for an
Engineering
tools for a range of tools for an engineering engineering engineering
Tool (Dev C++)
engineering engineering activity, but may activity. activity.
activities. activity. not identify the
most relevant
tool.
Generation and Demonstrates Demonstrates a Demonstrates Demonstrates Demonstrates
Interpretation skillful ability to very good ability good ability to some ability to minimal or no
of results using generate results to generate generate results generate ability to generate
modern tools using modern results using using modern results using results using
(Dev C++)
tools. modern tools. tools. modern tools. modern tools.
Ability to Demonstrates Demonstrates a Demonstrates Demonstrates Demonstrates
relate skillful ability to very good ability good ability to some ability to minimal or no
experiment understand to understand understand understand ability to
with theory significance of significance of significance of significance of understand
and its experiment and its experiment and experiment and its experiment significance of
relation to the its relation to the relation to the and its relation experiment and its
significance
theory theory theory. to the theory relation to the
theory

1
Engr. Muhammad Arslan Rafique
Lab 02: To implement bubble sort, selection sort and insertion sort algorithms
on
an array type data structure.

Objective: To learn how to implement different sorting algorithms on an array type data
structure.
Sorting

Arrays:

The process of arranging data in a specified order is called sorting. Numeric type data may be
arranged either in ascending or in descending order. Similarly, character type data type may be
arranged in alphabetical order.

There are different methods to sort data into a list. The most commonly used methods
are:
i. Bubble Sort
ii. Selection Sort
iii. Insertion Sort

Bubble
Sort:

The bubble sort method is used to arrange values of an array in ascending or in descending
order.

To arrange an array in ascending order, two neighboring elements are compared. If one element is
larger than the other, the two are exchanged. Through the exchange of elements, the larger value
slowly floats or bubbles up to the top.

To sort data in an array of n elements, n-1 iterations are


required.

To understand Bubble sorting, a numerical example is given


below.

4 9 1 3

As n = 4;

So total iterations = 3
Iteration 1:

4 9 1 3
4 9 1 3
4 1 9 3
4 1 3 9
Iteration 2:

4 1 3 9
2
Engr. Muhammad Arslan Rafique
1 4 3 9
1 3 4 9

3
Engr. Muhammad Arslan Rafique
Iteration 3:

1 3 4 9
1 3 4 9
Algorithm:

begin BubbleSort(list)

for all elements of list


if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for

return list

end BubbleSort

Pseudocode of Bubble Sort algorithm can be written as follows:


procedure bubbleSort( list : array of items )

loop = list.count;

for i = 0 to loop-1 do:


swapped = false

for j = 0 to loop-1 do:

/* compare the adjacent elements */


if list[j] > list[j+1] then
/* swap them */
swap( list[j], list[j+1] )
swapped = true
end if

end for

/*if no number was swapped that means


array is sorted now, break the loop.*/
if(not swapped) then
break
end if
end for

end procedure return list


Selection Sort:
4
Engr. Muhammad Arslan Rafique
This method is also used for sorting arrays in ascending or in descending order. If an array has n
elements, n-1 iterations are required to sort the array.

Following method is used to sort an array in ascending order using selection sort:

In the 1st iteration the value in the first element is assumed to be the smallest. Then the next smallest
element is found in the array. This value is interchanged with the 1st element. Now the 1st element of
the array has the smallest value.

In the 2nd iteration, the smallest value from the second element to last element of list is found. This
value is interchanged with the second element of the array.

This process is repeated until the entire array is sorted.


Consider the following depicted array as an example.

For the first position in the sorted list, the whole list is scanned sequentially. The first position where
14 is stored presently, we search the whole list and find that 10 is the lowest value.

So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the list,
appears in the first position of the sorted list.

For the second position, where 33 is residing, we start scanning the rest of the list in a linear manner.

We find that 14 is the second lowest value in the list and it should appear at the second place. We
swap these values.

After two iterations, two least values are positioned at the beginning in a sorted manner.

The same process is applied to the rest of the items in the array.
5
Engr. Muhammad Arslan Rafique
Following is a pictorial depiction of the entire sorting process −

Algorithm:

Step 1 − Set MIN to location 0


Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted

Pseudocode of Selection Sort algorithm can be written as follows:

6
Engr. Muhammad Arslan Rafique
procedure selection sort
list : array of items
n : size of list

for i = 1 to n - 1
/* set current element as minimum*/
min = i

/* check the element to be minimum */

for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for

/* swap the minimum element with the current element*/


if indexMin != i then
swap list[min] and list[i]
end if
end for

end procedure

Insertion Sort:

The insertion sort method is used to arrange values of an array in ascending or in descending order.

Step 1: The second element of an array is compared with the elements that appears before it
(only first element in this case). If the second element is smaller than first element, second
element is inserted in the position of first element. After first step, first two elements of an array
will be sorted.

Step 2: The third element of an array is compared with the elements that appears before it (first and
second element). If third element is smaller than first element, it is inserted in the position of first
element. If third element is larger than first element but, smaller than second element, it is inserted
in the position of second element. If third element is larger than both the elements, it is kept in
the position as it is. After second step, first three elements of an array will be sorted.

Step 3: Similarly, the fourth element of an array is compared with the elements that appear
before it (first, second and third element) and the same procedure is applied and that element is
inserted in the proper position. After third step, first four elements of an array will be sorted.

If there are n elements to be sorted. Then, this procedure is repeated n-1 times to get sorted list of
array.

7
Engr. Muhammad Arslan Rafique
Figure: Sorting Array in Ascending Order Using Insertion Sort Algorithm
Algorithm:
Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted

Pseudocode of Insertion Sort algorithm can be written as follows:

procedure insertionSort( A : array of items )


int holePosition
int valueToInsert

for i = 1 to length(A) inclusive do:

/* select value to be inserted */


valueToInsert = A[i]
holePosition = i

8
Engr. Muhammad Arslan Rafique
/*locate hole position for the element to be inserted */

while holePosition > 0 and A[holePosition-1] > valueToInsert do:


A[holePosition] = A[holePosition-1]
holePosition = holePosition -1
end while

/* insert the number at hole position */


A[holePosition] = valueToInsert

end for

end procedure

Pointers:

A pointer is a variable that is used to store a memory address. Variables contain a value, while
pointers contain a memory address. Pointers help in allocating memory dynamically. Pointers are
used to create and manipulate data structures such as linked lists, queues, stacks, trees, etc.

Syntax:
type* variable_name;

int* a;

float* b;

void* c;
There are two operators of pointers:

Dereference *

It returns the contents of the memory location pointed to.

Reference (Address Operator) &

It gives the address of a variable. A simple program for pointers is:

int *ptr, var1, var2;

var1 = 5;

ptr = &var1;

var2 = *ptr;

9
Engr. Muhammad Arslan Rafique
Pictorially, it can be shown as:

Only two operations can be performed on pointers (addition and subtraction)

10
Engr. Muhammad Arslan Rafique
Lab Task:

1. Write a program to sort the following list by using bubble sorting method.

4 9 1 3

INPUT:

OUTPUT:

11
Engr. Muhammad Arslan Rafique
2. Write a program which takes names of five countries as input and prints them
in alphabetical order using bubble sorting method.

INPUT:

OUTPUT:

12
Engr. Muhammad Arslan Rafique
3. Write a program to sort the following list by using selection sorting method.

14 19 11 34

INPUT:

OUTPUT:

13
Engr. Muhammad Arslan Rafique
4. Write a program which takes names of five countries as input and prints them
in alphabetical order using selection sorting method.

INPUT:

OUTPUT:

14
Engr. Muhammad Arslan Rafique
5. Write a program to sort the following list by using insertion sorting method.

4 91 106 32

INPUT:

OUTPUT:

15
Engr. Muhammad Arslan Rafique
6. Write a program which takes names of five countries as input and prints them
in alphabetical order using insertion sorting method.

INPUT:

OUTPUT:

16
Engr. Muhammad Arslan Rafique
Lab Assessment
Understanding Ability to Generation and Ability to
of Engineering perform Interpretation of relate
Tools experiment using results using experiment Total
(Criteria 1) Engineering Tool modern tools with theory and 15
2 (Dev C++) (Dev C++) its significance Mark
(Criteria 2) (Criteria 3) (Criteria 4) s
6 4 3
Task 1

Task 2

Task 3

Task 4

Task 5

Task 6

Averag
e Marks

Lab Engineer Name: M. Arslan Rafique Signature: ___________________

17
Engr. Muhammad Arslan Rafique

You might also like