You are on page 1of 21

NAME: Ashfaq Ahmed

ID:19304025

COURSE CODE: 1303(Data structure and algorithm)

Faculty: Shah Fahad Ahmed Reza (HEAD OF CSE DEAPARTMENT)

Department of Computer Science and Engineering


INTRODUCTION

In this assignment I am going to complete three tasks. 1st task I am going to complete is all about abstract knowledge types
like int, double, float, bool, and view computer memory understanding the relationship between these data types. Second
task I am going to design a pseudo-code, and then write a program in c programming language. Generate an array of 1,000
random integers. Connect three methods to the merge form, insert sort , quickly sort, merge sort to be used on this array.
Time each algorithm and compare it Times. Draw the time graph versus the size of the array by varying the elements of the
array. Array from 10 to 1000 in steps of 100 for algorithms and a shape comment. Give a analysis in comparison to the
output of every algorithm. Third task I am going to show how radix sort is used to sort the numbers for the following
numbers-- 170, 45, 69, 80, 96, 55, 172, 356, 8, 5 demonstrate step-by - step radix activity.
Task no 1

Discuss abstract data types such as int, double, float, bool and show your understanding of the
relationship between these data types and a computer memory

INT:
Integers are integer numbers that can be both zero, positive and negative, but not decimal. For instance, 0,-
5,10We may use int to declare a variable integer.[2]

• Data form integer allows the number values of the variable to be stored.

• The keyword "int" refers to the data type of the integer.

• The int data type storage sizes are 2 or 4 or 8 bytes.

• Depending on the CPU processor we use, the frequency is different. If a 16-bit processor is used, the
data form is allocated 2 bytes (16-bit) of memory.

• Four bytes (32 bit) of memory is also allocated for int datatype for 32 bit processor and eight bytes of
memory (64 bit) for 63 bit processor.

Int (2 bytes) can store -32,768 to +32,767 values

• int (4 byte) can save the -2,147,483,648 to +2,147,483,647 values.

• You can go to "long int" and "long int" where the limits are very high if you want to use the integer
that crosses the above limit. [1]

Specifiers of formats in ints are either%d or%i for printing or scanning.[3]

FLOAT:

Data type of floating point allows the user to select decimal values. The average points, for example, may be
97.665. It is going to strip the decimal part off and just print 97 if we use int data sort. We need 'float' data type
to print the exact value. [1]

We can print the value using percent f, and Float is 4 bytes.

Int values can also be found in Float.

• The floating data type can store decimal values for a variable.
• Float data type storage size is 4. This varies also according to the "int" type of data processor in the
CPU.

• Up to 6 digits may be used by float data type after decimal use.

• In a variable with float data type , for example, 10.456789 may be saved.

• Float type is the most fundamental type of float point number.

• A single precision floating point number, consisting of: according to the IEEE standard, is exactly
32bits long;

• a little to display the message.

• 23 bits (included) for the 24-bit integer recording of the digits. This can be achieved at approximately 6
or 7 decimal digits.

• 8 bits to transfer the digits left or right for the binary exponent. This works to a maximum range of
approximately 10 ^ -38 to 10 ^ 38. [3]

DOUBLE:
You can think about float, twice and twice as long as the int, int, and int. Double is 8 bits, so you can be more
precise than float. This is useful for precise science programs. Float is only a type of data with one precision;
double is a type with double precision. For twice the same as other compilers, however, the quadruple data
precision is achieved. [1]

• Twice more bits are used than a float by the double-precision data form which yield about twice the
precise number of digits.

• The double precision floating point number, according to the IEEE standard, is 64 bits long, consisting
of:

o one sign bit. 

o 52 bits (plus one implied) for digit recording, operating to around fifteen decimal digits of
accuracy.

o 11 bits for the exhibitors, which runs from 10 ^ -308 to 10 ^ 308 approximately

• For the majority of scientific and technical calculations the dual data type is the preferred floating-
point type. [3]

BOOLEAN:
A boolean is a type of data that can be stored true or false in the C Standard Library. Each value of non-zero
matches true while 0 matches false.[5]

• A true boolean data type would have only two legal values – "true" and "fake" – to store logical values.
• C has no boolean data type and uses boolean testing integers as a rule.

o Zero is used to portray false, and One to portray true.

o Zero is interpreted as false and anything other than null is interpreted as true for interpretation.

o C Programmer defines the terms "true" and "false" to include values 1 and 0, respectively, to
make life easier.

• This used to be achieved by # define in the old days:

Down # define real 1

# define incorrect 0 0

• Today, rather than # define, it is easier to use const int:

