Sami Data Structure

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 36

Nanjing University of

Aeronautics and Astronautics


Data Structure Course Design Report

(2020-2021 Academic year first semester)

Name :- Samuel Addisu


Student ID:- 191864110

Year, 2020 month, 12 day ,30


Nanjing University of Aeronautics and Astronautics

1. Simulation of bank calling system

One of the major factors influencing the success of organizations in

today’s competitive world is to increase customer satisfaction through the

improvement of service quality. Therefore, service organizations have

focused on various ways to understand customer perceptions and have

planned different strategies in order to provide a greater degree of

service to customers . In any service organization, managers are mostly

concerned about the time that customers are required to wait for

receiving their service. The delays in receiving service which will lead to

queuing are the usual problems in industrial environments and even in

everyday life situations . The fundamental features of a standard queuing

system consist of the structure of the line, groups of demand, arrival and

service processes, and discipline of the queue . In on-site service

organizations such as governmental agencies, retail stores or banks, the

inability to optimize the capability of the service will result in long queues.

Hence, the recognition and understanding of customer demand and what

the customer prefers is the initial step for the improvement of the service

capability . Banks in particular pay special attention to service quality as

the most significant core competence . Therefore, different approaches

have been applied in order to improve service quality and consequently

-1-
Nanjing University of Aeronautics and Astronautics

customer satisfaction in the banking industry. For instance, Shao et al.

adopted agent simulation to determine the optimal resource

configuration of a bank, in terms of cost and customer satisfaction. In a

similar study, Sarkar et al. applied simulation to find the best alternative in

terms of reducing the service time of banks’ counters considering the

utilization rate of counters. However, Sandmann , used simulation for

studying delays in a set of queues with correlated service time at each

node, where service time for each customer at the first node is a random

variable and the consecutive service times are correlated with the one at

the first node. Hammond and Mahesh also tried to propose a simulation

model, in which some bank teller management policies were applied to

achieve the desired level of service quality. In other different studies,

Jiantong and Liu and Wang and Sun , applied the six-sigma methodology

with DMAIC steps in banking services, aiming at enhancing service quality.

In addition, some other researchers tried to incorporate industrial

engineering techniques to improve the service quality of banks. As an

example, in the study conducted by Pei , motion study was performed in

order to reduce the service time of a bank’s tellers. Reviewing the relevant

literature reveals that tellers and lobby services are the two important

areas through which the productivity of banking operations can be

improved. Waiting for a long period of time, will result in customer

dissatisfaction. Therefore, banks should have adequate teller stations to

-2-
Nanjing University of Aeronautics and Astronautics

be able to provide quick service and at the same time ensure that tellers

are not idle and their productive work time is properly used . It should be

noted that waiting time relies on several factors such as arrival rate of

customers in the system, time of the day, rate at which the service is

provided, service type, and efficiency of counters . Customer behavior has

a tremendous influence on the effectiveness of service resource

utilization. A typical customer's characteristics can be influenced by the

following specifications: the type and quality of demand, the priority by

which she/he is served, the bearable queue length, the tolerable waiting

time, etc.

1.1 Algorithm Design


Waiting probability in the system: this represents the fraction of customers waiting in
the queue and its expression is:

P{W>0}=∑j=c∞πj=A(cμ/θ,λ/θ)E1,c1+[A(cμ/θ,λ/μ)
−1]E1,cPW>0=∑j=c∞πj=Acμ/θ,λ/θE1,c1+Acμ/θ,λ/μ−1E1,c (1)
where:

A(x,y)=xeyyxγ(x,y)=1+∑n=1∞yn∏k=1n(x+k),x>0,y>0Ax,y=xeyyxγx,y=1+∑n=1∞yn∏k=1nx+
k,x>0,y>0 (2)

and

E1,c=ρ⋅E1,c−11+ρ⋅E1,c−1,E1,0=1,ρ=λ/cμc≥1E1,c=ρ⋅E1,c−11+ρ⋅E1,c−1,E1,0=1,ρ=λ/cμc≥1 (3)
Probability of abandonment due to impatience: the fraction of
abandonment P{Ab}PAb can be understood simply as the product between the
probability of abandoning due to impatience P{Ab|W>0}PAb|W>0 and the
probability of waiting in the system P{W>0}PW>0 , i.e.:

P{Ab}=P{Ab|W>0}×P{W>0}PAb=PAb|W>0×PW>0 (4)
Therefore, it follows that:

