Certainly! Here's an updated version of the code with explanations for each part:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
class Student {
private:
std::string name;
int age;
public:
void setName(const std::string& newName) {
name = newName;
}
void setAge(int newAge) {
age = newAge;
}
std::string getName() const {
return name;
}
int getAge() const {
return age;
}
void print() const {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
// Overload the < operator to compare student names
bool operator<(const Student& other) const {
return name < other.name;
}
};
// Implementation of the bubble sort algorithm
template<typename T>
void bubbleSort(std::vector<T>& arr) {
int n = arr.size();
int comparisons = 0;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
comparisons++; // Increment comparison count
if (arr[j] < arr[j+1]) {
std::swap(arr[j], arr[j+1]);
}
}
}
std::cout << "Bubble Sort comparisons: " << comparisons << std::endl;
}
// Implementation of the shell sort algorithm
template<typename T>
void shellSort(std::vector<T>& arr) {
int n = arr.size();
int comparisons = 0;
for (int gap = n/2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
T temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] < temp; j -= gap) {
comparisons++; // Increment comparison count
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
std::cout << "Shell Sort comparisons: " << comparisons << std::endl;
}
// Implementation of the merge sort algorithm
template<typename T>
void merge(std::vector<T>& arr, int left, int middle, int right, int& comparisons) {
int i, j, k;
int n1 = middle - left + 1;
int n2 = right - middle;
std::vector<T> L(n1), R(n2);
for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[middle + 1 + j];
i = 0; j = 0; k = left;
while (i < n1 && j < n2) {
comparisons++; // Increment comparison count
if (L[i] < R[j]) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
}
while (i < n1) {
arr[k++] = L[i++];
}
while (j < n2) {
arr[k++] = R[j++];
}
}
template<typename T>
void mergeSortUtil(std::vector<T>& arr, int left, int right, int& comparisons) {
if (left < right) {
int middle = left + (right - left) / 2;
mergeSortUtil(arr, left, middle, comparisons);
mergeSortUtil(arr, middle + 1, right, comparisons);
merge(arr, left, middle, right, comparisons);
}
}
template<typename T>
void mergeSort(std::vector<T>& arr) {
int comparisons = 0;
mergeSortUtil(arr, 0, arr.size() - 1, comparisons);
std::cout << "Merge Sort comparisons: " << comparisons << std::endl;
}
int binarySearch(const std::vector<Student>& arr, const std::string& name) {
int left = 0;
int right = arr.size() - 1;
while (left <= right) {
int middle = left + (right - left) / 2;
if (arr[middle].getName() == name) {
return middle;
} else if (arr[middle].getName() < name) {
left = middle + 1;
} else {
right = middle - 1;
}
}
return -1;
}
int main() {
std::ifstream inputFile("students.txt");
int numStudents;
inputFile >> numStudents;
std::vector<Student> a1, a2, a3;
for (int i = 0; i < numStudents; i++) {
std::string name;
int age;
inputFile >> name >> age;
Student student;
student.setName(name);
student.setAge(age);
a1.push_back(student);
a2.push_back(student);
a3.push_back(student);
}
inputFile.close();
bubbleSort(a1);
shellSort(a2);
mergeSort(a3);
std::ofstream a1File("BubbleSort.txt");
for (const Student& student : a1) {
a1File << student.getName() << " " << student.getAge() << std::endl;
}
a1File.close();
std::ofstream a2File("ShellSort.txt");
for (const Student& student : a2) {
a2File << student.getName() << " " << student.getAge() << std::endl;
}
a2File.close();
std::ofstream a3File("MergeSort.txt");
for (const Student& student : a3) {
a3File << student.getName() << " " << student.getAge() << std::endl;
}
a3File.close();
std::string searchName;
std::cout << "Enter a student's name to search: ";
std::cin >> searchName;
int index = binarySearch(a1, searchName);
if (index != -1) {
std::cout << "Found at index " << index << std::endl;
a1[index].print();
} else {
std::cout << "Student not found." << std::endl;
}
return 0;
}
Now let's go through the code step by step:
-
Implement the class setter and print member function:
- The
Student class has been implemented with setter functions setName() and setAge(), and a print function print().
-
Overload the < operator:
- The
< operator has been overloaded in the Student class to compare student names.
-
Implement template functions:
- Three template sorting functions have been implemented:
bubbleSort(), shellSort(), and mergeSort(). Each function takes a vector of elements as input and sorts them using the respective algorithm. The number of comparisons made during sorting is also counted and displayed.
-
Read into dynamic arrays of student objects from a file:
- The
students.txt file is read, and the number of students is stored in the numStudents variable.
- Three dynamic arrays (
a1, a2, and a3) of Student objects are created, and each student's information is read from the file and stored in the arrays.
-
Sort the arrays using different sorting algorithms:
- The three arrays (
a1, a2, and a3) are sorted using the three implemented sorting algorithms: bubbleSort(), shellSort(), and mergeSort().
-
Save the sorted arrays in separate text files:
- The sorted arrays are saved in three separate text files: "BubbleSort.txt", "ShellSort.txt", and "MergeSort.txt", as per the requirement.
-
Count comparisons for each sorting algorithm:
- The number of comparisons made during each sorting algorithm is counted and displayed.
-
Implement a binary search algorithm:
- The binarySearch() function is implemented to perform a binary search on the sorted arrays. It returns the index of the found student or -1 if not found.
-
Main function implementation:
- The main function reads the student data from the file, sorts each array using the respective sorting algorithm, saves the sorted arrays to separate text files, and performs a search on the first array using the binary search algorithm.
That's it! This code should fulfill the requirements you mentioned.