= 1 for the const;

= 0; int incorrect;

• C99 gives a new header, stdbool.h, which defines some new and helpful styles:

— The "Bool" type is equivalent to " Bool" type recently described.

Bool is an unsigned integer only attributable to the values of 0 or 1

To try to save anything else in Bool stores a 1. (Remember that C considers any other non-zero real, i.e.
1)

· Today, the variables form "bool" may be declared.

• also stdbool.h defines the "true" and "false" constants, with values of 1 and 0.

• Use # include < stdbool.h > at the top of your program to use these new features.[4]
Task no 2

Design pseudo-code and then write a program in any programming language that generates an array of
1,000 random integers. Add three methods for merge sort, insertion sort and quick sort to use on this
array. Time each algorithm and compare the times. Draw a graph of time versus size of the array by
varying the elements of the array from 10 to a 1000 in steps of 100 for the algorithms and comment of the
shape of the graph in relation to efficiency of each of the algorithms.

Pseudocode:

Insertion Sort:

/* sorting element in ascending order */


int i, j, v, List[1000];
/* start with the 2nd element */
For loop ( i from 1 to less than n ){
v = List[i]; // store the current value
j = i - 1; // go for the previous element
/* compare with the previous elements in index j */
while loop( j less than 0 and List[j] greater than v ){
/* move forward if value at index j is large */
List[j+1] = List[j];
j--; // go for the next previous element
}
List[j+1] = v; // store current value
}

Merge Sort: {
A[] is the array and n is the length of the array left[i]=A[i];
void mergeSort(int A[],int n) }
{ For loop from mid to n-1
If n is less than 2{ {
return;}//return from the current function right[i-mid]=A[i];
mid = n/2;//finding the midpoint of the array }
left[mid];//creating an array with the length of mergeSort(left,mid);//recursive call
mid mergeSort(right,n-mid);//recursive call
right[n-mid];// creating an array with the length merge(left,mid,right,n-mid,A,n);//call a function
of (n-mid) }
for loop from 0 to mid-1
void merge(int left[],int sizeL,int right[], int sizeR, increment k;
int merged[], int sizeM) }
{ While loop i less than sizeL)
int i=0,j=0,k=0; {
while loop i less than sizeL and j less than sizeR merged[k] = left[i];
{ increment i;
If left[i]is less than or equal to right[j] increment k;
{ }
merged[k] = left[i]; while loop j less sizeR
increment i; {
} merged[k] = right[j];
Else increment j;
{ increment k;
merged[k] = right[j]; }
increment j; }
}

Quick Sort:
void QuickSort(int A[],int startn,int endn)
{
if(startn greater than or equal endn)
return;//return from the current function
int pIndex = Partition(A,startn,endn);//call Partition function
QuickSort(A,startn,pIndex-1);//recursive call
QuickSort(A,pIndex+1,endn);//recursive call
}
int Partition(int A[],int startn,int endn)
{
int i;
int pivot = A[endn];
int pIndex = startn;
for loop (i from startn to endn-1)
{
if(A[i] less than or equal to pivot)
{
int temp = A[i];
A[i]=A[pIndex];
A[pIndex]=temp;
Increment pIndex
}
}
int temp = A[pIndex];
A[pIndex]=A[endn];
A[endn]=temp;
return pIndex;//return pIndex from the fuction
}

Program in c:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

int randoms(int upper)