P{Ab|W>0}=∑n=c∞πnPn−c{Ab}/P{W>0}=1ρA(cμ/θ,λ/μ)+1−1ρPAb|
W>0=∑n=c∞πnPn−cAb/PW>0=1ρAcμ/θ,λ/μ+1−1ρ (5)

-3-
Nanjing University of Aeronautics and Astronautics

Average waiting time of customers who are waiting: exploring the linear relationship
between the fraction of abandonment P{Ab}PAb and the average waiting
time E{W}EW that occurs not only in the Erlang-A system, but in other models with
exponential patience and expressed by P{Ab}=θ.E(W)PAb=θ.EW , we find:

E[W|W>0]=1θ.[1ρA(cμ/θ,λ/θ)+1−1ρ]EW|W>0=1θ.1ρAcμ/θ,λ/θ+1−1ρ (6)

1.2 Source Code

#include <bits/stdc++.h>
using namespace std;

// this is the structure/class for customer


struct customer{
int ari_hr,ar_min,bt; // Arrival time-hrs:minutes, bt MEANS bussiness time

//operator overloading of '<' (less than) operator


bool operator <(customer& c2){
if(ari_hr==c2.ari_hr)ar_min<c2.ar_min;
return ari_hr<c2.ari_hr;
}

};
int main(){
srand(time(0));

int wnum; //window numbers in bank

while(1){
cout<<"Please Enter the number of windows for the bank.\n";
cin>>wnum;
if(wnum<=0)break; // if wnum<=0 program will terminate
int n=1+rand()%100;
vector<customer> qu(n); // randomly generate all the customer arrival time and
bussiness time
for(int i=0;i<n;++i){

-4-
Nanjing University of Aeronautics and Astronautics

qu[i].ari_hr=9+rand()%8;
qu[i].ar_min=rand()%60;
qu[i].bt=rand()%30;
}
sort(qu.begin(),qu.end()); //sorting the list accprding to their arrival time
/*
last[i] will store the time at which the last customer finishes work at window i+1
serve_cnt[i] will count the number of customer served at window i+1
*/
int last[wnum]={0},cust_num=0,serve_cnt[wnum]={0};
for(int i=0;i<n;++i) {
if(qu[i].bt==0)continue;
++cust_num;
int min_last=0; //store the window number which finishes earliest
for(int j=1;j<wnum;++j) {
if(last[j]<last[min_last])min_last=j;
}
++serve_cnt[min_last];
int wt=last[min_last]-qu[i].ari_hr*60-qu[i].ar_min; //waiting time calulation
wt=max(wt,0); //waiting cant be negative

last[min_last]=(qu[i].ari_hr*60+qu[i].ar_min+wt+qu[i].bt); // updating the last of


the current window
//printing the output into the screen
cout<<"number "<<cust_num<<" customer
"<<qu[i].ari_hr<<":"<<qu[i].ar_min<<" arrive,";
cout<<" wait for "<<wt<<"minutes, ";
int x=qu[i].ari_hr,y=qu[i].ar_min;
y+=wt;
x+=(y/60);
y%=60;
cout<<x<<":"<<y<<" at number "<<min_last+1<<" window handle bussiness,last
"<<qu[i].bt<<" minutes.\n";
}

cout<<"No longer accept customer bussiness, ready to leave work\n\n";


for(int i=0;i<wnum;++i){
cout<<"number "<<i+1<<" window serve totally "<<serve_cnt[i]<<"
customers.\n\n";
}
}
return 0;
}

-5-
Nanjing University of Aeronautics and Astronautics

1.3 Program Run Results

-6-
Nanjing University of Aeronautics and Astronautics

1.4 Summary
The validation was made by comparing the deviations of the performance measures
obtained through analytical queueing models and those observed with the actual data
and also estimated with the results from discrete simulation models. In order to meet
this objective, we studied different analytical queueing models with abandonment
found in the literature, which use exponential and generic probability distributions in
the abandonment process.

Mixed and non-mixed distributions based on Lognormal and Exponential were used to
model the abandonment process, among the possible distributions adjusted by the
adherence test applied to these data. The arrival, service and abandonment rates
were extracted using data from the Bank Call Center.

