SlideShare a Scribd company logo
Homework Assignment 1 
ICT:5102, Data Structure & Algorithm 
 
Md.Dedarul Hasan 
PGD in IT, Registration No: 0417311011 
Session: April,2017 
 
 
 
 
 
INTRODUCTION OF TASK TO DO 
Insertion Sort, Merge Sort & Quick Sort Algorithms.Total Comparison & Swap Count                       
During Sort Proces. Array Sorting By Ascending, Descending & Random Order                     
Implementations. 
GIVEN INPUT ARRAY 
{195, 134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162,165, 
192,131, 142, 120, 185, 102, 181, 107, 198, 106, 176, 121, 178, 119, 128, 193, 127, 123, 143, 
155, 186,191, 122, 132, 158, 129, 183, 163, 180, 103, 188, 150, 151, 172, 118, 174, 170, 104, 
130, 116, 117,112, 139, 194, 147, 153, 164, 169, 199, 148, 138, 200, 190, 126, 152, 161, 179, 
149, 137, 133, 110,159, 113, 140, 160, 105, 184, 182, 135, 114, 125, 168, 189, 124, 108, 187, 
166, 156, 109, 167, 157} 
 
 
 
 
 
 
 
 
1   
CODE FOR INSERTION SORT WITH COMPARISON & SWAP 
#include <stdio.h> 
#include <math.h> 
int comparsioncounter=0; 
int swapcounter=0; 
//METHOD FOR INSERTION 
SORT****************************************************************************************
*************// 
void insertionSort(int arr[], int size){ 
int i, key, j; 
for (i = 1; i <size; i++){ 
comparsioncounter++; 
key = arr[i]; 
j = i-1; 
while (j >= 0 && arr[j] > key) 
{ 
comparsioncounter++; 
arr[j+1] = arr[j]; 
swapcounter++; 
j = j-1; 
} 
arr[j+1] = key; 
swapcounter++; 
} 
} 
2   
//METHOD TO PRINT AN ARRAY OF SIZE n 
********************************************************************************************// 
void printArray(int arr[], int size){ 
int i; 
for (i=0; i < size; i++) 
printf("%d ", arr[i]); 
printf("n"); 
} 
// MAIN METHOD TO EXECUTE INSERTION 
SORT****************************************************************************************
*// 
int main(){ 
int arr[100] = 
{195, 134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162,165, 192,131, 
142, 120, 185, 102, 181, 107, 198, 106, 176, 121 ,178 ,119, 128 ,193 ,127 ,123 ,143, 155 ,186,191, 122 
,132 ,158, 129 ,183, 163, 180 ,103 ,188, 150 ,151, 172 ,118 ,174 ,170, 104, 130, 116, 117,112 ,139 ,194, 
147 ,153, 164, 169 ,199, 148 ,138 ,200 ,190 ,126 ,152 ,161 ,179 ,149 ,137 ,133 ,110,159 ,113, 140 ,160 
,105 ,184 ,182 ,135 ,114 ,125 ,168 ,189 ,124 ,108 ,187 ,166 ,156 ,109 ,167 ,157}; 
//int arr[] = {12, 11, 13, 5, 6}; 
//int arr[] = {29, 10, 14, 37, 6}; 
int arr_size = sizeof(arr)/sizeof(arr[0]); 
printf("GIVEN ARRAY WHICH HAVE TO BE SORTED: nn"); 
printArray(arr, arr_size); 
//PRINT INSERTION 
SORT****************************************************************************************
************// 
printf("nSORTED ARRAY USING INSERTION SORT ALGORITHM IS:nn"); 
insertionSort(arr, arr_size); 
3   
printArray(arr, arr_size); 
//PRINT THE NUMBER OF COMPARISONS AND 
SWAPS********************************************************************************// 
printf("nnNUMBER OF COMPARISONS IS: %dnn", comparsioncounter); 
printf("NUMBER OF SWAP IS : %dn", swapcounter); 
return 0; 
} 
 
OUTPUT: 
Fig :1- Insertion sort in ascending 
 
 
 
4   
 
CODE FOR MERGE SORT WITH COMPARISON & SWAP 
#include<stdlib.h> 
#include<stdio.h> 
//MARGE SORT PROGRAM 
int comparsioncounter=0; 
int swapcounter=0; 
int swapcounter1=0; 
//METHOD FOR MERGE 
SORT********************************************************************************
**************************// 
void merge(int arr[], int l, int m, int r){ 
int i, j, k; 
int n1 = m - l + 1; 
int n2 = r - m; 
//CREATE TEMP ARRAY 
int L[n1], R[n2]; 
/* COPY DATA TO TEMP ARRAYS L[] and R[] */ 
for (i = 0; i < n1; i++) 
L[i] = arr[l + i]; 
for (j = 0; j < n2; j++) 
R[j] = arr[m + 1+ j]; 
/* MARGE THE TEMP ARRAYS BACK INTO ARRAY [l...r]*/ 
5   
i = 0; 
j = 0; 
k = l; 
while (i < n1 && j < n2){ 
if (L[i] <= R[j]){ 
arr[k] = L[i]; 
i++; 
} 
else{ 
arr[k] = R[j]; 
swapcounter++; 
j++; 
} 
comparsioncounter++; 
k++; 
} 
/* COPY THE REMAINING ELEMENTS OF L[],IF THERE ARE ANY */ 
while (i < n1){ 
arr[k] = L[i]; 
i++; 
k++; 
//count3++; 
} 
/* COPY THE REMAINING ELEMENTS OF R[],ANY IF THERE ANY */ 
6   
while (j < n2){ 
arr[k] = R[j]; 
j++; 
k++; 
//count4++; 
} 
//printArray(arr, n); 
//printf("Number of comparsions is %dn", count1+count2+count3+count4); 
//printf("Number of swaps is %dn", swapcount); 
} 
/* l IS FOR LEFT INDEX and r IS FOR RIGHT INDEX OF THE SUB ARRAY OF arr HAVE TO 
BE SORTED */ 
void mergeSort(int arr[], int l, int r){ 
if (l < r){ 
int m = l+(r-l)/2; 
// SORT FIRST & LAST HALVES 
mergeSort(arr, l, m); 
mergeSort(arr, m+1, r); 
merge(arr, l, m, r); 
} 
} 
/* UTILITY FUNCTIONS */ 
/* PRINT ARRAY */ 
void printArray(int A[], int size){ 
7   
int i; 
for (i=0; i < size; i++) 
printf("%d ", A[i]); 
printf("n"); 
} 
//MAIN METHOD 
*************************************************************************************
*******************************// 
int main(){ 
int arr[]= 
{195, 134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162, 165, 
192,131, 142, 120, 185, 102, 181, 107, 198, 106, 176, 121 ,178 ,119, 128 ,193 ,127 ,123 ,143, 
155 ,186,191, 122 ,132 ,158, 129 ,183, 163, 180 ,103 ,188, 150 ,151, 172 ,118 ,174 ,170, 104, 
130, 116, 117,112 ,139 ,194, 147 ,153, 164, 169 ,199, 148 ,138 ,200 ,190 ,126 ,152 ,161 ,179 
,149 ,137 ,133 ,110,159 ,113, 140 ,160 ,105 ,184 ,182 ,135 ,114 ,125 ,168 ,189 ,124 ,108 ,187 
,166 ,156 ,109 ,167 ,157}; 
//int arr[] = {12, 11, 13, 5, 6, 7}; 
//int arr[] = {3,2,1,5,4,8}; 
//int arr[] = {38,27,43,3,9,82,10}; 
int arr_size = sizeof(arr)/sizeof(arr[0]); 
printf("GIVEN ARRAY WHICH HAVE TO BE SORT IS: nn"); 
printArray(arr, arr_size); 
//MARGE SORTING 
METHOD****************************************************************************
******************************// 
mergeSort(arr, 0, arr_size - 1); 
printf("nSORTED ARRAY USING MARGE SORT ALGORITHM IS:nn"); 
8   
printArray(arr, arr_size); 
//PRINT TOTAL COMPARISON AND SWAP 
COUNT******************************************************************************
***********// 
printf("Number of comparsions is %dn", comparsioncounter); 
printf("Number of swap is %dn", swapcounter); 
return 0; 
} 
OUTPUT: 
Fig :2- Merge sort in ascending 
 
 
 
