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

CS2001L Fall 2021 Lab03

The document outlines Lab 03 for CS 2001L at DHA Suffa University, focusing on sorting algorithms: Bubble Sort, Selection Sort, and Insertion Sort. It details the mechanics of each algorithm, including their operational processes and time complexities. Additionally, it provides a lab assignment requiring students to implement these sorting algorithms in a C++ program that reads from and writes to text files.

Uploaded by

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

CS2001L Fall 2021 Lab03

The document outlines Lab 03 for CS 2001L at DHA Suffa University, focusing on sorting algorithms: Bubble Sort, Selection Sort, and Insertion Sort. It details the mechanics of each algorithm, including their operational processes and time complexities. Additionally, it provides a lab assignment requiring students to implement these sorting algorithms in a C++ program that reads from and writes to text files.

Uploaded by

sancrypto77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DHA Suffa University

Department of Computer Science


CS 2001L – Data Structures and Algorithms Lab
Fall 2021

Lab 03 – Sorting Algorithms


Objective:
To learn about
 Bubble Sort
 Selection Sort
 Insertion Sort

Bubble Sort
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm
in which each pair of adjacent elements is compared and the elements are swapped if they are not
in order.

How Bubble Sort Works


We take an unsorted array for our example. Bubble sort takes Ο(n2) time.

Bubble sort starts with very first two elements, comparing them to check which one is greater.

In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33
with 27.

We find that 27 is smaller than 33 and these two values must be swapped.

The new array should look like this −


Next we compare 33 and 35. We find that both are in already sorted positions.

Then we move to the next two values, 35 and 10.

We know then that 10 is smaller 35. Hence they are not sorted.

We swap these values. We find that we have reached the end of the array. After one iteration,
the array should look like this −

To be precise, we are now showing how an array should look like after each iteration. After the
second iteration, it should look like this −

Notice that after each iteration, at least one value moves at the end.

And when there's no swap required, bubble sorts learns that an array is completely sorted.

Now we should look into some practical aspects of bubble sort.


Implementation:
Selection Sort:

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-
based algorithm in which the list is divided into two parts, the sorted part at the left end and the
unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire
list.

The smallest element is selected from the unsorted array and swapped with the leftmost element,
and that element becomes a part of the sorted array. This process continues moving unsorted
array boundary by one element to the right.

How Selection Sort Works?

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.
Following is a pictorial depiction of the entire sorting process −
Implementation:
Insertion Sort:

This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is


always sorted. For example, the lower part of an array is maintained to be sorted. An element
which is to be inserted in this sorted sub-list, has to find its appropriate place and then it has to be
inserted there. Hence the name, insertion sort.

The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-
list (in the same array).

How Insertion Sort Works?

We take an unsorted array for our example.

Insertion sort compares the first two elements.

It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.

Insertion sort moves ahead and compares 33 with 27.

And finds that 33 is not in the correct position.

It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that the
sorted sub-list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list
remains sorted after swapping.

By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10.
These values are not in a sorted order.

So we swap them.

However, swapping makes 27 and 10 unsorted.

Hence, we swap them too.

Again we find 14 and 10 in an unsorted order.

We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.

This process goes on until all the unsorted values are covered in a sorted sub-list.
Implementation:

<
LAB ASSIGNMENT

1. Create a file called unsorted.txt and copy/type the following integer numbers into that
file.

23
-9
15
8
12
-23
-10
10
17

Now, create a program file with Roll No. and Lab No. (e.g.: “cs201xxx_Lab03”) with
the following requirements:
 In your main function, read the unsorted.txt file in an array. You do not know
exactly how many numbers there are in the file (your program should be generic),
so declare an array of sufficiently large size and use the while loop to read the
numbers into the array.
 Then, create a function to sort this array in ascending order using all the sorting
algorithms we have covered in the lab. You need to call this function and pass the
array to this function from the main function.
 In your main function, write the sorted array to an output file named sorted.txt.

You can use the following links for better understanding of filing in C++:
 https://www.udacity.com/blog/2021/05/how-to-read-from-a-file-in-cpp.html
 https://www.cplusplus.com/doc/tutorial/files/

SUBMISSION GUIDELINES

 Take a screenshot of each task (code and its output), labeled properly.
 Place all the screenshots and the code files (properly labeled) in a single folder labeled
with Roll No and Lab No. e.g. ‘cs201xxx_Lab03’.
 Submit the folder at LMS
 -100% policies for plagiarism.

You might also like