In addition, estimates were obtained for performance measures of the system, such
as: the average waiting time, the probability of waiting, the probability of
abandonment and traffic intensity. These parameters (rates) were inserted into the
analytical queueing models considered in this study and these performance measures
were calculated. Comparing these measurements with those extracted from the actual
data, it can be affirmed that the generic analytical queueing models to represent user
patience, considered in this study, produced results very close to those extracted from
the actual data and, therefore, captured well the reality of the Call Center in all the
analyzed performance measures.

2. Expression conversion

A conversion expression specifies how to convert a number from one unit to another. For
example, to convert a length in metres to millimetres, you must multiply the metres length by
1,000. A conversion expression is what is used to do this.
Conversion expressions can be used in five different ways: to just convert a number from one
unit to another, to add two numbers, to subtract two numbers, to multiply two numbers and to
divide two numbers. Sometimes, the necessary arithmetic can be done without a conversion.
For example, if you don't define a specific 'conversion' for adding two numbers, the system

-7-
Nanjing University of Aeronautics and Astronautics

will just convert one to the units of the other and do a normal add. The full rules are shown in
the table below. In this table A represents any unit of measure and B represents any unit of
measure with different units to A. Where rules are given, one of them must be available. If
not, the operation will be abandoned and the result will be undefined.

2.1 Algorithm Design


 Let us consider the infix expression 2 + 3 * 4 and its postfix will be 2 3 4 * +. Notice
that between infix and postfix the order of the numbers(or operands) is unchanged.
It is 2 3 4 in both the cases. But the order of the operators * and + is affected in the
two expressions.
 Only one stack is enough to convert an infix expression to postfix expression. The
stack that we used in the algorithm will be used to change the order of operators
form infix to postfix. The stack we use will only contain operators and open
parentheses symbol ‘(‘. Postfix expressions do not contain parentheses. We shall not
output the parentheses in the postfix output.
THE ALGORITHM:-

1. Create a stack
2. for each character ‘t’ in the input stream {
 if (t is an operand)
output t
 else if (t is a right parentheses){
POP and output tokens until a left parentheses is popped(but do not output)
}
 else {
POP and output tokens until one of lower priority than t is encountered or a left
parentheses is encountered
or the stack is empty
PUSH t
}
3. POP and output tokens until the stack is empty.
For better understanding, let us trace out an example A * B – (C + D) + E

2.2 Source Code


#include<iostream>
#include<stack>
#include<locale>
#include <climits>

-8-
Nanjing University of Aeronautics and Astronautics

using namespace std;


int preced(char ch) {  
if(ch == '+' || ch == '-')
return 1;
}else if(ch == '*' || ch == '/') {
return 2;
}else {
return 0;
}
}
string inToPost(string infix ) {  
stack<char> stk;
stk.push('#');   
string postfix = "";   
string::iterator it;
for(it = infix.begin(); it!=infix.end(); it++) { // for loop 
if(isalnum(char(*it)))  // if condition 
postfix += *it;
else if(*it == '(') // if condition 
stk.push('(');
else if(*it == ')') { // if condition 
while(stk.top() != '#' && stk.top() != '(') {
postfix += stk.top();  
stk.pop(); // pop element 
}
stk.pop();   
}else {
if(preced(*it) > preced(stk.top())) // if condition 
stk.push(*it);
else {
while(stk.top() != '#' && preced(*it) <= preced(stk.top())) { // while loop 
postfix += stk.top();
stk.pop();
}
stk.push(*it);
}
}
}
while(stk.top() != '#') {
postfix += stk.top();
stk.pop();
}

-9-
Nanjing University of Aeronautics and Astronautics

return postfix;
}

float scanNum(char ch){


int value;
value = ch;
return float(value-'0');
}
int isOperator(char ch){  
if(ch == '+'|| ch == '-'|| ch == '*'|| ch == '/')
return 1;
return -1;
}
int isOperand(char ch){
if(ch >= '0' && ch <= '9')
return 1;
return -1;
}
float operation(int a, int b, char op){
if(op == '+')
return b+a;
else if(op == '-')
return b-a;
else if(op == '*')
return b*a;
else if(op == '/')
return b/a;
else
return INT_MIN;
}
float postfixEval(string postfix){
int a, b;
stack<float> stk;
string::iterator it;
for(it=postfix.begin(); it!=postfix.end(); it++){
if(isOperator(*it) != -1){
a = stk.top();
stk.pop();
b = stk.top();
stk.pop();
stk.push(operation(a, b, *it));
}else if(isOperand(*it) > 0){
stk.push(scanNum(*it));

- 10 -
Nanjing University of Aeronautics and Astronautics

}
}
return stk.top();
}

int main() {  // main method 


string infix;
Label:
cout<<"\n\nEnter the infix expression : ";  // print message 
getline(cin,infix);
int flag=0;
for(int i=0;i<infix.size();i++)
{
if(infix[i]>='a' && infix[i]<='z'|| infix[i]>='A'&& infix[i]<='Z')
{
printf("Invalid String!! Your string should have '*','/','+','-',','(',')' and
integers.\nPlease re-enter the string..");
goto Label;
}
if(infix[i]>=32 && infix[i]<=39 || infix[i]==44 || infix[i]==46 || infix[i]>=58
&& infix[i]<=64 || infix[i]>=91 && infix[i]<=96 )
{
printf("Invalid String!! Your string should have '*','/','+','-',','(',')' and
integers.\nPlease re-enter the expression..");
goto Label;
}
if(infix[i]=='*' || infix[i]=='/' || infix[i]=='+' || infix[i]=='-' || infix[i]=='(' ||
infix[i]==')')
{
flag++;
}
}
if(flag!=0)
{
string postfix;
postfix=inToPost(infix);
cout << "Postfix Form Is : " << inToPost(infix) << endl;
cout << "Postfix evaluation : "<<postfixEval(postfix)<<endl;
}
else
{
printf("Invalid String!! Your string should only have '*','/','+','-',','(',')' and
integers.\nPlease re-enter the string.");

- 11 -
Nanjing University of Aeronautics and Astronautics

goto Label;
  
}
}
2.3
2.4 Program Run Results

2.5 Summary
We need to develop an algorithm to convert any infix expression to a postfix
expression. To do this we will look closer at the conversion process.
Only one stack is necessary to convert an infix to a postfix expression. To adjust the
order of operators from infix to postfix, the stack that we used in the algorithm will
be used. Only operators and open parentheses symbol '(' will include the stack we
use. Postfix expressions do not contain parentheses. In the postfix output, we will not
output parentheses.

3 huffman code
A Huffman code is a special type of optimal prefix code that is widely used for data
compression without loss. By means of Huffman coding, an algorithm developed by
David A. Huffman when he was a Sc.D, the method of discovering or using such a code
continues. MIT student and published in the paper "A Method for the Construction of
Minimum-Redundancy Codes" in 1952 .[1]

- 12 -
Nanjing University of Aeronautics and Astronautics

The output from the Huffman algorithm can be viewed as a code table of variable length
to encode a source symbol (such as a character in a file). For each potential value of the
source symbol, the algorithm extracts this table from the approximate likelihood or
frequency of occurrence (weight). As in other methods of entropy encoding, using fewer
bits than less common symbols, more common symbols are usually represented. The
method of Huffman can be effectively applied, finding a code linear in time to the
number of input weights if these weights are sorted.[2] However, while optimal among
methods that separately encode symbols.
Huffman coding uses a specific method for choosing the representation for each symbol,
resulting in a prefix code (sometimes called "prefix-free codes", that is, the bit string
representing some particular symbol is never a prefix of the bit string representing any
other symbol). Huffman coding is such a widespread method for creating prefix codes
that the term "Huffman code" is widely used as a synonym for "prefix code" even when
such a code is not produced by Huffman's algorithm.

3.1 Algorithm Design


Fixed-Length encoding - Using the same number of bits, every character
is assigned a binary code. Thus, for storage or transmission, a string like
"aabacdad" will require 64 bits (8 bytes), assuming that 8 bits are used
by each character.

Variable-Length encoding - Unlike fixed-length encoding, this scheme


uses a variable number of bits to encode the characters in the given text,
depending on their frequency. Therefore, the frequency of characters'a
',' b ',' c 'and'd' is 4,1,1 and 2 respectively for a given string like
"aabacdad". Since 'a' happens more often than 'b',' c' and 'd', it uses a
smaller number of bits, followed by 'd',' b' and 'c'. Suppose we assign
binary codes randomly to each character as follows—

A0

B 011 011

C,111

D, 11

This encodes the string 'aabacdad' to 000110111111011 (0 | 0 | 011 | 0


| 111 | 11 | 0 | 11) using a lower number of bits than the fixed-length
encoding scheme.

- 13 -
Nanjing University of Aeronautics and Astronautics

3.2 Source Code


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

#define MAX_TREE_HT 100

struct MinHeapNode {

char data;

unsigned freq;

struct MinHeapNode *left, *right;


};

struct MinHeap {

unsigned size;

unsigned capacity;

struct MinHeapNode** array;


};

struct MinHeapNode* newNode(char data, unsigned freq)


{
struct MinHeapNode* temp
= (struct MinHeapNode*)malloc
(sizeof(struct MinHeapNode));

- 14 -
Nanjing University of Aeronautics and Astronautics

temp->left = temp->right = NULL;


temp->data = data;
temp->freq = freq;

return temp;
}

struct MinHeap* createMinHeap(unsigned capacity)

struct MinHeap* minHeap


= (struct MinHeap*)malloc(sizeof(struct MinHeap));

minHeap->size = 0;

minHeap->capacity = capacity;

minHeap->array
= (struct MinHeapNode**)malloc(minHeap->
capacity * sizeof(struct MinHeapNode*));
return minHeap;
}

void swapMinHeapNode(struct MinHeapNode** a,


struct MinHeapNode** b)

struct MinHeapNode* t = *a;


*a = *b;
*b = t;
}

void minHeapify(struct MinHeap* minHeap, int idx)

int smallest = idx;


int left = 2 * idx + 1;

- 15 -
Nanjing University of Aeronautics and Astronautics

int right = 2 * idx + 2;

if (left < minHeap->size && minHeap->array[left]->


freq < minHeap->array[smallest]->freq)
smallest = left;

if (right < minHeap->size && minHeap->array[right]->


freq < minHeap->array[smallest]->freq)
smallest = right;

if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest],
&minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}