9   
 
 
CODE FOR QUICK SORT WITH COMPARISON & SWAP 
#include<stdio.h> 
//QUICK SORT PROGRAM C CODE 
// FUNCTION SWAP TWO ELEMENTS 
int comparsioncounter=0; 
int swapcounter=0; 
//METHOD FOR SWAP 
*************************************************************************************
**************************// 
void swap(int* a, int* b){ 
int t = *a; 
*a = *b; 
*b = t; 
} 
// THIS FUNCTION TAKES LAST ELEMENTS AS PIVOT ,PLACES THE PIVOT ELEMENT AT 
ITS CORRECT POSITION IN SORTED ARRAY******************// 
//AND PLACES ALL SMALLER [SMALLER THAN PIVOT] TO LEFT OF PIVOT AND ALL 
GREATER ELEMENTS TO RIGHT OF PIVOT************************// 
int partition (int arr[], int low, int high){ 
int pivot = arr[high]; 
int i = (low - 1); 
10   
for (int j = low; j <= high- 1; j++){ 
comparsioncounter++; 
if (arr[j] <= pivot) 
{ 
i++; 
swap(&arr[i], &arr[j]); 
swapcounter++; 
} 
} 
swap(&arr[i + 1], &arr[high]); 
return (i + 1); 
} 
// ARRAY TO BE SORTED, LOW-->STARTING INDEX, HIGH-->ENDING 
INDEX*****************************************************************// 
void quickSort(int arr[], int low, int high){ 
if (low < high){ 
int pi = partition(arr, low, high); 
quickSort(arr, low, pi - 1); 
quickSort(arr, pi + 1, high); 
} 
} 
//PRINT 
ARRAY******************************************************************************
*************************************// 
void printArray(int arr[], int size){ 
11   
for (int i=0; i <=size; i++) 
printf("%d ", arr[i]); 
printf("n"); 
} 
// MAIN 
METHOD****************************************************************************
**************************************// 
int main(){ 
int arr[]= 
{195, 134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162, 165, 
192,131, 142, 120, 185, 102, 181, 107, 198, 106, 176, 121 ,178 ,119, 128 ,193 ,127 ,123 ,143, 
155 ,186,191, 122 ,132 ,158, 129 ,183, 163, 180 ,103 ,188, 150 ,151, 172 ,118 ,174 ,170, 104, 
130, 116, 117,112 ,139 ,194, 147 ,153, 164, 169 ,199, 148 ,138 ,200 ,190 ,126 ,152 ,161 ,179 
,149 ,137 ,133 ,110,159 ,113, 140 ,160 ,105 ,184 ,182 ,135 ,114 ,125 ,168 ,189 ,124 ,108 ,187 
,166 ,156 ,109 ,167 ,157}; 
//int arr[] = {10, 7, 8, 9, 1, 5}; 
//int arr[] = {38,27,43,3,9,82,10}; 
//int arr[] ={10, 80, 30, 90, 40, 50, 70}; 
int arr_size = sizeof(arr)/sizeof(arr[0]); 
printf("GIVEN ARRAY WHICH HAVE TO BE SORTED: nn"); 
printArray(arr, arr_size); 
//QUICK SORT 
METHOD****************************************************************************
*********************************// 
quickSort(arr, 0, arr_size-1); 
printf("QUICK SORTED ARRAY IS : nn"); 
printArray(arr, arr_size); 
12   
//PRINT THE NUMBER OF COMPARISONS AND 
SWAPS******************************************************************************
********// 
printf("nnNUMBER OF COMPARISONS IS: %dnn", comparsioncounter); 
printf("NUMBER OF SWAP IS : %dn", swapcounter); 
return 0; 
} 
 
 
OUTPUT: 
Fig :3- Quick sort in ascending order 
 
 
 
 
13   
 
 
 
 
CODE FOR [101-200] SORT IN ASCENDING ORDER 
#include <stdio.h> 
//PRINT 
ARRAY******************************************************************************
************************************// 
void printArray(int arr[], int size){ 
int i; 
for (i=0; i < size; i++) 
printf("%d ", arr[i]); 
printf("n"); 
} 
//MAIN 
METHOD****************************************************************************
***************************************// 
int main(){ 
int arr[100]= 
{195,134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162, 165, 
192,131, 142, 120, 185, 102, 181, 107, 198, 106, 176, 121 ,178 ,119, 128 ,193 ,127 ,123 ,143, 
155 ,186,191, 122 ,132 ,158, 129 ,183, 163, 180 ,103 ,188, 150 ,151, 172 ,118 ,174 ,170, 104, 
130, 116, 117,112 ,139 ,194, 147 ,153, 164, 169 ,199, 148 ,138 ,200 ,190 ,126 ,152 ,161 ,179 
,149 ,137 ,133 ,110,159 ,113, 140 ,160 ,105 ,184 ,182 ,135 ,114 ,125 ,168 ,189 ,124 ,108 ,187 
,166 ,156 ,109 ,167 ,157}; 
14   
int temp=0; 
int arr_size = sizeof(arr)/sizeof(arr[0]); 
//PRINT THE GIVEN ARRAY 
printf("GIVEN ARRAY WHICH HAVE TO BE SORT IS: n"); 
printArray(arr, arr_size); 
// CODE FOR ASCENDING ORDER OF ARRAY ELEMENT 
for (int i = 0; i < arr_size; i++){ 
for (int j = i+1; j < arr_size; j++){ 
if (arr[j] < arr[i]){ 
int tmp = arr[i]; //Using temporary variable for storing   
arr[i] = arr[j]; //replacing value 
arr[j] = tmp; 
} 
} 
} 
//PRINT ARRAY IN ASCENDING 
ORDER******************************************************************************
*******************// 
printf("nALREADY IN ASCENDING SORT OF THAT ARRAY:n"); 
for(int i=0; i< arr_size; i++){ 
printf("%d ",arr[i]); 
} 
return 0; 
} 
15   
 
 
 
