Lab7 DSA BSEE20034

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Electrical Engineering Department

CS243L: Data Structures and Algorithms

Course Instructor: Momina Azam Dated: November 8, 2021

Lab Engineer: Muhammad Usama Riaz


Semester: 3rd

Session: 2020-2024 Batch: BSEE2020

Lab. 7 Implementation and Analysis of Quick Sort

Name Roll No Lab ReportMarks/100 Total Marks


(Scaled out of 10)
BSEE20075
Muhammad umer
shakir

Checked on:

Signature:
7.1 Objective
The goal of this handout is to learn about how merge sort and quick sort are used to sort an
integer array and why they are called Divide and Conquer algorithms.
7.2 Equipment and Component
Component Value Quantity
Description
Computer Available in lab 1

7.3 Conduct of Lab


1. Students are required to perform this experiment individually.
2. In case the lab experiment is not understood, the students are advised to seek help from
the courseinstructor, lab engineers, assigned teaching assistants (TA) and lab attendants.

7.4 Theory and Background


Counting sort is in a class of algorithms called ‘no comparison’ sorting algorithms. Counting
sort is a sorting algorithm that sorts the elements of an array by counting the number of
occurrences of each unique element in the array. The count is stored in an auxiliary (Other)
array and the sorting is done by mapping the count as an index of the auxiliary array.

7.4.1 In Place Sorting


A sorting algorithm is called “in-place” if it does not need to allocate as much extra space as
the array to be sorted while doing the sorting. For Example, if the input array is 1 million
elements, the algorithm does not need to allocate extra array space of a million elements to
do its working (Please note that a few extra variables are not a problem, with just a few extra
variables it will still be in-place sorting!).
7.4.2 Quick Sort Algorithm
Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data
into smaller arrays. It picks an element as pivot and partitions the given array around the
picked pivot. There are many different versions of quicksort that pick pivot in different ways.
1. Always pick first element as pivot.
2. Always pick last element as pivot
3. Pick a random element as pivot.
4. Pick median as pivot.
The key process in quicksort is partition (). Target of partitions is, given an array and an
element x of array as pivot, put x at its correct position in sorted array and put all smaller
elements (smaller than x) before x, and put all greater elements (greater than x) after x. All
this should be done in linear time.
Quicksort 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.
Q.1 Write code for a partition function for quicksort as you have covered in class, it must be a
deterministic function where you had fixed a place to pick up the pivot value from.
[5 Mark].

#include<iostream>
using namespace std;

int partion(int arr[],int l,int r)


{
int pivot =arr[r];
int i=l-1;
for(int j=l; j<r; j++)
{
if(arr[j]<=pivot)
{
i++;
swap(arr[i],arr[j]);
}

}
swap(arr[i+1],arr[r]);
return (i+1);
}
int main()
{
int arr[10]={3,7,9,1,5,2,6,12,13,11};

partion(arr,0,9);
for(int i=0; i<10; i++)
{
cout<<arr[i]<<" ";
}

Q2. a) Write a randomized partition function that chooses randomly the index number for the pivot
on every call.
Use the rand () function for a random number in C++.
The random number must be restricted to the range of the indexes of your array.

Sample use of rand()

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
// This program will create same sequence of
// random numbers on every program run

for(int i = 0; i<5; i++)


printf(" %d ", rand());

return 0;
}

b) Is the partition an in-place function? Briefly defend your answer. [5 + 3 Mark]


#include<iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int partion(int arr[],int l,int r)


{
int n = rand();
int pivot= l + n%(r-l+1);

int i=l-1;
for(int j=l; j<r; j++)
{
if(arr[j]<=pivot)
{
i++;
swap(arr[i],arr[j]);
}

}
swap(arr[i+1],arr[r]);
return (i+1);
}

