Ada Assignment
Ada Assignment
✗ Algorithm
Following are the steps involved in bubble sort(nfor sorting a given
array in ascending order):
Step 1: Starting with the first element(nindex = 0), compare the
current element with the next element of the array.
Step 2: If the current element is greater than the next element
of the array, swap them.
Step 3: If the current element is less than the next element,
move to the next element.
Step 4 : Repeat Step 1.
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
✗ Implementation
class BubbleSort
{
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
Output:
Sorted array:
11 12 22 25 34 64 90
Assignment 2: 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.
This algorithm is not suitable for large data sets as its average and worst
case complexities are of Ο(nn2), where n is the number of items.
✗ Algorithm
Following are the steps involved in selection sort(nfor sorting a given
array in ascending order):
Step 1: Starting from the first element, we search the smallest
element in the array, and replace it with the element in the first
position.
Step 2 : We then move on to the second position, and look for
smallest element present in the subarray, starting from index 1, till
the last index.
Step 3 : We replace the element at the second position in the original
array, or we can say at the first position in the subarray, with the
second smallest element.
Step 4: This is repeated, until the array is completely sorted.
✗ Implementation
class SelectionSort
{
void sort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void main(String args[])
{
SelectionSort ob = new SelectionSort();
int arr[] = {64,25,12,22,11};
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}}
Output:
Sorted array:
11 12 22 25 64
Assignment 3: 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 'insert'ed 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 (nin the same array). This algorithm is not
suitable for large data sets as its average and worst case complexity are of
Ο(nn2), where n is the number of items.
✗ Algorithm
Following are the steps involved in insertion sort:
Step 1 : We start by making the second element of the given array, i.e.
element at index 1, the key. The key element here is the new card that
we need to add to our existing sorted set of cards(nremember the
example with cards above).
Step 2 : We compare the key element with the element(ns) before it, in
this case, element at index 0:
•If the key element is less than the first element, we insert the key
element before the first element.
•If the key element is greater than the first element, then we insert it
after the first element.
Step 3 : Then, we make the third element of the array as key and will
compare it with elements to it's left and insert it at the right position.
Step 4 : And we go on repeating this, until the array is sorted.
✗ Implementation
class InsertionSort {
void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6 };
printArray(arr);
}
Output :
5 6 11 12 13
Assignment 4: Quick Sort
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 Ο(nn 2), where n is
the number of items.
✗ Algorithm
class QuickSort
{
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1); // index of smaller element
for (int j=low; j<high; j++)
{
if (arr[j] <= pivot)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
void sort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void main(String args[])
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = arr.length;
System.out.println("sorted array");
printArray(arr);
}
}
Output :
Sorted array:
1 5 7 8 9 10
Assignment 5: Linear Search
Linear search is a very simple search algorithm. In this type of search, a
sequential search is made over all items one by one. Every item is checked
and if a match is found then that particular item is returned, otherwise the
search continues till the end of the data collection.
✗ Algorithm
Linear Search (n Array A, Value x)
Step 1 : Set i to 1
Step 2 : if i > n then go to step 7
Step 3 : if A[i] = x then go to step 6
Step 4 : Set i to i + 1
Step 5 : Go to Step 2
Step 6 : Print Element x Found at index i and go to step 8
Step 7 : Print element not found
Step 8 : Exit
✗ Implementation
class LinearSearch
{
public static int search(int arr[], int x)
{
int n = arr.length;
for(int i = 0; i < n; i++)
{
if(arr[i] == x)
return i;
}
return -1;
}
public static void main(String args[])
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
✗ Algorithm
Following are the steps of implementation that we will be following:
Step 1 : Start with the middle element:
➢If the target value is equal to the middle element of the array, then
return the index of the middle element.
➢If not, then compare the middle element with the target value,
✔If the target value is greater than the number in the middle
index, then pick the elements to the right of the middle index, and
start with Step 1.
✔If the target value is less than the number in the middle index,
then pick the elements to the left of the middle index, and start
with Step 1.
Step 2 : When a match is found, return the index of the element
matched.
Step 3 : If no match is found, then return -1.
✗ Implementation
class BinarySearch {
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1; }
public static void main(String args[])
{ BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index " + result); } }
Output:
Element is present at index 3
Assignment 7:
Matrix Chain Multiplication
✗ Algorithm
Begin
define table minMul of size n x n, initially fill with all 0s
for length := 2 to n, do
fir i:=1 to n-length, do
j := i + length – 1
minMul[i, j] := ∞
for k := i to j-1, do
q := minMul[i, k] + minMul[k+1, j] +
array[i-1]*array[k]*array[j]
if q< minMul[i, j], then minMul[i, j] := q
done
done
done
return minMul[1, n-1]
End
✗ Implementation
class MatrixChainMultiplication
{ static int MatrixChainOrder(int p[], int i, int j)
{ if (i == j)
return 0;
int min = Integer.MAX_VALUE;
for (int k=i; k<j; k++)
{ int count = MatrixChainOrder(p, i, k) +
MatrixChainOrder(p, k+1, j) + p[i-1]*p[k]*p[j];
if (count < min)
min = count;
}
return min;
}
public static void main(String args[])
{
int arr[] = new int[] {1, 2, 3, 4, 3};
int n = arr.length;
System.out.println("Minimum number of multiplications is "+
MatrixChainOrder(arr, 1, n-1)); } }
Output :
Minimum number of multiplications is 30
Assignment 8 : Knapsack Problem
Given a set of items, each with a weight and a value, determine a subset of
items 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.
The knapsack problem is in combinatorial optimization problem. It
appears as a subproblem in many, more complex mathematical models of
real-world problems. One general approach to difficult problems is to
identify the most restrictive constraint, ignore the others, solve a knapsack
problem, and somehow adjust the solution to satisfy the ignored
constraints.
for i = 1 to n
do x[i] = 0
weight = 0
for i = 1 to n
if weight + w[i] ≤ W then
x[i] = 1
weight = weight + w[i]
else
x[i] = (nW - weight) / w[i]
weight = W
break
return x
✗ Implementation
import java.util.Arrays;
import java.util.Comparator;
public class FractionalKnapSack
{
public static void main(String[] args) {
int[] wt = {10, 40, 20, 30};
int[] val = {60, 40, 100, 120};
int capacity = 50;
double maxValue = getMaxValue(wt, val, capacity);
System.out.println("Maximum value we can obtain = " +
maxValue);
}
private static double getMaxValue(int[] wt,
int[] val, int capacity)
{
ItemValue[] iVal = new ItemValue[wt.length];
✗ Algorithm
Output :
0010
1000
0001
0100
Assignment 10 :
Dynamic Programming
Dynamic programming approach is similar to divide and conquer in
breaking down the problem into smaller and yet smaller possible sub-
problems. But unlike, divide and conquer, these sub-problems are not
solved independently. Rather, results of these smaller sub-problems are
remembered and used for similar or overlapping sub-problems.
Dynamic programming is used where we have problems, which can be
divided into similar sub-problems, so that their results can be re-used.
Mostly, these algorithms are used for optimization. Before solving the in-
hand sub-problem, dynamic algorithm will try to examine the results of the
previously solved sub-problems. The solutions of sub-problems are
combined in order to achieve the best solution.
So we can say that −
Algorithm
FLOYD-WARSHALL (W)
1 n ← rows [W]
2 D(0) ← W
3 for k ← 1 to n
4 do for i ← 1 to n
5 do for j ← 1 to n
6 do dij (k) ← MIN ( dij (k-1) , dik (k-1) + dkj (k-1) )
7 return D(n)