Description
Department of Computer Science and Engineering
Third Semester B. Tech.(CSE)
CS2092D Programming Laboratory Assignment #5
Policies for Submission and Evaluation:
• Ensure that your programs will compile and execute without errors in the Linux platform.
• Detection of ANY malpractice related to the lab course can lead to awarding an F grade in the course.
Naming Conventions for Submission
• Submit a single ZIP (.zip) file (do not submit in any other archived formats like .rar, .tar, .gz). The name of this file must be
ASSG<NUMBER> <ROLLNO> <FIRST-NAME>.zip
(Example: ASSG1 BxxyyyyCS LAXMAN.zip). DO NOT add any other files (like temporary files, input files, etc.) except your source code, into the zip archive.
• The source codes must be named as
ASSG<NUMBER> <ROLLNO> <FIRST-NAME> <PROGRAM-NUMBER>.c
(For example: ASSG1 BxxyyyyCS LAXMAN 1.c). If you do not conform to the above naming conventions, your submission might not be recognized by our automated tools, and hence will lead to a score of 0 marks for the submission. So, make sure that you follow the naming conventions.
Standard of Conduct
General Instructions
The Sorting Problem can be formally stated in terms of the input/output relationship as follows:
Input: A sequence of n numbers ha1,a2,…,ani
Output: A permutation of the input sequence such that .
QUESTIONS
1. Write a program that uses the Heap-Sort algorithm for sorting a given input sequence of integers present in an array A in non-decreasing order and prints the number of comparisons performed during sorting. Your program must contain the following functions: (the notation A[i..j] denotes the sub-array of A, contained within the ith and jth indices, both inclusive).
• A recursive function Max-Heapify(A,i) that takes as input an array A and lets the value at A[i] “float down” in the max-heap so that the subtree rooted at index i obeys the max-heap property.
• A function Build-Max-Heap(A) that takes as input an array A and build a max-heap on the input array A[1…n] where n is equal to A.length.
• A function Heapsort(A) that takes as input an array A and sorts an array A in place.
Input format:
• The first line of the input contains an integer n ∈ [0,105], the size of the array A.
• The second line lists the n elements in A, as space-separated integers in the range [−103,103].
Output Format:
• The first line of the output contains the elements of A in sorted order, separated by space.
• The second line of the output contains the number of comparisons performed during sorting.
Note:
The number of comparisons made by Heap-Sort is highly dependent on its implementation. As such, we will be considering the number of comparisons as per the algorithm given in CLRS.
Sample Input:
8
98 67 56 45 43 23 20 12
Sample Output:
12 20 23 43 45 56 67 98
24
• Main() – repeatedly reads a character ‘i’, ‘e’, ‘m ’, ‘d’ or ‘s’ from the terminal and calls the sub-functions appropriately until character ‘s’ is entered.
• Min-Heap-Insert(Q,k) – A function that inserts a new element with priority value k into the priority queue Q.
• Heap-Extract-Min(Q) – A function that takes as input a priority queue Q, removes and prints the element with the lowest priority. If the priority queue is empty, print −1.
• Heap-Minimum(Q) – A function that takes as input a priority queue Q, prints the element with the lowest priority without actually removing it from the priority queue. If the priority queue is empty, print −1.
Input format:
• First line contains a character from ‘i’, ‘e’, ‘m’, ‘d’, ‘s’ followed by at most two integers. The integers, if given, are in the range [1,106].
• Character ‘i’ is to insert the next integer, from the input into the priority queue. Character ‘i’ and integer are separated by space.
• Character ‘e’ is to remove and print the lowest key from the priority queue Q.
• Character ‘m’ is to print the lowest key from the priority queue Q without actually removing it.
• Character ‘d’ is to decrease the priority of an element in the priority queue. In this case, character ‘d’ is followed by two more integers, each separated by space. After this operation, the value at the position of first integer is decreased to the second integer.
• The character ‘s’ is to ‘stop’ the program.
Output Format:
• The output (if any) of each command should be printed on a separate line.
• For option ‘e’, remove and print the lowest key from the priority queue. If the priority queue is empty, then print −1.
• For option ‘m’, print the lowest key in the priority queue. If the priority queue is empty, then print −1.
Note:
In CLRS, Max-Priority Queue is discussed. CLRS also give an idea about how to implement Min-Priority Queue.
Sample Input :
i 100 i 110 i 95 e e i 120 e i 30
m
d 0 25m
e
e e s
Sample Output:
95
100
110
30
25
25
120 -1
Write a menu driven program to implement the above scenario as a Patient Scheduling Application, using a Priority Queue, Q (implemented as heap). Implement the following operations:
• Main() – repeatedly reads a character ‘i’, ‘e’, ‘m ’, ‘c’, ‘d’ or ‘s’ from the terminal and calls the sub-functions appropriately until character ‘s’ is entered.
• Insert-Patient(Q, k) – Inserts a new patient with token number k into the Queue Q.
• Extract-Next-Patient(Q) – Get the token number of the patient to be treated next and remove his/her priority from the queue.
• Get-Next-Patient(Q) – Get the token number of the patient to be treated next, from the Queue Q without removing it.
• Change-Token-Number(Q, k, x) – Change the token number of a patient from old value k to new value x in Queue Q. Assume that token number k is present in the Queue and x is lower than the current token number k.
• Display-Queue(Q) – Display all patients’ token numbers in the Queue Q in the order of their treatment (patient with lowest token number is treated first) without changing the queue.
Input format:
• The first line of the input contains a character from ‘i’, ‘e’, ‘m’, ‘c’, ‘d’, ‘s’ followed by at most two integers. The integers, if given, are in the range [1,106].
• Character ‘i’ is to insert the next integer, from the input into the Queue. Character ‘i’ and integer are separated by space.
• Character ‘e’ is to remove and print the patient with lowest token number from the Queue Q.
• Character ‘m’ is to print the patient with lowest token number from the Queue Q without removing it.
• Character ‘c’ is to change the token number of a patient in the Queue. In this case, character ‘c’ is followed by two more integers, each separated by space. After this operation, the patient’s token number with first integer is decreased to the second integer.
• The character ‘d’ is to display all patients’ token numbers in the Queue in the order of their treatment (Patient with lowest token number is treated first) without changing the queue.
• The character ‘s’ is to ‘stop’ the program.
Output Format:
• The output (if any) of each command should be printed on a separate line.
• For option ‘e’, remove and print the patient with lowest token number from the priority queue. If the Queue is empty, then print −1.
• For option ‘m’, print the patient with lowest token number in the Queue. If the Queue is empty, then print −1.
• For option ‘d’, display all patients’ token numbers in the Queue in the order in which they will get treatment (patient with lowest token number is treated first). Each token number should be separated by space.
Sample Input :
i 2 i 15 i 5 e i 4
m c 5 1 d e e e e s
Sample Output:
2
4
1 4 15
1
4
15
-1
Reviews
There are no reviews yet.