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

LABORATORY MANUAL

Subject Name: CS09204 Data Structures and Algorithms


Student Name Rahhim Adeem
Reg. No. 70129477
Date 3rd -March-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 03: Implementation of different Sorting Algorithms, Shell sort, Radix
sort and Quick Sort on array type data structures

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

Shell Sort:
Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm. This
algorithm avoids large shifts as in case of insertion sort, if the smaller value is to the far right
and has to be moved to the far left.

This algorithm uses insertion sort on a widely spread elements, first to sort them and then
sorts the less widely spaced elements. This spacing is termed as interval. This interval is
calculated based on Knuth's formula as −

Knuth's Formula

h=h*3+1

where −
h is interval with initial value 1

This algorithm is quite efficient for medium-sized data sets as its average and worst case
complexity are of Ο(n), where n is the number of items.

How Shell Sort Works?


Let us consider the following example to have an idea of how shell sort works. We take the
same array we have used in our previous examples. For our example and ease of
understanding, we take the interval of 4. Make a virtual sub-list of all values located at the
interval of 4 positions. Here these values are {35, 14}, {33, 19}, {42, 27} and {10, 44}

We compare values in each sub-list and swap them (if necessary) in the original array. After
this step, the new array should look like this −

2
Engr. Muhammad Arslan Rafique
Then, we take interval of 2 and this gap generates two sub-lists - {14, 27, 35, 42}, {19, 10,
33, 44}

We compare and swap the values, if required, in the original array. After this step, the array
should look like this −

Finally, we sort the rest of the array using interval of value 1. Shell sort uses insertion sort to
sort the array.

Following is the step-by-step depiction –

3
Engr. Muhammad Arslan Rafique
We see that it required only four swaps to sort the rest of the array.

Algorithm
Following is the algorithm for shell sort.

Step 1 − Initialize the value of h


Step 2 − Divide the list into smaller sub-list of equal interval h
Step 3 − Sort these sub-lists using insertion sort
Step 3 − Repeat until complete list is sorted

Pseudocode
Following is the pseudocode for shell sort.

procedure shellSort()
A : array of items
/* calculate interval*/
while interval < A.length /3 do:
interval = interval * 3 + 1
end while
while interval > 0 do:
for outer = interval; outer < A.length; outer ++ do:

4
Engr. Muhammad Arslan Rafique
/* select value to be inserted */
valueToInsert = A[outer]
inner = outer;
/*shift element towards right*/
while inner > interval -1 && A[inner - interval] >= valueToInsert do:
A[inner] = A[inner - interval]
inner = inner - interval
end while
/* insert the number at hole position */
A[inner] = valueToInsert
end for
/* calculate interval*/
interval = (interval -1) /3;
end while
end procedure

Quicksort:
Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data
into smaller arrays. A large array is partitioned into two arrays one of which holds values
smaller than the specified value, say pivot, based on which the partition is made and another
array holds values greater than the pivot value.
Quick sort partitions an array and then calls itself recursively twice to sort the two resulting
subarrays. This algorithm is quite efficient for large-sized data sets as its average and worst
case complexity are of Ο(n2), where n is the number of items.
Partition in Quick Sort
Following animated representation explains how to find the pivot value in an array.

Unsorted Array

The pivot value divides the list into two parts. And recursively, we find the pivot for each
sub-lists until all lists contains only one element.

Quick Sort Pivot Algorithm


Based on our understanding of partitioning in quick sort, we will now try to write an
algorithm for it, which is as follows.

5
Engr. Muhammad Arslan Rafique
Step 1 − Choose the highest index value has pivot
Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot

Quick Sort Pivot Pseudocode


The pseudocode for the above algorithm can be derived as −

function partitionFunc(left, right, pivot)


leftPointer = left
rightPointer = right - 1

while True do
while A[++leftPointer] < pivot do
//do-nothing
end while

while rightPointer > 0 && A[--rightPointer] > pivot do


//do-nothing
end while

if leftPointer >= rightPointer


break
else
swap leftPointer,rightPointer
end if

end while

swap leftPointer,right
return leftPointer

6
Engr. Muhammad Arslan Rafique
end function

Quick Sort Algorithm


Using pivot algorithm recursively, we end up with smaller possible partitions. Each partition
is then processed for quick sort. We define recursive algorithm for quicksort as follows −

Step 1 − Make the right-most index value pivot


Step 2 − partition the array using pivot value
Step 3 − quicksort left partition recursively
Step 4 − quicksort right partition recursively

Quick Sort Pseudocode


To get more into it, let see the pseudocode for quick sort algorithm −

procedure quickSort(left, right)


if right-left <= 0
return
else
pivot = A[right]
partition = partitionFunc(left, right, pivot)
quickSort(left,partition-1)
quickSort(partition+1,right)
end if
end procedure

Radix Sort:
The sorts we’ve looked at so far treat the key as a simple numerical value that is compared
with other values to sort the data. The radix sort disassembles the key into digits and arranges
the data items according to the value of the digits. Amazingly, no comparisons are necessary.
Algorithm for the Radix Sort:
We’ll discuss the radix sort in terms of normal base-10 arithmetic, which is easier to
visualize. However, an efficient implementation of the radix sort would use base-2 arithmetic
to take advantage of the computer’s speed in bit manipulation. We’ll look at the radix sort
rather than the similar but somewhat more complex radix-exchange sort. The word radix
means the base of a system of numbers. Ten is the radix of the decimal system and 2 is the
radix of the binary system. The sort involves examining each digit of the key separately,
starting with the 1s (least significant) digit.
1. All the data items are divided into 10 groups, according to the value of their 1s digit.
2. These 10 groups are then reassembled: All the keys ending with 0 go first, followed
by all the keys ending in 1, and so on up to 9. We’ll call these steps a sub-sort.
3. In the second sub-sort, all data is divided into 10 groups again, but this time according
to the value of their 10s digit. This must be done without changing the order of the