int isSizeOne(struct MinHeap* minHeap)


{

return (minHeap->size == 1);


}

struct MinHeapNode* extractMin(struct MinHeap* minHeap)

struct MinHeapNode* temp = minHeap->array[0];


minHeap->array[0]
= minHeap->array[minHeap->size - 1];

--minHeap->size;
minHeapify(minHeap, 0);

return temp;
}

void insertMinHeap(struct MinHeap* minHeap,


struct MinHeapNode* minHeapNode)

- 16 -
Nanjing University of Aeronautics and Astronautics

++minHeap->size;
int i = minHeap->size - 1;

while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {

minHeap->array[i] = minHeap->array[(i - 1) / 2];


i = (i - 1) / 2;
}

minHeap->array[i] = minHeapNode;
}

void buildMinHeap(struct MinHeap* minHeap)

int n = minHeap->size - 1;
int i;

for (i = (n - 1) / 2; i >= 0; --i)


minHeapify(minHeap, i);
}

void printArr(int arr[], int n)


{
int i;
for (i = 0; i < n; ++i)
printf("%d", arr[i]);

printf("\n");
}

int isLeaf(struct MinHeapNode* root)

return !(root->left) && !(root->right);


}

- 17 -
Nanjing University of Aeronautics and Astronautics

struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size)

struct MinHeap* minHeap = createMinHeap(size);


int i;
for ( i=0; i<size; ++i)
{
minHeap->array[i] = newNode(data[i], freq[i]);}

minHeap->size = size;
buildMinHeap(minHeap);

return minHeap;
}

struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size)

{
struct MinHeapNode *left, *right, *top;

struct MinHeap* minHeap = createAndBuildMinHeap(data, freq, size);

while (!isSizeOne(minHeap)) {

left = extractMin(minHeap);
right = extractMin(minHeap);

top = newNode('$', left->freq + right->freq);

top->left = left;
top->right = right;

insertMinHeap(minHeap, top);
}

return extractMin(minHeap);
}

- 18 -
Nanjing University of Aeronautics and Astronautics

void printCodes(struct MinHeapNode* root, int arr[], int top)

if (root->left) {

arr[top] = 0;
printCodes(root->left, arr, top + 1);
}

if (root->right) {

arr[top] = 1;
printCodes(root->right, arr, top + 1);
}

if (isLeaf(root)) {

printf("%c: ", root->data);


printArr(arr, top);
}
}

void HuffmanCodes(char data[], int freq[], int size)

struct MinHeapNode* root


= buildHuffmanTree(data, freq, size);

int arr[MAX_TREE_HT], top = 0;

printCodes(root, arr, top);


}

void printFrequency(int freq[],char *a)


{

- 19 -
Nanjing University of Aeronautics and Astronautics

printf("\nThe frequency of lowercase letters are: \n");


int i;
for (i = 0; i < 26; i++) {

if (freq[i] != 0) {

a[i]=i+'a';
printf("%c - %d\n",
a[i], freq[i]);
}
} printf("\n");
}