/*void quicksort(int arr[],int l,int r)


{
if(l<r)
{
int pi=partion(arr,l,r);
quicksort(arr,l,pi-1);
quicksort(arr,pi+1,r);
}

}*/
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout<<arr[i]<<" ";
}
int main()
{
int arr[10]={3,7,9,1,5,2,6,12,13,11};

partion(arr,0,9);
for(int i=0; i<10; i++)
{
cout<<arr[i]<<" ";

}
}
Part(b)
Quick sort is a in place sorting algorithm. Because portion function does give
an array and an element x of array as
pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x,
and put all greater elements (greater than x) after x.

Q3. Write code for the quicksort function, your task is to work out
a) arguments to the Quicksort function
b) base case that will terminate or end recursion
c) arguments to the Partition function [10 Mark]

#include<iostream>
using namespace std;

int partion(int arr[],int l,int r)


{
int pivot =arr[r];
int i=l-1;
for(int j=l; j<r; j++)
{
if(arr[j]<=pivot)
{
i++;
swap(arr[i],arr[j]);
}

}
swap(arr[i+1],arr[r]);
return (i+1);
}

void quicksort(int arr[],int l,int r)


{
if(l<r)
{
int pi=partion(arr,l,r);
quicksort(arr,l,pi-1);
quicksort(arr,pi+1,r);
}

}
int main()
{
int arr[10]={3,7,9,1,5,2,6,12,13,11};
quicksort(arr,0,4);

for(int i=0; i<10; i++)


{
cout<<arr[i]<<" ";

}
Student Name Registration# Batch: EE-2020

Assessment Rubrics
Method: Lab reports and instructor observation during lab sessions.
Outcome assessed:
a. Ability to conduct experiments, as well as to analyze and interpret data (P)
b. Ability to function on multi-disciplinary teams (A)
c. Ability to use the techniques, skills, and modern engineering tools necessary for engineering practice (P)

Performance metric Mapping (task no. Max Exceeds expectation Meets expectation Does not meet expectation Obtained
and description) marks marks
1. Realization of 1 Functionality 40 Executes without errors excellent Executes without errors, user Does not execute due to syntax errors,
experiment (a) user prompts, good use of prompts are understandable, runtime errors, user prompts are
symbols, spacing in output. minimum use of symbols or spacing misleading or non-existent. No testing has
Through testing has been in output. Some testing has been been completed (20-0)
completed (45-41) completed (40-21)
2. Teamwork (b) 1 Group 5 Actively engages and cooperates Cooperates with other group Distracts or discourages other group
Performance with other group member(s) in member(s) in a reasonable manner members from conducting the experiment
effective manner (5-4) but conduct can be improved (3-2) (1-0)
3. Conducting 1 On Spot 10 Able to make changes (5-4) Partially able to make changes (3-2) Unable to make changes (1-0)
experiment (a, c) Changes
2 Viva 10 Answered all questions (5-4) Few incorrect answers (3-2) Unable to answer all questions (1-0)
4. Laboratory safety 1 Code 5 Observes lab safety rules; Generally, observes safety rules and Disregards lab safety and disciplinary rules
and disciplinary rules commenting adheres to the lab disciplinary disciplinary guidelines with minor (1-0)
(a) guidelines aptly (5-4) lapses (3-2)
5. Data collection (c) 1 Code Structure 5 Excellent use of white space, Includes name, and assignment, Poor use of white space (indentation, blank
creatively organized work, white space makes the program lines) making code hard to read,
excellent use of variables and fairly easy to read. Title, organized disorganized and messy (1-0)
constants, correct identifiers for work, good use of variables (3-2)
constants, No line-wrap (5-4)
6. Data analysis (a, c) 1 Algorithm 20 Solution is efficient, easy to A logical solution that is easy to A difficult and inefficient solution (1-0)
understand, and maintain (5-4) follow but it is not the most
efficient (3-2)
7. Computer use (c) 1 Documentation 5 Timely documented (5-4) Late documented (3-2) Not documented (1-0)

Max Marks (total): 100 Obtained Marks (Total):

Lab Engineer Signature: ________________________

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy