Professional Documents
Culture Documents
Practical File of Design of Algorithms and Analysis
Practical File of Design of Algorithms and Analysis
Of
Design of Algorithms and Analysis
for
B.Tech.(CSE) degree program
Submitted by
Abidullah
Roll No.17002101
CSE 6th Semester
Submitted to
Ms. Astha Gautam
Associate Professor
Department of CSE
APG (Alakh Prakash Goyal) Shimla University
Date: 22/06/2020
Experiment No. 1
Problem Description
#include<iostream>
#include<cstdlib>
return index;
}
// Swapping pvt value from high, so pvt value will be taken as pivot while
partitioni
ng.
swap(&a[high], &a[pvt]);
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
QuickSort(arr, 0, n-1);
Output:
1 Problem Solution
1. Split the data into two equal half until we get at most one element in both half.
2. Merge Both into one making sure the resulting sequence is sorted.
3. Recursively split them and merge on the basis of constraint given in step 1.
4. Display the result.
5. Exit.
2 Program/Source Code
C++ program to implement Merge sort.
#include <iostream>
}
else
{
t = a[i]; k++;
e i++;
m
p
[
k temp[k] = a[j];
] k++;
j++;
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
MergeSort(arr, 0, n-1);
// Printing the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}
3 Program Explanation:
1. Take input of data.
2. Call MergeSort() function.
3. Recursively split the array into two equal parts.
4. Split them until we get at most one element in both half.
5. Combine the result by invoking Merge().
6. It combines the individually sorted data from low to mid and mid+1 to high.
7. Return to main and display the result.
8. Exit.
4 Output:
Enter the number of data element to be sorted: 10
Enter element 1: 23
Enter element 2: 987
Enter element 3: 45
Enter element 4: 65
Enter element 5: 32
Enter element 6: 9
Enter element 7: 475
Enter element 8: 1
Enter element 9: 17
Enter element 10: 3
Description: This is a C++ Program to solve fractional knapsack. The knapsack problem
or rucksack problem is a problem in combinatorial optimization: Given a set of items,
each with a mass and a value, determine the number of each item to include in a
collection so that the total weight is less than or equal to a given limit and the total value
is as large as possible. It derives its name from the problem faced by someone who is
constrained by a fixed-size knapsack and must fill it with the most valuable items.
#include<iostream
> using
namespace std; int
main()
{
int array[2][100], n, w, i, curw, used[100], maxi = -1, totalprofit = 0;
Output:
sort.
Problem Description
1. Heap sort is a comparison based algorithm.
2. It is improved version of selection sort.
Problem Solution
1. Build a max heap using the given data element.
2. Delete the root node repeatedly.
3. Store the node at the end of the array.
4. Display the result.
5. Exit.
Program/Source Code
C++ program to implement Heap Sort.
#include <iostream>
// A function to
heapify the array. void
MaxHeapify(int a[],
int i, int n)
{
int j,
temp;
temp =
a[i]; j =
2*i;
while (j <= n)
{
if (j < n && a[j+1] >
a[j]) j = j+1;
return 0;
}
6 Program Explanation
1. Take input of data.
2. Call Build MaxHeap() function with ‘arr’ the array of data and ‘n-1’ the number of values,
in the argument list.
3. After building the max heap call HeapSort().
4. Switch the root value of heap with the last index value of array since root value is
highest among all.
5. Decrement the last index value.
6. Repeat it for all the element.
7. Return to main and display the result.
8. Exit.
7 Output:
Enter the number of data element to be
sorted: 10 Enter element 1: 9
Enter element 2: 6
Enter element 3: 4
Enter element 4: 3
Enter element 5: 8
Enter element 6: 7
Enter element 7: 5
Enter element 8: 2
Enter element 9: 0
Enter element 10: 1
Experiment No.
Problem
search. Description
We have to write a C++ Program which either finds the position of an element in an array or detects
the presence of an element in an array using Binary Search Algorithm.
Case 1. Average Case: When the element to be searched for is any random element from the
array. If the input array is {1, 2, 3, 4, 5, 6}
and the element to be searched for is 6,
then the output will be SEARCH SUCCESSFUL.
Case 2. Best case: When the element to be searched for is the middle element of the
array. If the input array is {-3, 31, 66}
and the element to be searched is 31,
then the output will be SEARCH SUCCESSFUL.
Case 3. Worst Case: When the element searched for is absent and it is either smaller or
greater than all the elements.
If the input array is {1, 1, 3, 6, 9}
and the element to be searched for
is 10, then the output will be
SEARCH FAILED
8 Problem Solution
We will be having an array of numbers, we just need to find out the position of an element in
the array if it is present. It can be done using Linear Search but it takes up a lot of time, so we
need a better searching algorithm that performs the same task but in less time in comparison
to Linear Search. In worst case Linear Search takes O(n) time, but Binary Search takes O(log
n) time.
9 Program/Source Code
Here is source code of the C++ Program to find the position of an element requested by the
user using Binary Search Algorithm using iteration.
/*
* C++ program to accept N numbers sorted in ascending order
* and to search for a given number using Binary Search.
* Report success or failure.
*/
#include
<iostream>
using
namespace
std; class BS
{
public:
/*
* Binary Search function
*/
void BinarySearch(int array[], int keynum, int num)
{
int low = 1;
int high = num;
int mid;
do
{
mid = (low + high) / 2;
if (keynum < array[mid])
{
high = mid - 1;
}
else if (keynum > array[mid])
{
low = mid + 1;
}
}
while (keynum != array[mid] && low <= high);
if (keynum == array[mid])
{
cout<<"SEARCH SUCCESSFUL \n";
}
else
{
cout<<"SEARCH FAILED \n";
}
}
};
int main()
{
int array[10];
int i, j, num, temp, keynum;
int low, mid, high;
cout<<"Enter the value of num \n";
cin>>num;
cout<<"Enter the elements one by one \n";
for (i = 0; i < num; i++)
{
cin>>array[i];
}
/*
* Bubble sort
*/
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
cout<<"Enter the element to be searched \n";
cin>>keynum;
// Binary searching begins
BS b1;
b1.BinarySearch(array, keynum, num);
return 0;
}
10 Program Explanation
1. Here in this program we have first requested the array of numbers and the value to be
searched from the user.
2. Since binary search is applied upon a sorted array we have created a separate bubble
sort function to sort the array before searching.
3. After sorting the array we have passed the array to the function called
BinarySearch(array, keynum, num) having array, key and the size of array as its
parameters.
4. In BinarySearch(array, keynum, num) we first find the middle index of the array, and
then compare the key with the middle element of array i.e. array[middle].
5. If key is less than the middle element of array we divide the array in to two halves i.e. low to
mid-1 and (mid+1) to high, where mid is the middle index, low is the starting and high is the ending
index of the array. After dividing the array we start searching in the left half of the array i.e. low to
mid-1.
6. If the key is greater than the middle element then we search in the right half of the array
i.e mid+1 to high.
7. In each iteration we divide the array until it has only 1 element left.
8. If we are able to find the element we print “SEARCH SUCCESSFUL” otherwise we print
“SEARCH FAILED”.
11 Output:
1. Enter the value of num
6
Enter the elements one by one
123456
Enter the element to be searched
6
SEARCH SUCCESSFUL