OUTPUT: 
Fig :4- [101-200] array in ascending order 
 
 
 
CODE FOR [200-101] SORT IN DESCENDING ORDER 
#include <stdio.h> 
//PRINT 
ARRAY******************************************************************************
*************************************// 
16   
void printArray(int arr[], int size){ 
int i; 
for (i=0; i < size; i++) 
printf("%d ", arr[i]); 
printf("n"); 
} 
//MAIN 
METHOD****************************************************************************
***************************************// 
int main(){ 
int arr[100]= 
{195, 134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162, 165, 
192,131, 142, 120, 185, 102, 181, 107, 198, 106, 176, 121 ,178 ,119, 128 ,193 ,127 ,123 ,143, 
155 ,186,191, 122 ,132 ,158, 129 ,183, 163, 180 ,103 ,188, 150 ,151, 172 ,118 ,174 ,170, 104, 
130, 116, 117,112 ,139 ,194, 147 ,153, 164, 169 ,199, 148 ,138 ,200 ,190 ,126 ,152 ,161 ,179 
,149 ,137 ,133 ,110,159 ,113, 140 ,160 ,105 ,184 ,182 ,135 ,114 ,125 ,168 ,189 ,124 ,108 ,187 
,166 ,156 ,109 ,167 ,157}; 
int temp=0; 
int arr_size = sizeof(arr)/sizeof(arr[0]); 
//PRINT THE GIVEN 
ARRAY******************************************************************************
***************************// 
printf("GIVEN ARRAY WHICH HAVE TO BE SORT IS: nn"); 
printArray(arr, arr_size); 
// CODE FOR ASCENDING ORDER OF ARRAY ELEMENT 
for (int i = 0; i < arr_size; i++){ 
for (int j = i+1; j < arr_size; j++){ 
17   
if (arr[j] > arr[i]){ 
int tmp = arr[i]; //Using temporary variable for storing 
last value 
arr[i] = arr[j]; //replacing value 
arr[j] = tmp; 
} 
} 
} 
//PRINT ARRAY IN DESCENDING 
ORDER******************************************************************************
*****************// 
printf("nALREADY IN DESCENDING SORT OF THAT ARRAY:n"); 
printf("n"); 
for(int i=0; i< arr_size; i++){ 
printf("%d ",arr[i]); 
} 
printf("n"); 
return 0; 
} 
 
 
 
 
 
18   
OUTPUT: 
Fig :5- [200-101] like descending order 
 
 
CODE FOR [101-200] SORT IN RANDOM ORDER 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
//101-200 SORT USING THE RANDOM FUNCTION 
// FUNCTION TO SWAP THE INTEGER ARRAY ELEMENT to swap to integers 
//METHOD FOR 
SWAP*******************************************************************************
********************************// 
19   
void swap (int *a, int *b){ 
int temp = *a; 
*a = *b; 
*b = temp; 
} 
// PRINT ARRAY 
FUNCTIOKN*************************************************************************
*******************************// 
void printArray (int arr[], int size){ 
for (int i = 0; i < size; i++) 
printf("%d ", arr[i]); 
printf("n"); 
} 
// A FUNCTION TO GENERATE A RANDOM 
NUMBERS***************************************************************************
***********// 
void randomize ( int arr[], int n ){ 
srand ( time(NULL) ); 
for (int i = n-1; i > 0; i--){ 
// PIC A RANDOM INDEX FROM 0 TO i 
int j = rand() % (i+1); 
// SWAP arr[i] WITH THE ELEMENT AT RANDOM INDEXS 
swap(&arr[i], &arr[j]); 
} 
} 
20   
//MAIN 
METHOD****************************************************************************
***************************************// 
int main(){ 
int arr[100] = 
{195, 134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162, 165, 
192,131, 142, 120, 185, 102, 181, 107, 198, 106, 176, 121 ,178 ,119, 128 ,193 ,127 ,123 ,143, 
155 ,186,191, 122 ,132 ,158, 129 ,183, 163, 180 ,103 ,188, 150 ,151, 172 ,118 ,174 ,170, 104, 
130, 116, 117,112 ,139 ,194, 147 ,153, 164, 169 ,199, 148 ,138 ,200 ,190 ,126 ,152 ,161 ,179 
,149 ,137 ,133 ,110,159 ,113, 140 ,160 ,105 ,184 ,182 ,135 ,114 ,125 ,168 ,189 ,124 ,108 ,187 
,166 ,156 ,109 ,167 ,157}; 
//int i; 
int arr_size = sizeof(arr)/sizeof(arr[0]); 
printf("GIVEN ARRAY WHICH HAVE TO BE REARANGE WITH RANDOM: n"); 
printf("n"); 
for(int i = 0; i < arr_size; i++){ 
printf("%d ", arr[i]); 
} 
//PRINT RANDOM ARRAY 
ELEMENT***************************************************************************
*************************// 
printf("n"); 
printf("nSORTED USING RANDOM SORT IS : nn"); 
int n = sizeof(arr)/ sizeof(arr[0]); 
randomize (arr, n); 
printArray(arr, n); 
printf("n"); 
21   
return 0; 
} 
 
 
OUTPUT: 
Fig :6- [101-200] array sort in random order 
 
 
 
 
 
 
 