void findFrequncy(char S[],char *a, int *f)


{
int i = 0;

while (S[i] != '\0') {

if (S[i] >= 'a' && S[i] <= 'z')


{
f[S[i] - 'a']++;

}i++; }

printFrequency(f,a);
}

int main()
{
char S[2000];

FILE *filePointer;
char ch;
int i=0;

//2
filePointer = fopen("data.txt", "r");

//3

- 20 -
Nanjing University of Aeronautics and Astronautics

if (filePointer == NULL)
{
printf("File is not available \n");
}
else
{
//4
while ((ch = fgetc(filePointer)) != EOF)
{
S[i]=ch;
i++;
}
}
fclose(filePointer);
char arr[26];
int freq[26]={0};
printf("The data from file is: \n %s \n",S);
findFrequncy(S,arr,freq);

int size = sizeof(arr) / sizeof(arr[0]);


printf("The corresponding Huffman codes:\n");
HuffmanCodes(arr, freq, 26);

return 0;
}

3.3 Program Run

Results

- 21 -
Nanjing University of Aeronautics and Astronautics

3.4 Summary
the real problem lies with the decoding phase. If we try and decode the string
00011011111011, it will be quite ambiguous since, it can be decoded to the multiple
strings, few of which are-
aaadacdad (0 | 0 | 0 | 11 | 0 | 111 | 11 | 0 | 11)
aaadbcad (0 | 0 | 0 | 11 | 011 | 111 | 0 | 11)
aabbcb (0 | 0 | 011 | 011 | 111 | 011)
… and so on
To prevent such ambiguities during decoding, the encoding phase should satisfy
the “prefix rule” which states that no binary code should be a prefix of another
code. This will produce uniquely decodable codes. The above codes for ‘a’, ‘b’, ‘c’
and ‘d’ do not follow prefix rule since the binary code for a, i.e. 0, is a prefix of binary
code for b i.e 011, resulting in ambiguous decodable codes.

4. Hash search

The Hash Table is a data structure that associatively stores data. Data is stored in an array
format in a hash table, where every data value has its own unique index value. If we
know the index of the desired data, data access becomes very rapid.

- 22 -
Nanjing University of Aeronautics and Astronautics

Thus, regardless of the size of the data, it becomes a data structure in which insertion
and search operations are very easy. The Hash Table uses an array as a storage medium
and uses a hash method to create an index from which an element is to be inserted or
located.

4.1 Algorithm Design


it may happen that the hashing technique is used to create an already used
index of the array. In such a case, we can search the next empty location in
the array by looking into the next cell until we find an empty cell. This
technique is called linear probing.
Following are the basic primary operations of a hash table.

Search − Searches an element in a hash table.



Insert − inserts an element in a hash table.



delete − Deletes an element from a hash table.


4.2 Source Code


include <stdio.h>
include<stdlib.h>
include<string.h>
include<math.h>
define M 12
define N 467
typedef struct student {

- 23 -
Nanjing University of Aeronautics and Astronautics

char name[50];
char sex[10];
char number[M];
}ST;
void Inittable(ST* A);
void Readfromfile(ST* A);
int Hashfun(char* number);
void Query(ST* A, char* number);
//int getprime(int n);
void print(ST* A);
//__________________
void Inittable(ST* A) {
ST stud;
strcpy_s(stud.number, sizeof(stud.number), "-1");
strcpy_s(stud.name, sizeof(stud.name), "none");
strcpy_s(stud.sex, sizeof(stud.sex), "none");
for (int i = 0; i < N; i++)
A[i] = stud;
}
//__________________
void Readfromfile(ST* A) {
FILE* fp;
fopen_s(&fp, "input.txt", "r");
char name[50];
char sex[10];
char number[M];

int index;
int count;
while (fscanf_s(fp, "%s %s %s", name,sizeof(name), sex,sizeof(sex),
number,sizeof(number))==3) {
ST stud;
strcpy_s(stud.name,sizeof(stud.name), name);
strcpy_s(stud.sex, sizeof(stud.sex), sex);
strcpy_s(stud.number, sizeof(stud.number), number);
count = 0;
index = Hashfun(stud.number);
while (1) {
if (count == 80)
break;
if (strcmp(A[index].number, "-1") == 0) {
A[index] = stud;
break;
}

- 24 -
Nanjing University of Aeronautics and Astronautics

index = (index + 1) % N;
count++;
}
}
}
//_______________
void Query(ST* A, char* number) {
int index = Hashfun(number);
int count = 0;
while (1) {
if (strcmp(A[index].number, number) == 0) {
printf("found at index %d with total hash %d\n", index,count);
return;
}
else if (strcmp(A[index].number, "-1") == 0 || count==80) {
printf("not found with total hash %d\n",count);
return;
}
else {
count++;
index = (index + 1) % N;
}
}
}
//_________________
int Hashfun(char* number) {
unsigned long long int num;
sscanf_s(number,"%llu\n",&num );
return num % N;
}
//__________________
void print(ST* A) {
for (int i = 0; i < N; i++) {
printf("%s %s %s\n", A[i].name, A[i].sex, A[i].number);
}
}
//__________________
int main(){
ST* A= (ST*)malloc(sizeof(ST) * N);;
Inittable(A);
Readfromfile(A);
Query(A,"15840971965");
Query(A, "13777904575");
Query(A, "15840971000");

- 25 -
Nanjing University of Aeronautics and Astronautics

Query(A, "13501099785");
Query(A, "23140971965");
return 0;
}

