0% found this document useful (0 votes)
6 views7 pages

Lab 2

Uploaded by

Nguyễn Hào
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Lab 2

Uploaded by

Nguyễn Hào
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 1:

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>

#define MAX_WORDS 101

// Structure to store a word and its meaning


struct WORD {
char Name[256]; // English word
char Meaning[512]; // Vietnamese meaning
};

// 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);
}

// Function to perform binary search on the word array


int BinarySearchNoRecursion(struct WORD W[], int n, const char name[]) {
int left = 0;
int right = n - 1;
while(left<=right){
int mid = (left+right)/2;
int cmp = strcmp(W[mid].Name, name);

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 ReadData(std::string filename, int a[]) {


std::ifstream file(filename);
if (!file) {
std::cerr << "Không thể mở tệp " << filename << std::endl;
return -1;
}
int i = 0;
while (file >> a[i]) ++i;
file.close();
return i;
}
Bai 2:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

// Function to show the first 'amount' elements from a file


void ShowHead(const string& filename, int amount) {
ifstream file(filename); // Open the file using ifstream
if (!file) {
cerr << "Error: Could not open file " << filename << endl;
return;
}

int number;
int count = 0;

// Read and print the first 'amount' numbers from the file
while (file >> number && count < amount) {
cout << number << endl;
count++;
}

file.close(); // Close the file after reading


}
Bai 3:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;


void ReadData(const string& filename, vector<int>& data) {
ifstream file(filename); // Open the file using ifstream
if (!file) {
cerr << "Error: Could not open file " << filename << endl;
return;
}

int number;
while (file >> number) {
data.push_back(number); // Read each number from the file and store in the
vector
}

file.close(); // Close the file after reading


}

// Interchange Sort implementation


void InterchangeSort(vector<int>& data) {
int n = data.size();
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (data[i] > data[j]) {
// Swap elements
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
}

// 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

// Step 1: Read data from the file


ReadData(filename, data);

// Step 2: Sort the data using Interchange Sort


InterchangeSort(data);

// Step 3: Display the first 'k' elements


for (int i = 0; i < k && (long unsigned int)i < data.size(); i++) {
cout << data[i] << endl;
}
}
Bai 4:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;


void ReadData(const string& filename, vector<int>& data) {
ifstream file(filename); // Open the file using ifstream
if (!file) {
cerr << "Error: Could not open file " << filename << endl;
return;
}

int number;
while (file >> number) {
data.push_back(number); // Read each number from the file and store in the
vector
}

file.close(); // Close the file after reading


}

// Interchange Sort implementation


void BubbleSort(vector<int>& data) {
int n = data.size();
for (int i = 0; i < n-1; i++) {
for (int j = n-1; j >i; j--) {
if (data[j] < data[j-1]) {
// Swap elements
int temp = data[j];
data[j] = data[j-1];
data[j-1] = temp;
}
}
}
}

// 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

// Step 1: Read data from the file


ReadData(filename, data);

// Step 2: Sort the data using Interchange Sort


BubbleSort(data);
// Step 3: Display the first 'k' elements
for (int i = 0; i < k && (long unsigned int)i < data.size(); i++) {
cout << data[i] << endl;
}
}
Bai 5
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;


void ReadData(const string& filename, vector<int>& data) {
ifstream file(filename); // Open the file using ifstream
if (!file) {
cerr << "Error: Could not open file " << filename << endl;
return;
}

int number;
while (file >> number) {
data.push_back(number); // Read each number from the file and store in the
vector
}

file.close(); // Close the file after reading


}

// Interchange Sort implementation


void InsertionSort(vector<int>& data) {
int n = data.size();
int pos ,x;
for(int i=1;i<n;i++){
x = data[i];
pos = i-1;
while(pos>=0&&data[pos]>x){
data[pos+1] = data[pos];
pos--;
}
data[pos+1]=x;
}
}

// 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

// Step 1: Read data from the file


ReadData(filename, data);

// Step 2: Sort the data using Interchange Sort


InsertionSort(data);

// Step 3: Display the first 'k' elements


for (int i = 0; i < k && (long unsigned int)i < data.size(); i++) {
cout << data[i] << endl;
}
}
Bai 6
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;


void ReadData(const string& filename, vector<int>& data) {
ifstream file(filename); // Open the file using ifstream
if (!file) {
cerr << "Error: Could not open file " << filename << endl;
return;
}

int number;
while (file >> number) {
data.push_back(number); // Read each number from the file and store in the
vector
}

file.close(); // Close the file after reading


}

// Interchange Sort implementation


void SelectionSort(vector<int>& data) {
int n = data.size();
for(int i = 0 ;i<n;i++){
int min = i;
for(int j=i;j<n;j++){
if(data[j]<data[min])
min = j;
}
if(min!=i){
int tmp = data[min];
data[min]= data[i];
data[i]=tmp;
}
}
}

// 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

// Step 1: Read data from the file


ReadData(filename, data);

// Step 2: Sort the data using Interchange Sort


SelectionSort(data);

// Step 3: Display the first 'k' elements


for (int i = 0; i < k && (long unsigned int)i < data.size(); i++) {
cout << data[i] << endl;
}
}

You might also like