Lab 2
Lab 2
Bai 1:
int LinearSearch(int a[],int n,int k){
for(int i=0;i<n;i++){
if(a[i]==k)
return 1;
}
return -1;
}
Bai 2:
int BinarySearch(int a[],int left,int right,int k){
int mid = (left+right)/2;
while(left<=right){
if(a[mid]==k)
return mid;
else if(a[mid]>k)
{
right = mid-1;
return BinarySearch(a,left,right,k);
}
else{
left = mid+1;
return BinarySearch(a,left,right,k);
}
}
return -1;
}
Bai 3:
void CompareLinearBinarySearch(int a[],int n,int k){
clock_t begin,end;
begin = clock();
int m=-1;
for(int j=0;j<1000000;j++){
for(int i=0;i<n;i++){
if(a[i]==k){
m = i;
break;
}
}
}
printf("%d\n",m);
end = clock();
double cpu_use = (double)(end-begin)/CLOCKS_PER_SEC;
double t = cpu_use;
begin = clock();
int v=-1;
for(int j=0;j<1000000;j++){
int l = 0;
int r = n-1;
v=-1;
while (l <= r) {
int m = l + (r - l) / 2;
if (a[m] == k)
v=m;
if (a[m] < k)
l = m + 1;
else
r = m - 1;
}
}
printf("%d\n",v);
end = clock();
cpu_use = (double)(end-begin)/CLOCKS_PER_SEC;
double h = cpu_use;
if(t<h)
printf("Linear faster than Binary");
else
printf("Binary faster than Linear");
}
Bai 4:
// Function to perform binary search on the word array
int BinarySearch(struct WORD W[], int left, int right, const char name[]) {
while (left <= right) {
int mid = left + (right - left) / 2;
int cmp = strcmp(W[mid].Name, name);
if (cmp == 0) {
return mid; // Word found
}
if (cmp < 0) {
left = mid + 1; // Search in the right half
} else {
right = mid - 1; // Search in the left half
}
}
return -1; // Word not found
}
Bai 5:
#include <stdio.h>
#include <string.h>
// Function prototypes
void ReadData(struct WORD W[], char fname[]);
int BinarySearch(struct WORD W[], int left, int right, const char name[]);
// Function to read data from a file and populate the word array
void ReadData(struct WORD W[], char fname[]) {
FILE *file = fopen(fname, "r");
if (!file) {
printf("Error: Could not open file %s\n", fname);
return;
}
int i = 0;
char buffer[1024];
while (fgets(buffer, sizeof(buffer), file) && i < MAX_WORDS) {
// Split the line into name (English) and meaning (Vietnamese)
char *token = strtok(buffer, ",");
if (token != NULL) {
strncpy(W[i].Name, token, sizeof(W[i].Name));
token = strtok(NULL, "\n");
if (token != NULL) {
strncpy(W[i].Meaning, token, sizeof(W[i].Meaning));
i++;
}
}
}
fclose(file);
}
if (cmp == 0) {
printf("%s",W[mid].Meaning); // Word found
return 1;
}
if (cmp < 0) {
left = mid + 1; // Search in the right half
} else {
right = mid - 1; // Search in the left half
}
}// Word not found
printf("Not found");
return -1;
}
Lab 2:
Bai 1:
#include <ostream>
int number;
int count = 0;
// Read and print the first 'amount' numbers from the file
while (file >> number && count < amount) {
cout << number << endl;
count++;
}
int number;
while (file >> number) {
data.push_back(number); // Read each number from the file and store in the
vector
}
// Function to sort elements using Interchange Sort and display the first 'k'
elements
void SortElements(const string& filename, int k) {
vector<int> data; // Vector to store numbers
int number;
while (file >> number) {
data.push_back(number); // Read each number from the file and store in the
vector
}
// Function to sort elements using Interchange Sort and display the first 'k'
elements
void SortElements(const string& filename, int k) {
vector<int> data; // Vector to store numbers
int number;
while (file >> number) {
data.push_back(number); // Read each number from the file and store in the
vector
}
// Function to sort elements using Interchange Sort and display the first 'k'
elements
void SortElements(const string& filename, int k) {
vector<int> data; // Vector to store numbers
int number;
while (file >> number) {
data.push_back(number); // Read each number from the file and store in the
vector
}
// Function to sort elements using Interchange Sort and display the first 'k'
elements
void SortElements(const string& filename, int k) {
vector<int> data; // Vector to store numbers