{
int i;
int num = rand() % upper;
return num;
}
void merge(int left[],int sizeL,int right[], int sizeR, int merged[], int sizeM)
{
int i=0,j=0,k=0;
while(i<sizeL && j<sizeR)
{
if(left[i]<=right[j])
{
merged[k] = left[i];
i++;
}
else
{
merged[k] = right[j];
j++;
}
k++;
}
while(i<sizeL)
{
merged[k] = left[i];
i++;
k++;
}
while (j<sizeR)
{
merged[k] = right[j];
j++;
k++;
}
}
void mergeSort(int A[],int n)
{
if(n<2)
return;
int i;
int mid = n/2;
int left[mid];
int right[n-mid];
for(i=0;i<mid;i++)
{
left[i]=A[i];
}
for(i=mid;i<n;i++)
{
right[i-mid]=A[i];
}
mergeSort(left,mid);
mergeSort(right,n-mid);
merge(left,mid,right,n-mid,A,n);
}
int partition(int A[],int startn,int endn)
{
int i;
int pivot = A[endn];
int pIndex = startn;
for(i=startn;i<endn;i++)
{
if(A[i]<=pivot)
{
int temp = A[i];
A[i]=A[pIndex];
A[pIndex]=temp;
pIndex++;
}
}
int temp = A[pIndex];
A[pIndex]=A[endn];
A[endn]=temp;
return pIndex;
}
void quickSort(int A[],int startn,int endn)
{
if(startn>=endn)
return;
int pIndex = partition(A,startn,endn);
quickSort(A,startn,pIndex-1);
quickSort(A,pIndex+1,endn);
}
void insertionSort(int List[],int n)
{
int i,j,v;
for(i=1;i<n;i++)
{
v=List[i];
j=i-1;
while(j>=0 && List[j]>v)
{
List[j+1]=List[j];
j--;
}
List[j+1] = v;
}
}
int main()
{
char in;
int f = 0;
int arr[1000];
int i;
for(i=0;i<1000;i++)
{
arr[i]=randoms(10000);
}
printf("For merge sort press 1\nFor quick sort press 2\nFor insertion sort press 3\nInput:");
scanf("%c",&in);
switch(in)
{
case '1':
mergeSort(arr,1000);
break;
case '2':
quickSort(arr,0,999);
break;
case '3':
insertionSort(arr,1000);
break;
default:
f = 1;
}
if(f==1)
{
printf("Invalid input");
return 0;
}
for(i=0;i<1000;i++)
{
printf("%d\n",arr[i]);
}
return 0;
}

Time vs size
Insertion Quick Merge
1.8
1.6
1.4
y: Time(milli second)

1.2
1
0.8
0.6
0.4
0.2
0
0 200 400 600 800 1000 1200
x: Size

Graph Analysis:

The analysis is done with fixed number of elements in an array and the size is 1000. This chart shows the
comparisons of time complexity among the three algorithms (insertion, quick and merge sort). Insertion sort,
quick sort, merge sort is represented respectively with blue line, red line, green line in the chart. When 100size
was taken into account time taken by three sorts are quite close (between 0.25 to 0.4). As gradually size
increases, we can a huge difference among these algorithms. Quick sort is best for any number of sizes.
Insertion is very much efficient for small number of size but as size increases the efficiency gradually deceases.
Merge sort is not that faster than quick sort but quite faster than insertion sort.

Task no 3

For the following numbers, show how radix sort can be used to sort the numbers. You
need to illustrate the operations of radix sort step by steps.

170, 45, 69, 80, 96, 55, 172, 356, 8, 5


Input list :

170 045 069 080 096 055 172 356 008 005

Step1:

BinSort on lower digit / Pass 1:

170 045 069 080 096 055 172 356 008 005

0 1 2 3 4 5 6 7 8 9
170

BinSort on lower digit:

170 045 069 080 096 055 172 356 008 005

0 1 2 3 4 5 6 7 8 9
170 045

BinSort on lower digit :

170 045 069 080 096 055 172 356 008 005

0 1 2 3 4 5 6 7 8 9
170 045 069

BinSort on lower digit:

170 045 069 080 096 055 172 356 008 005

0 1 2 3 4 5 6 7 8 9
170 045 069
080

BinSort on lower digit:

170 045 069 080 096 055 172 356 008 005

0 1 2 3 4 5 6 7 8 9
170 045 096 069
080

BinSort on lower digit:

170 045 069 080 096 055 172 356 008 005

0 1 2 3 4 5 6 7 8 9
170 045 096 069
080 055

BinSort on lower digit:

170 045 069 080 096 055 172 356 008 005

0 1 2 3 4 5 6 7 8 9
170 172 045 096 069
080 055

BinSort on lower digit:

170 045 069 080 096 055 172 356 008 005
0 1 2 3 4 5 6 7 8 9
170 172 045 096 069
080 055 356

BinSort on lower digit:

170 045 069 080 096 055 172 356 008 005

0 1 2 3 4 5 6 7 8 9
170 172 045 096 008 069
080 055 356

BinSort on lower digit:

170 045 069 080 096 055 172 356 008 005

0 1 2 3 4 5 6 7 8 9
170 172 045 096 008 069
080 055 356
005

After 1st step sorting

170 080 172 045 055 005 096 356 008 069

Step2:

(BinSort on next higher digit / Pass 2 :)

170 080 172 045 055 005 096 356 008 069

0 1 2 3 4 5 6 7 8 9
170

BinSort on next higher digit:

170 080 172 045 055 005 096 356 008 069
0 1 2 3 4 5 6 7 8 9
170 080

BinSort on next higher digit:

170 080 172 045 055 005 096 356 008 069

0 1 2 3 4 5 6 7 8 9
170 080
172

