2. Example
Example 01
Implementing the Book
class and a program that sorts a list of Book
objects using quicksort in C++.
1. Implementation of Book
Class
#include <iostream>#include <vector>#include <string>
class Book {private: int bookID; std::string bookName; std::string ISBN;
public: // Constructor Book() : bookID(0), bookName(""), ISBN("") {}
// Parameterized Constructor for easier initialization Book(int id, std::string name, std::string isbn) : bookID(id), bookName(name), ISBN(isbn) {}
// Destructor ~Book() {}
// Getters int GetBookID() { return bookID; } std::string GetBookName() { return bookName; } std::string GetISBN() { return ISBN; }
// Setters void SetBookID(int id) { bookID = id; } void SetBookName(std::string name) { bookName = name; } void SetBookISBN(std::string isbn) { ISBN = isbn; }
// Comparator for sorting based on bookID (or change this to sort by another field if needed) bool operator<(const Book& other) const { return bookID < other.bookID; }};
// Function to print the list of booksvoid PrintBooks(const std::vector<Book>& books) { for (const auto& book : books) { std::cout << "Book ID: " << book.GetBookID() << ", Name: " << book.GetBookName() << ", ISBN: " << book.GetISBN() << std::endl; }}
2. QuickSort Implementation
// QuickSort Partition functionint Partition(std::vector<Book>& books, int low, int high) { Book pivot = books[high]; // Choosing the last element as the pivot int i = low - 1;
for (int j = low; j < high; j++) { if (books[j] < pivot) { // Using the operator< defined in the Book class i++; std::swap(books[i], books[j]); } } std::swap(books[i + 1], books[high]); return i + 1;}
// QuickSort functionvoid QuickSort(std::vector<Book>& books, int low, int high) { if (low < high) { int pivotIndex = Partition(books, low, high); QuickSort(books, low, pivotIndex - 1); // Sort left of pivot QuickSort(books, pivotIndex + 1, high); // Sort right of pivot }}
3. Main Function to Test
int main() { // Creating a list of Book objects std::vector<Book> books = { Book(3, "C++ Programming", "123-A"), Book(1, "Algorithms", "456-B"), Book(5, "Data Structures", "789-C"), Book(2, "Operating Systems", "101-D"), Book(4, "Database Systems", "112-E") };
std::cout << "Books before sorting:\n"; PrintBooks(books);
// Sorting the list of books using QuickSort QuickSort(books, 0, books.size() - 1);
std::cout << "\nBooks after sorting by Book ID:\n"; PrintBooks(books);
return 0;}
4. Explanation
- The
Book
class encapsulates data likebookID
,bookName
, andISBN
. The getters and setters allow access and modification of these attributes. - The
QuickSort
algorithm is implemented using recursion, with aPartition
function to reorder elements around a pivot. - The
operator<
is overloaded for theBook
class to compareBook
objects bybookID
. You can modify this to sort by other attributes (e.g.,bookName
orISBN
). - The
PrintBooks
function outputs the sorted or unsorted list of books.
5. Output
Books before sorting:Book ID: 3, Name: C++ Programming, ISBN: 123-ABook ID: 1, Name: Algorithms, ISBN: 456-BBook ID: 5, Name: Data Structures, ISBN: 789-CBook ID: 2, Name: Operating Systems, ISBN: 101-DBook ID: 4, Name: Database Systems, ISBN: 112-E
Books after sorting by Book ID:Book ID: 1, Name: Algorithms, ISBN: 456-BBook ID: 2, Name: Operating Systems, ISBN: 101-DBook ID: 3, Name: C++ Programming, ISBN: 123-ABook ID: 4, Name: Database Systems, ISBN: 112-EBook ID: 5, Name: Data Structures, ISBN: 789-C