You are on page 1of 7

Design & Analysis of Algorithm

ASSIGNMENT#1
 

 
Name:         Arbaz Ahmad Khan
Roll No:   BCSM -F18-308
Subject:   Design and Analysis of Algorithm
Class:        BSCS
Section:     5A
 
 
 
Submitted To:    Sir Imran
 

Page | 1
Design & Analysis of Algorithm

Merge Sort Implementation in C++ Language


The merge sort technique is based on divide and conquer technique. We divide the
while data set into smaller parts and merge them into a larger piece in sorted order.
It is also very effective for worst cases because this algorithm has lower time
complexity for worst case also.
C++ Implementation

Code:
#include <iostream> using

namespace std;

void merge(int arr[], int p, int q, int r) {

// Create L ← A[p..q] and M ← A[q+1..r]

int n1 = q - p + 1; int n2 = r - q; int

L[n1], M[n2]; for (int i = 0; i < n1; i++)

L[i] = arr[p + i]; for (int j = 0; j < n2; j++)

M[j] = arr[q + 1 + j];

int i, j, k; i = 0; j = 0; k

= p; while (i < n1 && j <

n2) { if (L[i] <= M[j]) {

arr[k] = L[i];

i++; } else {

arr[k] = M[j];

j++;

k++;

Page | 2
Design & Analysis of Algorithm

// When we run out of elements in either L or M, //

pick up the remaining elements and put in A[p..r]

while (i < n1) { arr[k] = L[i]; i++; k++;

while (j < n2) {

arr[k] = M[j];

j++; k++;

void mergeSort(int arr[], int l, int r) {

if (l < r) {

// m is the point where the array is divided into two subarrays

int m = l + (r - l) / 2; mergeSort(arr, l, m); mergeSort(arr, m +

1, r);

merge(arr, l, m, r);

void printArray(int arr[], int size) {

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

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

endl;

int main() { int arr[] = {6, 5, 12, 10,

9, 1}; int size = sizeof(arr) /

sizeof(arr[0]); mergeSort(arr, 0, size

Page | 3
Design & Analysis of Algorithm

- 1); cout << "Sorted array: \n";

printArray(arr, size); return 0;

Recursive Example of Merge Sort


#include <iostream> using
namespace std; void
merge(int *,int, int , int );
void merge_sort(int *arr, int low, int high)
{ int mid; if
(low < high){
//divide the array at mid and sort independently using merge sort
mid=(low+high)/2; merge_sort(arr,low,mid);
merge_sort(arr,mid+1,high); //merge or conquer sorted arrays
merge(arr,low,high,mid);
}
} // Merge
sort
void merge(int *arr, int low, int high, int mid)
{ int i, j, k, c[50]; i = low; k
= low; j = mid + 1; while (i
<= mid && j <= high) {
if (arr[i] < arr[j]) {
c[k] = arr[i]; k+
+; i++; }
else { c[k] =
arr[j]; k++;
j++;
}
}
while (i <= mid) {
c[k] = arr[i]; k+
+; i++;
}
while (j <= high) {
c[k] = arr[j]; k+
+; j++; }

Page | 4
Design & Analysis of Algorithm

for (i = low; i < k; i++)


{ arr[i] = c[i];
}
}
// read input array and call mergesort
int main() { int myarray[30],
num;
cout<<"Enter number of elements to be sorted:";
cin>>num;
cout<<"Enter "<<num<<" elements to be sorted:";
for (int i = 0; i < num; i++) { cin>>myarray[i];
}
merge_sort(myarray, 0, num-1);
cout<<"Sorted array\n"; for (int
i = 0; i < num; i++)
{
cout<<myarray[i]<<"\t";
}
}

Output:
Enter the number of elements to be sorted:10

Enter 10 elements to be sorted:11 12 23 32 105 24 54 74 81 13

Sorted array

11 12 13 23 24 32 54 74 81 105

Implementation of Insertion Sort in C++


In the insertion sort technique, we start from the second element and compare it
with the first element and put it in a proper place. Then we perform this process
for the subsequent elements.

Page | 5
Design & Analysis of Algorithm

We compare each element with all its previous elements and put or insert the
element in its proper position. Insertion sort technique is more feasible for arrays
with a smaller number of elements. It is also useful for sorting linked lists.

General Algorithm

Repeat Steps 2 to 5 for K = 1 to N-1


set temp = A[K]
set J = K – 1
Repeat while temp <=A[J] set A[J + 1] =
A[J] set J = J – 1
[end of inner loop]
set A[J + 1] = temp
[end of loop]
exit

C++ Implementation

Code:
#include<iostream>
using namespace std;
int main ()
{
int myarray[10] = { 12,4,3,1,15,45,33,21,10,2};
cout<<"\nInput list is \n"; for(int i=0;i<10;i++)
{

Page | 6
Design & Analysis of Algorithm

cout <<myarray[i]<<"\t";
}
for(int k=1; k<10; k++)
{
int temp = myarray[k];
int j= k-1;
while(j>=0 && temp <= myarray[j])
{
myarray[j+1] = myarray[j];
j = j-1;
}
myarray[j+1] = temp;
}
cout<<"\nSorted list is \n";
for(int i=0;i<10;i++)
{
cout <<myarray[i]<<"\t";
}
}

Page | 7

You might also like