4.3 Program Run Results

- 26 -
Nanjing University of Aeronautics and Astronautics

4.4 Summary
A hash table (hash map) in computing is a data structure that implements an abstract
data form associative list, a structure that can map keys to values. A hash table
utilizes a hash function to compute an index through an array of buckets or slots,
often called a hash code, from which the desired value can be found.

5. Implementation of various sorting algorithms

Bubble Sort
Bubble sort, is a simple comparison-based sorting algorithm that repeatedly steps
through the list, compares adjacent elements and swaps them if they are in the
wrong order. The pass through the list is repeated until the list is sorted.
Complexity:
Worst complexity: n^2
Best complexity: n
Insertion Sort
Insertion sort is a simple sorting algorithm that builds the final list one item at a time.
This is an in-place comparison-based sorting algorithm. It is much less efficient on
large lists than more advanced algorithms such as quick sort, heap sort, or merge
sort.
Complexity:
Worst complexity: n^2
Best complexity: n^2
Selection Sort
Selection sort 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.
Complexity:
Worst complexity: n^2
Best complexity: n^2
Shell Sort
Shell sort, also known as Shell sort or Shell’s method, is an in-place comparison
sorting algorithm. It can be seen as either a generalization of sorting by exchange or
sorting by insertion.
Complexity:
Average complexity: n*log(n)^2 or n^(3/2)
Best complexity: n

- 27 -
Nanjing University of Aeronautics and Astronautics

5.1 Algorithm Design


An algorithm that places elements of a list in a certain order is a sorting algorithm. Numerical
order and lexicographical order are the orders most commonly used. Efficient sorting is critical for
optimizing the effectiveness of other algorithms that require input data to be in sorted lists (such as
search and merge algorithms). Sorting is also often useful for data canonicalization and human-
readable output processing. More formally, two conditions must be met by the performance of any
sorting algorithm:

The performance is in a non-decreasing order (each element according to the desired total order is
no smaller than the previous element);

The output is a permutation of the input (a reordering, but still maintaining all the original
elements).

In addition, the input data is often stored in an array that allows random access rather than a list
that allows only sequential access, although, after sufficient adjustment, several algorithms can be
applied to either data form.

5.2 Source Code


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

int qC, qM;

void swap(int x, int y){


int temp = x;
x = y;
y = temp;
}