BinSort on next higher digit:

170 080 172 045 055 005 096 356 008 069

0 1 2 3 4 5 6 7 8 9
045 170 080
172

170 080 172 045 055 005 096 356 008 069

0 1 2 3 4 5 6 7 8 9
045 055 170 080
172

BinSort on next higher digit:

170 080 172 045 055 005 096 356 008 069

0 1 2 3 4 5 6 7 8 9
005 045 055 170 080
172
BinSort on next higher digit:

170 080 172 045 055 005 096 356 008 069

0 1 2 3 4 5 6 7 8 9
005 045 055 170 080 096
172

BinSort on next higher digit:

170 080 172 045 055 005 096 356 008 069

0 1 2 3 4 5 6 7 8 9
005 045 055 170 080 096
356 172

BinSort on next higher digit:

170 080 172 045 055 005 096 356 008 069

0 1 2 3 4 5 6 7 8 9
005 045 055 170 080 096
008 356 172

BinSort on next higher digit:

170 080 172 045 055 005 096 356 008 069

0 1 2 3 4 5 6 7 8 9
005 045 055 069 170 080 096
008 356 172

After 2nd step sorting:

005 008 045 055 356 069 170 172 080 096
Step3:

BinSort on next higher or highest digit / Pass 3 :

005 008 045 055 356 069 170 172 080 096

0 1 2 3 4 5 6 7 8 9
005

BinSort on next higher/highest digit:

005 008 045 055 356 069 170 172 080 096

0 1 2 3 4 5 6 7 8 9
005
008

BinSort on next higher/highest digit:

005 008 045 055 356 069 170 172 080 096

0 1 2 3 4 5 6 7 8 9
005
008
045

BinSort on next higher/highest digit:

005 008 045 055 356 069 170 172 080 096

0 1 2 3 4 5 6 7 8 9
005
008
045
055
BinSort on next higher/highest digit:

005 008 045 055 356 069 170 172 080 096

0 1 2 3 4 5 6 7 8 9
005 356
008
045
055

BinSort on next higher/highest digit:

005 008 045 055 356 069 170 172 080 096

0 1 2 3 4 5 6 7 8 9
005 356
008
045
055
069

BinSort on next higher/highest digit:

005 008 045 055 356 069 170 172 080 096

0 1 2 3 4 5 6 7 8 9
005 170 356
008
045
055
069

BinSort on next higher/highest digit:

005 008 045 055 356 069 170 172 080 096

0 1 2 3 4 5 6 7 8 9
005 170 356
008 172
045
055
069

BinSort on next higher/highest digit:

005 008 045 055 356 069 170 172 080 096

0 1 2 3 4 5 6 7 8 9
005 170 356
008 172
045
055
069
080

BinSort on next higher/highest digit:

005 008 045 055 356 069 170 172 080 096

0 1 2 3 4 5 6 7 8 9
005 170 356
008 172
045
055
069
080
096

After 3rd step sorting:

005 008 045 055 069 080 096 170 172 356

The final output

005 008 045 055 069 080 096 170 172 356
CONCLUSION
In this assignment, I addressed abstract data form, such as int, float, double and Boolean, and pseudocodes, and
programming insertion, quick, merge sort, and their run time graph with the analysis Next, I showed the
simulation of the type of radix that was given in the question.

References:

[1] fresh2refresh.com. 2020. C Data Types. [online] Available at: <https://fresh2refresh.com/c-programming/c-data-


types/> [Accessed 9 June 2020].

[2] Programiz.com. 2020. C Data Types. [online] Available at: <https://www.programiz.com/c-programming/c-data-


types> [Accessed 9 June 2020].

[3] Cs.uic.edu. 2020. C Programming Course Notes - Data Types. [online] Available at:
<https://www.cs.uic.edu/~jbell/CourseNotes/C_Programming/DataTypes.html> [Accessed 9 June 2020].

[4] Cs.uic.edu. 2020. C Programming Course Notes - Decisions And Branching. [online] Available at:
<https://www.cs.uic.edu/~jbell/CourseNotes/C_Programming/Decisions.html#:~:text=A%20true%20boolean
%20data%20type,is%20used%20to%20represent%20true.> [Accessed 9 June 2020].

[5] Educative: Interactive Courses for Software Developers. 2020. What Is Boolean In C?. [online] Available at:
<https://www.educative.io/edpresso/what-is-boolean-in-c#:~:text=A%20boolean%20is%20a%20data,the%20header
%20file%E2%80%8B%20stdbool.> [Accessed 9 June 2020].

You might also like