7
Engr. Muhammad Arslan Rafique
previous sort. That is, within each of the 10 groups, the ordering of the items remains
the same as it was after step 2; the sub-sorts must be stable.
4. Again the 10 groups are recombined, those with a 10s digit of 0 first, then those with
a 10s digit of 1, and so on up to 9.
5. This process is repeated for the remaining digits. If some keys have fewer digits than
others, their higher-order digits are considered to be 0. Here’s an example, using
seven data items, each with three digits. Leading zeros are shown for clarity.

421 240 035 532 305 430 124 // unsorted array


(240 430) (421) (532) (124) (035 305) // sorted on 1s digit
(305) (421 124) (430 532 035) (240) // sorted on 10s digit
(035) (124) (240) (305) (421 430) (532) // sorted on 100s digit
035 124 240 305 421 430 532 // sorted array
The parentheses delineate the groups. Within each group the digits in the appropriate position
are the same. To convince yourself that this approach really works, try it on a piece of paper
with some numbers you make up.

Designing a Program:

In practice the original data probably starts out in an ordinary array. Where should the 10
groups go? There’s a problem with using another array or an array of 10 arrays. It’s not likely
there will be exactly the same number of 0s, 1s, 2s, and so on in every digit position, so it’s
hard to know how big to make the arrays. One way to solve this problem is to use 10 linked
lists instead of 10 arrays. Linked lists expand and contract as needed. We’ll use this approach.
An outer loop looks at each digit of the keys in turn. There are two inner loops: The first
takes the data from the array and puts it on the lists; the second copies it from the lists back to
the array. You need to use the right kind of linked list. To keep the sub-sorts stable, you need
the data to come out of each list in the same order it went in
Efficiency of the Radix Sort:
At first glance the efficiency of the radix sort seems too good to be true. All you do is copy
the original data from the array to the lists and back again. If there are 10 data items, this is
20 copies. You repeat this procedure once for each digit. If you assume, say, 5-digit numbers,
then you’ll have 20*5 equals 100 copies. If you have 100 data items, there are 200*5 equals
1,000 copies. The number of copies is proportional to the number of data items, which is
O(N), the most efficient sorting algorithm we’ve seen.
Unfortunately, it’s generally true that if you have more data items, you’ll need longer keys. If
you ave 10 times as much data, you may need to add another digit to the key. The number of
copies is proportional to the number of data items times the number of digits in the key. The
number of digits is the log of the key values, so in most situations we’re back to O(N*logN)
efficiency, the same as quicksort.
There are no comparisons, although it takes time to extract each digit from the number. This
must be done once for every two copies. It may be, however, that a given computer can do
the digit-extraction in binary more quickly than it can do a comparison. Of course, like
mergesort, the radix sort uses about twice as much memory as quicksort.

8
Engr. Muhammad Arslan Rafique
9
Engr. Muhammad Arslan Rafique
Lab Tasks:
1. Write a program that sort an array through shell sort? Also write its algorithm.

INPUT:

OUTPUT:

ALGOTITHM:

1. Start with a gap value (usually half the length of the array).
2. Divide the array into smaller subarrays of size gap.
3. For each subarray, sort the elements using Insertion Sort.
4. Repeat step 2 and 3 with a smaller gap value until the gap value is 1.
5. Apply the Insertion Sort to the whole array.
As Shell Sort is a variation of Insertion Sort, where instead of comparing adjacent elements,
elements at a distance of gap are compared and swapped.

10
Engr. Muhammad Arslan Rafique
2. Write a program that sort an array through quick sort? Also write its algorithm.

INPUT:

OUTPUT:

ALGORITHM:

1. Choose a pivot element (usually the rightmost element in the array).


2. Partition the array into two partitions such that all elements less than or equal to the
pivot are on the left partition, and all elements greater than the pivot are on the right
partition.
3. Recursively apply steps 1 and 2 on the left and right partitions until the partitions
contain only one element or are empty.
Quick Sort is an efficient sorting algorithm with an average time complexity of O(n log n)
and a worst-case time complexity of O(n^2).

11
Engr. Muhammad Arslan Rafique
3. Write a program that sort an array through radix sort? Also write its algorithm.

INPUT:

OUTPUT:

ALGORITHM:

1. Find the maximum number in the array to determine the number of digits required for
the largest number.
2. For each digit place (starting from the least significant to the most significant), use
counting sort to sort the array based on the digit at that place.
3. Repeat step 2 for each digit place until the entire number is sorted.

12
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
(Criteria 1) Engineering Tool modern tools with theory and Total
2 (Dev C++) (Dev C++) its significance 15
(Criteria 2) (Criteria 3) (Criteria 4) Marks
6 4 3
Task 1

Task 2

Task 3

Average
Marks

Lab Engineer Name: M. Arslan Rafique Signature: ___________________

13
Engr. Muhammad Arslan Rafique

You might also like