22   
MATERIALS 
1. Code Block IDE Tools 
2. GDB/CDB debugger 
3. IntelliJ IDE/Netbeans IDE 
4. JDK 
PROCEDURE 
1. C Programming 
2. C++ Programming 
3. Java Programming 
 
OUTPUT DATA REPORT FOR TOTAL COMPARISONS & SWAPS COUNT: 
ALGORITHM  NO OF COMPARISONS  NO OF SWAPS 
Insertion Sort     
Merge Sort     
Quick Sort     
RESULTS 
We have used C programming language for that assignment completions. Which has                       
given the ultimate result for Insertion Sort, Merge Sort & Quick Sort using their algorithm                             
& pseudo codes. After that we have sorted the given input array [101-200] in Ascending                             
,Descending & also Randomize options too. 
 
23   
CONCLUSION 
 
1. Insert sort is more efficient than bubble sort and selection sort.In this algo we 
divide the entire array into parts; the sorted array and the unsorted array.With 
every iteration we have to place the first element of the unsorted array in the 
correct position of the sorted array and increase the sorted list by one. 
Time Complexity: 
when elements are sorted there are no swaps and the correct position of the                           
element in the sorted list is the current index itself.The time complexity is : O(n) 
Insertion sort gets penalized if comparison or copying is slow. In other words, the                           
maximum array size which is faster to sort with insertion sort compared to                         
O(nlogn) algorithms gets smaller if comparison or copying of the array elements is                         
slow. 
Divide and Conquer Approach- 
In this approach the algorithm is divide into multiple sub-problems.Each of these                       
sub-problems is then solved separately and the solution of each sub-problem is                       
used to solve the original problem. 
Divide and conquer technique uses recursion to solve each of the sub-problem. 
 
 
 
 
 
 
 
 
24   
Fig : Insertion Sort Time Complexity 
 