void InsertionSort(int A[],int n){


int i,j,x;

- 28 -
Nanjing University of Aeronautics and Astronautics

int iC = 0, iM = 0;
for(i=1;i<n;i++){
j=i-1;
x=A[i];
iM++;
while(j>-1 && A[j]>x)
{
iC++;
A[j+1]=A[j];
iM++;
j--;
}
A[j+1]=x;
}
printf("\n\nDirect insertion sort needs %d times comparisons, %d times of
moves",iC,iM);
}

void SelectionSort(int A[],int n)


{
int i,j,k, sC, sM;

for(i=0;i<n-1;i++)
{
for(j=k=i;j<n;j++)
{
sC++;
if(A[j]<A[k])
k=j;
}
swap(A[i],A[k]);
sM++;
}

printf("\n\nDirect selection sort needs %d times comparisons, %d times of moves",sC,sM);


}
int partition(int A[],int l,int h)
{
int pivot=A[l];
int i=l,j=h;
do
{
do{i++;}while(A[i]<=pivot); qC++;
do{j--;}while(A[j]>pivot); qC++;

- 29 -
Nanjing University of Aeronautics and Astronautics

if(i<j) qC++; swap(A[i],A[j]); qM++;


}while(i<j); qC++;
swap(A[l],A[j]); qC++;
return j;
}
void QuickSort(int Quick[], int low, int high){

qC++;

if(low<high){
int p = partition(Quick,low,high);
QuickSort(Quick,low,p-1);
low = p+1;
QuickSort(Quick, low, high);
}
// cout<<"Quick sort needs "<<qC<<" times comparisons, "<<qM<<" times of moves\n";
}

void BubbleSort(int bubble[],int n)


{

int comparisons = 0, moves = 0;


int i;
for( i=1;i<n;i++){
int j;
for( j=0;j<n-1;j++){

comparisons++;
if(bubble[j]>bubble[j+1]){

swap(bubble[j],bubble[j+1]);

moves++;
}
}
}

printf("\nBubble sort needs %d times comparisons, %d times of


moves",comparisons,moves);
}

int mC=0,mM=0;
void Merge(int A[], int low, int mid, int high){

- 30 -
Nanjing University of Aeronautics and Astronautics

int i = low;
int j = mid+1;
int k = low;
int B[high+1];
mC++;
while (i <= mid && j <= high){
mC++;
if (A[i] < A[j]){
mC++;
B[k++] = A[i++];
mM++;
} else {
B[k++] = A[j++];
mM++;
}
}
mC++;
while (i <= mid){
mC++;
B[k++] = A[i++];
mM++;
}
while (j <= high){
mC++;
B[k++] = A[j++];
mM++;
}

for ( i=low; i<=high; i++){


mC++;
A[i] = B[i];
mM++;
}
}

void MergeSort(int arr[], int n){


int p;
for (p=2; p<=n; p=p*2){
int i;
for ( i=0; i+p-1<n; i=i+p){
int low = i;
int high = i+p-1;
int mid = (low+high)/2;
Merge(arr, low, mid, high);

- 31 -
Nanjing University of Aeronautics and Astronautics

}
}
if (p/2 < n){
Merge(arr, 0, p/2-1, n-1);
}
printf("\n\nMerge sort needs %d times comparisons, %d times of moves",mC,mM);
}

int temp,hC=0,hM=0;;
void heapify(int arr[], int size, int i)
{

int largest = i;
int left = 2*i + 1;
int right = 2*i + 2;
hC++;
if (left < size && arr[left] >arr[largest])
largest = left;

hC++;
if (right < size && arr[right] > arr[largest])
largest = right;

hC++;
if (largest != i)
{
swap(arr[i],arr[largest]) ;
hM++;
heapify(arr, size, largest);
}
}

void heapSort(int arr[], int size)


{
int i;
for (i = size / 2 - 1; i >= 0; i--)
heapify(arr, size, i);
for (i=size-1; i>=0; i--)
{
swap(arr[0],arr[i]) ;
hM++;
heapify(arr, i, 0);
}

- 32 -
Nanjing University of Aeronautics and Astronautics

printf("\n\nHeap sort needs %d times comparisons, %d times of moves",hC,hM);


}

int main(){

int size;
while(1){
printf("\n\nPlease enter input size of problem: ");
scanf("%d",&size);

int *arr = (int*)malloc(size*sizeof(int));

int i;
for( i=0; i<size; i++){
int val = rand();
arr[i] = val;
}

BubbleSort(arr,size);
InsertionSort(arr,size);
SelectionSort(arr,size);

qC = 0; qM = 0;
QuickSort(arr,0,size-1) ;
printf("\n\nQuick sort needs %d times comparisons, %d times of moves",qC,qM);
heapSort(arr,size);
MergeSort(arr,size);

return 0;
}

5.3 Program Run Results

- 33 -
Nanjing University of Aeronautics and Astronautics

5.4 Summary
1. Understand and implement quick and merge sort.
2. Understand the use cases of insertion, bucket, counting, radix sort.
3. Learn how heap sort works with the tree data structure.
4. Know why you shouldn't use bubble sort or selection sort

- 34 -
Nanjing University of Aeronautics and Astronautics

- 35 -

You might also like