2. Merge sort uses the divide and conquer approach.It is one of the most efficient                           
algo for sorting.In this algo,we divide the list into two from the middle and keep                             
dividing the list until the sub-problems has only one element list. 
We then merge the list and while merging the list we sort them in the ascending                               
order. 
Time complexity: 
We are dividing the list into no matter if the list is sorted or no.But if the array is                                     
sorted,while merging the list there are no swaps merging results into an array                         
itself.Thus, the best ,average and worst case time complexity is: O(nlogn) 
Surprisingly fast, at least with the optimizations used in this test (ie. the sorting                           
function doesn't need to allocate the secondary array each time it is called). Given                           
that it is always O(nlogn), it is a very good alternative if the extra memory                             
requirement is not a problem. 
Array elements with fast comparisons and slow copying seem to slightly penalize                       
merge sort. 
25   
Fig: Merge Sort Time Complexity 
 
3. Quick sort uses the similar approach of divide and conquer technique 
In this technique, element is selected which is the pivot element.Now the element                         
which are less than the pivot are placed to the left of the array and the element                                 
which are more than the pivot are placed to the right of the pivot.The index of the                                 
pivot element is then returned back to the function.The same function is called to                           
the sub-array left to the pivot which has all the elements less than the pivot and                               
also to the right of the sub-array which has the elements more than the pivot. 
After calling the function recursively,the resulting function will be a sorted array. 
Time complexity: 
The best case is when the elements are in a sorted manner. The best and average                               
case time complexity is : O(nlogn) 
The worst case time complexity is when the elements are in a reverse sorted                           
manner.The time complexity is :O(n2) 
In this Quick Sort,the last element in the list is taken as the pivot element.This                             
Quick Sort is a bit slow as compared to other approaches of Quick Sort. 
26   
 
Fig: Quick Sort Time Complexity 
 
4. For Random sort approach,Most of the times this is going to be the approach                           
since we are going to sort random number elements 
Other sort,should never be employed.They take hours to sort a million                     
elements.We can see that by the readings provided in the zip file of the                           
assignment 5 folder. 
Merge sort performs very well for this sort.It should be employed. 
Also the quick sort algorithm performs very well for a million elements.All three                         
approaches of quick sort are pretty fast and any of them can be                         
employed.However,if one requires very good efficiency,he should use Quicksort                 
pivot-median approach. 
 
 
 
27   
 
Fig: Random Sort Time Complexity 
 
REFERENCES 
1. Class Lectures & pseudo-codes 
2. Stack Overflow 
3. GeeksforGeeks 
4. Other Blog Sites 
5. Books Help 
 
28   

More Related Content

PDF
A tarde13 de_agosto_de_2016_a_tardepag3
PDF
Lataif e ashrafi malfoozat e syed makhdoom ashraf 41
PDF
Seerat unnabi - Mufti Zia ud Din - Abul Hasanat Research Academy
PDF
#include iostream using namespace std; void InsertionSort(int .pdf
PDF
COSTEP Student Loan Brochure
PDF
codes.txt.pdf code presentation engineering
A tarde13 de_agosto_de_2016_a_tardepag3
Lataif e ashrafi malfoozat e syed makhdoom ashraf 41
Seerat unnabi - Mufti Zia ud Din - Abul Hasanat Research Academy
#include iostream using namespace std; void InsertionSort(int .pdf
COSTEP Student Loan Brochure
codes.txt.pdf code presentation engineering

Similar to Home workassignment 1 report google docs (20)

PPTX
C++ programming pattern
PDF
check the modifed code now you will get all operations done.termin.pdf
PPTX
Nested For Loops and Class Constants in Java
PDF
Program of sorting using shell sort #include stdio.h #de.pdf
PDF
Mtk khoirul bab 1
PPT
C++ programming
PDF
PDF
I have written the code but cannot complete the assignment please help.pdf
PDF
i am using C++ codingsource coding Program that sorts an arra.pdf
DOCX
Network lap pgms 7th semester
PDF
#include iostream using namespace std; class Array { priva.pdf
PDF
Insertion_Sort_Code.pdf
PPT
SP-First-Lecture.ppt
PDF
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
PDF
Janusz Korczak - wykaz książek i artykułów w zbiorach BP Cieszyn
PPTX
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
PDF
oodp elab.pdf
PDF
C programming.   For this code I only need to add a function so th.pdf
C++ programming pattern
check the modifed code now you will get all operations done.termin.pdf
Nested For Loops and Class Constants in Java
Program of sorting using shell sort #include stdio.h #de.pdf
Mtk khoirul bab 1
C++ programming
I have written the code but cannot complete the assignment please help.pdf
i am using C++ codingsource coding Program that sorts an arra.pdf
Network lap pgms 7th semester
#include iostream using namespace std; class Array { priva.pdf
Insertion_Sort_Code.pdf
SP-First-Lecture.ppt
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
Janusz Korczak - wykaz książek i artykułów w zbiorach BP Cieszyn
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
oodp elab.pdf
C programming.   For this code I only need to add a function so th.pdf
Ad

More from LITS IT Ltd,LASRC.SPACE,SAWDAGOR BD,FREELANCE BD,iREV,BD LAW ACADEMY,SMART AVI,HEA,HFSAC LTD. (20)

DOCX
PDF
লালমনিরহাট ৩ আসনের ভোট কেন্দ্র সমুহ - LALMONIRHAT 3 SADAR VOTE CENTER ALL
PDF
লালমনিরহাট ৩ আসনের ওয়ার্ড সমূহ - LALMONIRHAT 3 WARD LIST ALL
PDF
ভূমি সংশ্লিষ্ট অপরাধ প্রতিকার ও প্রতিরোধ ৩৬ নং আইন, ২০২৩,
PDF
The Bangladesh Legal Practitioner_s and Bar Council Order, 1972.pdf
PDF
Programming Your Home Automate with Arduino, Android, and Your Computer.pdf
DOCX
বিবিসি রুলেস ও অরডার, ১৯৭২ বেয়ার এক্ট -BBC BARE ACT.docx
লালমনিরহাট ৩ আসনের ভোট কেন্দ্র সমুহ - LALMONIRHAT 3 SADAR VOTE CENTER ALL
লালমনিরহাট ৩ আসনের ওয়ার্ড সমূহ - LALMONIRHAT 3 WARD LIST ALL
ভূমি সংশ্লিষ্ট অপরাধ প্রতিকার ও প্রতিরোধ ৩৬ নং আইন, ২০২৩,
The Bangladesh Legal Practitioner_s and Bar Council Order, 1972.pdf
Programming Your Home Automate with Arduino, Android, and Your Computer.pdf
বিবিসি রুলেস ও অরডার, ১৯৭২ বেয়ার এক্ট -BBC BARE ACT.docx
Ad

Recently uploaded (20)

PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Well-logging-methods_new................
PDF
Digital Logic Computer Design lecture notes
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPT
introduction to datamining and warehousing
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
PPT on Performance Review to get promotions
DOCX
573137875-Attendance-Management-System-original
PPT
Project quality management in manufacturing
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
additive manufacturing of ss316l using mig welding
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Geodesy 1.pptx...............................................
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Operating System & Kernel Study Guide-1 - converted.pdf
UNIT 4 Total Quality Management .pptx
Well-logging-methods_new................
Digital Logic Computer Design lecture notes
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
introduction to datamining and warehousing
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPT on Performance Review to get promotions
573137875-Attendance-Management-System-original
Project quality management in manufacturing
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Lecture Notes Electrical Wiring System Components
additive manufacturing of ss316l using mig welding
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Geodesy 1.pptx...............................................
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...

Home workassignment 1 report google docs

  • 1. Homework Assignment 1  ICT:5102, Data Structure & Algorithm    Md.Dedarul Hasan  PGD in IT, Registration No: 0417311011  Session: April,2017           
  • 2. INTRODUCTION OF TASK TO DO  Insertion Sort, Merge Sort & Quick Sort Algorithms.Total Comparison & Swap Count                        During Sort Proces. Array Sorting By Ascending, Descending & Random Order                      Implementations.  GIVEN INPUT ARRAY  {195, 134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162,165,  192,131, 142, 120, 185, 102, 181, 107, 198, 106, 176, 121, 178, 119, 128, 193, 127, 123, 143,  155, 186,191, 122, 132, 158, 129, 183, 163, 180, 103, 188, 150, 151, 172, 118, 174, 170, 104,  130, 116, 117,112, 139, 194, 147, 153, 164, 169, 199, 148, 138, 200, 190, 126, 152, 161, 179,  149, 137, 133, 110,159, 113, 140, 160, 105, 184, 182, 135, 114, 125, 168, 189, 124, 108, 187,  166, 156, 109, 167, 157}                  1   
  • 3. CODE FOR INSERTION SORT WITH COMPARISON & SWAP  #include <stdio.h>  #include <math.h>  int comparsioncounter=0;  int swapcounter=0;  //METHOD FOR INSERTION  SORT**************************************************************************************** *************//  void insertionSort(int arr[], int size){  int i, key, j;  for (i = 1; i <size; i++){  comparsioncounter++;  key = arr[i];  j = i-1;  while (j >= 0 && arr[j] > key)  {  comparsioncounter++;  arr[j+1] = arr[j];  swapcounter++;  j = j-1;  }  arr[j+1] = key;  swapcounter++;  }  }  2   
  • 4. //METHOD TO PRINT AN ARRAY OF SIZE n  ********************************************************************************************//  void printArray(int arr[], int size){  int i;  for (i=0; i < size; i++)  printf("%d ", arr[i]);  printf("n");  }  // MAIN METHOD TO EXECUTE INSERTION  SORT**************************************************************************************** *//  int main(){  int arr[100] =  {195, 134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162,165, 192,131,  142, 120, 185, 102, 181, 107, 198, 106, 176, 121 ,178 ,119, 128 ,193 ,127 ,123 ,143, 155 ,186,191, 122  ,132 ,158, 129 ,183, 163, 180 ,103 ,188, 150 ,151, 172 ,118 ,174 ,170, 104, 130, 116, 117,112 ,139 ,194,  147 ,153, 164, 169 ,199, 148 ,138 ,200 ,190 ,126 ,152 ,161 ,179 ,149 ,137 ,133 ,110,159 ,113, 140 ,160  ,105 ,184 ,182 ,135 ,114 ,125 ,168 ,189 ,124 ,108 ,187 ,166 ,156 ,109 ,167 ,157};  //int arr[] = {12, 11, 13, 5, 6};  //int arr[] = {29, 10, 14, 37, 6};  int arr_size = sizeof(arr)/sizeof(arr[0]);  printf("GIVEN ARRAY WHICH HAVE TO BE SORTED: nn");  printArray(arr, arr_size);  //PRINT INSERTION  SORT**************************************************************************************** ************//  printf("nSORTED ARRAY USING INSERTION SORT ALGORITHM IS:nn");  insertionSort(arr, arr_size);  3   
  • 5. printArray(arr, arr_size);  //PRINT THE NUMBER OF COMPARISONS AND  SWAPS********************************************************************************//  printf("nnNUMBER OF COMPARISONS IS: %dnn", comparsioncounter);  printf("NUMBER OF SWAP IS : %dn", swapcounter);  return 0;  }    OUTPUT:  Fig :1- Insertion sort in ascending        4   
  • 6.   CODE FOR MERGE SORT WITH COMPARISON & SWAP  #include<stdlib.h>  #include<stdio.h>  //MARGE SORT PROGRAM  int comparsioncounter=0;  int swapcounter=0;  int swapcounter1=0;  //METHOD FOR MERGE  SORT******************************************************************************** **************************//  void merge(int arr[], int l, int m, int r){  int i, j, k;  int n1 = m - l + 1;  int n2 = r - m;  //CREATE TEMP ARRAY  int L[n1], R[n2];  /* COPY DATA TO TEMP ARRAYS L[] and R[] */  for (i = 0; i < n1; i++)  L[i] = arr[l + i];  for (j = 0; j < n2; j++)  R[j] = arr[m + 1+ j];  /* MARGE THE TEMP ARRAYS BACK INTO ARRAY [l...r]*/  5   
  • 7. i = 0;  j = 0;  k = l;  while (i < n1 && j < n2){  if (L[i] <= R[j]){  arr[k] = L[i];  i++;  }  else{  arr[k] = R[j];  swapcounter++;  j++;  }  comparsioncounter++;  k++;  }  /* COPY THE REMAINING ELEMENTS OF L[],IF THERE ARE ANY */  while (i < n1){  arr[k] = L[i];  i++;  k++;  //count3++;  }  /* COPY THE REMAINING ELEMENTS OF R[],ANY IF THERE ANY */  6   
  • 8. while (j < n2){  arr[k] = R[j];  j++;  k++;  //count4++;  }  //printArray(arr, n);  //printf("Number of comparsions is %dn", count1+count2+count3+count4);  //printf("Number of swaps is %dn", swapcount);  }  /* l IS FOR LEFT INDEX and r IS FOR RIGHT INDEX OF THE SUB ARRAY OF arr HAVE TO  BE SORTED */  void mergeSort(int arr[], int l, int r){  if (l < r){  int m = l+(r-l)/2;  // SORT FIRST & LAST HALVES  mergeSort(arr, l, m);  mergeSort(arr, m+1, r);  merge(arr, l, m, r);  }  }  /* UTILITY FUNCTIONS */  /* PRINT ARRAY */  void printArray(int A[], int size){  7   
  • 9. int i;  for (i=0; i < size; i++)  printf("%d ", A[i]);  printf("n");  }  //MAIN METHOD  ************************************************************************************* *******************************//  int main(){  int arr[]=  {195, 134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162, 165,  192,131, 142, 120, 185, 102, 181, 107, 198, 106, 176, 121 ,178 ,119, 128 ,193 ,127 ,123 ,143,  155 ,186,191, 122 ,132 ,158, 129 ,183, 163, 180 ,103 ,188, 150 ,151, 172 ,118 ,174 ,170, 104,  130, 116, 117,112 ,139 ,194, 147 ,153, 164, 169 ,199, 148 ,138 ,200 ,190 ,126 ,152 ,161 ,179  ,149 ,137 ,133 ,110,159 ,113, 140 ,160 ,105 ,184 ,182 ,135 ,114 ,125 ,168 ,189 ,124 ,108 ,187  ,166 ,156 ,109 ,167 ,157};  //int arr[] = {12, 11, 13, 5, 6, 7};  //int arr[] = {3,2,1,5,4,8};  //int arr[] = {38,27,43,3,9,82,10};  int arr_size = sizeof(arr)/sizeof(arr[0]);  printf("GIVEN ARRAY WHICH HAVE TO BE SORT IS: nn");  printArray(arr, arr_size);  //MARGE SORTING  METHOD**************************************************************************** ******************************//  mergeSort(arr, 0, arr_size - 1);  printf("nSORTED ARRAY USING MARGE SORT ALGORITHM IS:nn");  8   
  • 10. printArray(arr, arr_size);  //PRINT TOTAL COMPARISON AND SWAP  COUNT****************************************************************************** ***********//  printf("Number of comparsions is %dn", comparsioncounter);  printf("Number of swap is %dn", swapcounter);  return 0;  }  OUTPUT:  Fig :2- Merge sort in ascending        9   
  • 11.     CODE FOR QUICK SORT WITH COMPARISON & SWAP  #include<stdio.h>  //QUICK SORT PROGRAM C CODE  // FUNCTION SWAP TWO ELEMENTS  int comparsioncounter=0;  int swapcounter=0;  //METHOD FOR SWAP  ************************************************************************************* **************************//  void swap(int* a, int* b){  int t = *a;  *a = *b;  *b = t;  }  // THIS FUNCTION TAKES LAST ELEMENTS AS PIVOT ,PLACES THE PIVOT ELEMENT AT  ITS CORRECT POSITION IN SORTED ARRAY******************//  //AND PLACES ALL SMALLER [SMALLER THAN PIVOT] TO LEFT OF PIVOT AND ALL  GREATER ELEMENTS TO RIGHT OF PIVOT************************//  int partition (int arr[], int low, int high){  int pivot = arr[high];  int i = (low - 1);  10   
  • 12. for (int j = low; j <= high- 1; j++){  comparsioncounter++;  if (arr[j] <= pivot)  {  i++;  swap(&arr[i], &arr[j]);  swapcounter++;  }  }  swap(&arr[i + 1], &arr[high]);  return (i + 1);  }  // ARRAY TO BE SORTED, LOW-->STARTING INDEX, HIGH-->ENDING  INDEX*****************************************************************//  void quickSort(int arr[], int low, int high){  if (low < high){  int pi = partition(arr, low, high);  quickSort(arr, low, pi - 1);  quickSort(arr, pi + 1, high);  }  }  //PRINT  ARRAY****************************************************************************** *************************************//  void printArray(int arr[], int size){  11   
  • 13. for (int i=0; i <=size; i++)  printf("%d ", arr[i]);  printf("n");  }  // MAIN  METHOD**************************************************************************** **************************************//  int main(){  int arr[]=  {195, 134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162, 165,  192,131, 142, 120, 185, 102, 181, 107, 198, 106, 176, 121 ,178 ,119, 128 ,193 ,127 ,123 ,143,  155 ,186,191, 122 ,132 ,158, 129 ,183, 163, 180 ,103 ,188, 150 ,151, 172 ,118 ,174 ,170, 104,  130, 116, 117,112 ,139 ,194, 147 ,153, 164, 169 ,199, 148 ,138 ,200 ,190 ,126 ,152 ,161 ,179  ,149 ,137 ,133 ,110,159 ,113, 140 ,160 ,105 ,184 ,182 ,135 ,114 ,125 ,168 ,189 ,124 ,108 ,187  ,166 ,156 ,109 ,167 ,157};  //int arr[] = {10, 7, 8, 9, 1, 5};  //int arr[] = {38,27,43,3,9,82,10};  //int arr[] ={10, 80, 30, 90, 40, 50, 70};  int arr_size = sizeof(arr)/sizeof(arr[0]);  printf("GIVEN ARRAY WHICH HAVE TO BE SORTED: nn");  printArray(arr, arr_size);  //QUICK SORT  METHOD**************************************************************************** *********************************//  quickSort(arr, 0, arr_size-1);  printf("QUICK SORTED ARRAY IS : nn");  printArray(arr, arr_size);  12   
  • 14. //PRINT THE NUMBER OF COMPARISONS AND  SWAPS****************************************************************************** ********//  printf("nnNUMBER OF COMPARISONS IS: %dnn", comparsioncounter);  printf("NUMBER OF SWAP IS : %dn", swapcounter);  return 0;  }      OUTPUT:  Fig :3- Quick sort in ascending order          13   
  • 15.         CODE FOR [101-200] SORT IN ASCENDING ORDER  #include <stdio.h>  //PRINT  ARRAY****************************************************************************** ************************************//  void printArray(int arr[], int size){  int i;  for (i=0; i < size; i++)  printf("%d ", arr[i]);  printf("n");  }  //MAIN  METHOD**************************************************************************** ***************************************//  int main(){  int arr[100]=  {195,134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162, 165,  192,131, 142, 120, 185, 102, 181, 107, 198, 106, 176, 121 ,178 ,119, 128 ,193 ,127 ,123 ,143,  155 ,186,191, 122 ,132 ,158, 129 ,183, 163, 180 ,103 ,188, 150 ,151, 172 ,118 ,174 ,170, 104,  130, 116, 117,112 ,139 ,194, 147 ,153, 164, 169 ,199, 148 ,138 ,200 ,190 ,126 ,152 ,161 ,179  ,149 ,137 ,133 ,110,159 ,113, 140 ,160 ,105 ,184 ,182 ,135 ,114 ,125 ,168 ,189 ,124 ,108 ,187  ,166 ,156 ,109 ,167 ,157};  14   
  • 16. int temp=0;  int arr_size = sizeof(arr)/sizeof(arr[0]);  //PRINT THE GIVEN ARRAY  printf("GIVEN ARRAY WHICH HAVE TO BE SORT IS: n");  printArray(arr, arr_size);  // CODE FOR ASCENDING ORDER OF ARRAY ELEMENT  for (int i = 0; i < arr_size; i++){  for (int j = i+1; j < arr_size; j++){  if (arr[j] < arr[i]){  int tmp = arr[i]; //Using temporary variable for storing    arr[i] = arr[j]; //replacing value  arr[j] = tmp;  }  }  }  //PRINT ARRAY IN ASCENDING  ORDER****************************************************************************** *******************//  printf("nALREADY IN ASCENDING SORT OF THAT ARRAY:n");  for(int i=0; i< arr_size; i++){  printf("%d ",arr[i]);  }  return 0;  }  15   
  • 17.       OUTPUT:  Fig :4- [101-200] array in ascending order        CODE FOR [200-101] SORT IN DESCENDING ORDER  #include <stdio.h>  //PRINT  ARRAY****************************************************************************** *************************************//  16   
  • 18. void printArray(int arr[], int size){  int i;  for (i=0; i < size; i++)  printf("%d ", arr[i]);  printf("n");  }  //MAIN  METHOD**************************************************************************** ***************************************//  int main(){  int arr[100]=  {195, 134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162, 165,  192,131, 142, 120, 185, 102, 181, 107, 198, 106, 176, 121 ,178 ,119, 128 ,193 ,127 ,123 ,143,  155 ,186,191, 122 ,132 ,158, 129 ,183, 163, 180 ,103 ,188, 150 ,151, 172 ,118 ,174 ,170, 104,  130, 116, 117,112 ,139 ,194, 147 ,153, 164, 169 ,199, 148 ,138 ,200 ,190 ,126 ,152 ,161 ,179  ,149 ,137 ,133 ,110,159 ,113, 140 ,160 ,105 ,184 ,182 ,135 ,114 ,125 ,168 ,189 ,124 ,108 ,187  ,166 ,156 ,109 ,167 ,157};  int temp=0;  int arr_size = sizeof(arr)/sizeof(arr[0]);  //PRINT THE GIVEN  ARRAY****************************************************************************** ***************************//  printf("GIVEN ARRAY WHICH HAVE TO BE SORT IS: nn");  printArray(arr, arr_size);  // CODE FOR ASCENDING ORDER OF ARRAY ELEMENT  for (int i = 0; i < arr_size; i++){  for (int j = i+1; j < arr_size; j++){  17   
  • 19. if (arr[j] > arr[i]){  int tmp = arr[i]; //Using temporary variable for storing  last value  arr[i] = arr[j]; //replacing value  arr[j] = tmp;  }  }  }  //PRINT ARRAY IN DESCENDING  ORDER****************************************************************************** *****************//  printf("nALREADY IN DESCENDING SORT OF THAT ARRAY:n");  printf("n");  for(int i=0; i< arr_size; i++){  printf("%d ",arr[i]);  }  printf("n");  return 0;  }            18   
  • 20. OUTPUT:  Fig :5- [200-101] like descending order      CODE FOR [101-200] SORT IN RANDOM ORDER  #include <stdio.h>  #include <stdlib.h>  #include <time.h>  //101-200 SORT USING THE RANDOM FUNCTION  // FUNCTION TO SWAP THE INTEGER ARRAY ELEMENT to swap to integers  //METHOD FOR  SWAP******************************************************************************* ********************************//  19   
  • 21. void swap (int *a, int *b){  int temp = *a;  *a = *b;  *b = temp;  }  // PRINT ARRAY  FUNCTIOKN************************************************************************* *******************************//  void printArray (int arr[], int size){  for (int i = 0; i < size; i++)  printf("%d ", arr[i]);  printf("n");  }  // A FUNCTION TO GENERATE A RANDOM  NUMBERS*************************************************************************** ***********//  void randomize ( int arr[], int n ){  srand ( time(NULL) );  for (int i = n-1; i > 0; i--){  // PIC A RANDOM INDEX FROM 0 TO i  int j = rand() % (i+1);  // SWAP arr[i] WITH THE ELEMENT AT RANDOM INDEXS  swap(&arr[i], &arr[j]);  }  }  20   
  • 22. //MAIN  METHOD**************************************************************************** ***************************************//  int main(){  int arr[100] =  {195, 134, 144, 141, 145, 197, 177, 101, 196, 146, 175, 173, 154, 171, 111, 136, 115, 162, 165,  192,131, 142, 120, 185, 102, 181, 107, 198, 106, 176, 121 ,178 ,119, 128 ,193 ,127 ,123 ,143,  155 ,186,191, 122 ,132 ,158, 129 ,183, 163, 180 ,103 ,188, 150 ,151, 172 ,118 ,174 ,170, 104,  130, 116, 117,112 ,139 ,194, 147 ,153, 164, 169 ,199, 148 ,138 ,200 ,190 ,126 ,152 ,161 ,179  ,149 ,137 ,133 ,110,159 ,113, 140 ,160 ,105 ,184 ,182 ,135 ,114 ,125 ,168 ,189 ,124 ,108 ,187  ,166 ,156 ,109 ,167 ,157};  //int i;  int arr_size = sizeof(arr)/sizeof(arr[0]);  printf("GIVEN ARRAY WHICH HAVE TO BE REARANGE WITH RANDOM: n");  printf("n");  for(int i = 0; i < arr_size; i++){  printf("%d ", arr[i]);  }  //PRINT RANDOM ARRAY  ELEMENT*************************************************************************** *************************//  printf("n");  printf("nSORTED USING RANDOM SORT IS : nn");  int n = sizeof(arr)/ sizeof(arr[0]);  randomize (arr, n);  printArray(arr, n);  printf("n");  21   
  • 23. return 0;  }      OUTPUT:  Fig :6- [101-200] array sort in random order                22   
  • 24. MATERIALS  1. Code Block IDE Tools  2. GDB/CDB debugger  3. IntelliJ IDE/Netbeans IDE  4. JDK  PROCEDURE  1. C Programming  2. C++ Programming  3. Java Programming    OUTPUT DATA REPORT FOR TOTAL COMPARISONS & SWAPS COUNT:  ALGORITHM  NO OF COMPARISONS  NO OF SWAPS  Insertion Sort      Merge Sort      Quick Sort      RESULTS  We have used C programming language for that assignment completions. Which has                        given the ultimate result for Insertion Sort, Merge Sort & Quick Sort using their algorithm                              & pseudo codes. After that we have sorted the given input array [101-200] in Ascending                              ,Descending & also Randomize options too.    23   
  • 25. CONCLUSION    1. Insert sort is more efficient than bubble sort and selection sort.In this algo we  divide the entire array into parts; the sorted array and the unsorted array.With  every iteration we have to place the first element of the unsorted array in the  correct position of the sorted array and increase the sorted list by one.  Time Complexity:  when elements are sorted there are no swaps and the correct position of the                            element in the sorted list is the current index itself.The time complexity is : O(n)  Insertion sort gets penalized if comparison or copying is slow. In other words, the                            maximum array size which is faster to sort with insertion sort compared to                          O(nlogn) algorithms gets smaller if comparison or copying of the array elements is                          slow.  Divide and Conquer Approach-  In this approach the algorithm is divide into multiple sub-problems.Each of these                        sub-problems is then solved separately and the solution of each sub-problem is                        used to solve the original problem.  Divide and conquer technique uses recursion to solve each of the sub-problem.                  24   
  • 26. Fig : Insertion Sort Time Complexity    2. Merge sort uses the divide and conquer approach.It is one of the most efficient                            algo for sorting.In this algo,we divide the list into two from the middle and keep                              dividing the list until the sub-problems has only one element list.  We then merge the list and while merging the list we sort them in the ascending                                order.  Time complexity:  We are dividing the list into no matter if the list is sorted or no.But if the array is                                      sorted,while merging the list there are no swaps merging results into an array                          itself.Thus, the best ,average and worst case time complexity is: O(nlogn)  Surprisingly fast, at least with the optimizations used in this test (ie. the sorting                            function doesn't need to allocate the secondary array each time it is called). Given                            that it is always O(nlogn), it is a very good alternative if the extra memory                              requirement is not a problem.  Array elements with fast comparisons and slow copying seem to slightly penalize                        merge sort.  25   
  • 27. Fig: Merge Sort Time Complexity    3. Quick sort uses the similar approach of divide and conquer technique  In this technique, element is selected which is the pivot element.Now the element                          which are less than the pivot are placed to the left of the array and the element                                  which are more than the pivot are placed to the right of the pivot.The index of the                                  pivot element is then returned back to the function.The same function is called to                            the sub-array left to the pivot which has all the elements less than the pivot and                                also to the right of the sub-array which has the elements more than the pivot.  After calling the function recursively,the resulting function will be a sorted array.  Time complexity:  The best case is when the elements are in a sorted manner. The best and average                                case time complexity is : O(nlogn)  The worst case time complexity is when the elements are in a reverse sorted                            manner.The time complexity is :O(n2)  In this Quick Sort,the last element in the list is taken as the pivot element.This                              Quick Sort is a bit slow as compared to other approaches of Quick Sort.  26   
  • 28.   Fig: Quick Sort Time Complexity    4. For Random sort approach,Most of the times this is going to be the approach                            since we are going to sort random number elements  Other sort,should never be employed.They take hours to sort a million                      elements.We can see that by the readings provided in the zip file of the                            assignment 5 folder.  Merge sort performs very well for this sort.It should be employed.  Also the quick sort algorithm performs very well for a million elements.All three                          approaches of quick sort are pretty fast and any of them can be                          employed.However,if one requires very good efficiency,he should use Quicksort                  pivot-median approach.        27   
  • 29.   Fig: Random Sort Time Complexity    REFERENCES  1. Class Lectures & pseudo-codes  2. Stack Overflow  3. GeeksforGeeks  4. Other Blog Sites  5. Books Help    28