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

Source Codes

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

Source Codes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

 ANS(:-)

To complete the VectorSorting assignment, you'll need to implement both the selection sort and
quicksort algorithms in C++ and integrate them into a provided program that reads bids from a
CSV file and sorts them by their titles. Below are the steps to achieve this, including setting up
the project, implementing the sorting algorithms, and providing code reflections and pseudocode.
Step-by-Step Implementation
Step 1: Setup the Project
1. Create a New C++ Project:

 Open your C++ development environment (e.g., Eclipse, Visual Studio).


 Create a new project named “VectorSorting”.
 Ensure you select the correct compiler in Toolchains.

2. Add Project Files:

 Download the starter program files (provided in the zip file).


 Replace the auto-generated files in the /src directory of your project with
the downloaded files.
 Refresh the project in the Project Explorer pane to add all new files.

3. Compiler Settings:

 Since C++ 11 features are used, you may need to add the -std=c++11
compiler switch to your project settings.
Step 2: Implement Selection Sort

SelectionSort Method:
void selectionSort(vector<Bid>& bids) {
for (size_t i = 0; i < bids.size(); ++i) {
size_t minIndex = i;
for (size_t j = i + 1; j < bids.size(); ++j) {
if (bids[j].title < bids[minIndex].title) {
minIndex = j;
}
}
if (minIndex != i) {
swap(bids[i], bids[minIndex]);
}
}
}

Explanation:
o Iterate over each element in the vector.

o Find the smallest element in the unsorted portion.

o Swap the smallest element with the first element of the unsorted portion.

o Repeat until the entire vector is sorted.


Step 3: Implement QuickSort

QuickSort Methods:
int partition(vector<Bid>& bids, int begin, int end) {
int low = begin + 1;
int high = end;
Bid pivot = bids[begin];
while (true) {
while (low <= high && bids[high].title >= pivot.title) {
high--;
}
while (low <= high && bids[low].title <= pivot.title) {
low++;
}
if (low <= high) {
swap(bids[low], bids[high]);
} else {
break;
}
}
swap(bids[begin], bids[high]);
return high;
}

void quickSort(vector<Bid>& bids, int begin, int end) {


if (begin >= end) {
return;
}
int mid = partition(bids, begin, end);
quickSort(bids, begin, mid - 1);
quickSort(bids, mid + 1, end);
}

Explanation:
o Partition: Choose a pivot and rearrange elements such that those less than the
pivot are on its left and those greater are on its right.
o QuickSort: Recursively apply the partition to subarrays until the entire array is
sorted.

 Step 2

Step 4: Integrate Sorting Methods in the Main Program

Update the main method to allow the user to select and execute these sorting methods:
int main() {
vector<Bid> bids;

// Load bids from CSV


loadBids("eBid_Monthly_Sales.csv", bids);

int choice = 0;
while (choice != 9) {
cout << "Menu:" << endl;
cout << "1. Load Bids" << endl;
cout << "2. Display All Bids" << endl;
cout << "3. Selection Sort All Bids" << endl;
cout << "4. QuickSort All Bids" << endl;
cout << "9. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;

switch (choice) {
case 1:
loadBids("eBid_Monthly_Sales.csv", bids);
break;
case 2:
displayBids(bids);
break;
case 3:
selectionSort(bids);
displayBids(bids);
break;
case 4:
quickSort(bids, 0, bids.size() - 1);
displayBids(bids);
break;
case 9:
cout << "Goodbye." << endl;
break;
default:
cout << "Invalid choice." << endl;
}
}

return 0;
}

Explanation:
The simplified C++ code snippet provided represents a console application that manages a list of
bids for auctions. The program presents the user with a menu that allows them to choose to load
a list of bids from a CSV file, display all loaded bids, sort the list of bids using either a selection
sort or a quicksort algorithm, or exit the program. The switch-case structure facilitates the
selection of the user's choice, executing the corresponding function. The main loop continues to
prompt for choices from the user until the exit option (choice 9) is selected.
Code Reflection
The purpose of this assignment was to implement and compare the performance of selection sort
and quicksort algorithms. Selection sort is a simple, yet inefficient O(n^2) algorithm suitable for
small datasets. Quicksort, on the other hand, is an efficient O(n log n) algorithm suitable for
larger datasets. Implementing these algorithms provided hands-on experience with sorting and
helped understand their strengths and weaknesses.
Pseudocode
Selection Sort:
for each element in the array
find the minimum element in the unsorted portion
swap it with the first unsorted element
end for
QuickSort:
function quickSort(array, low, high)
if low >= high then return
pivotIndex = partition(array, low, high)
quickSort(array, low, pivotIndex - 1)
quickSort(array, pivotIndex + 1, high)
end function

function partition(array, low, high)


pivot = array[low]
left = low + 1
right = high
while true
while left <= right and array[right] >= pivot
right = right - 1
while left <= right and array[left] <= pivot
left = left + 1
if left <= right then
swap(array[left], array[right])
else
break
swap(array[low], array[right])
return right
end function

 Answer
Conclusion:
By completing this assignment, we implemented two different sorting algorithms and integrated
them into a C++ program to handle and sort bid data. This exercise reinforced understanding of
sorting algorithms, their implementation, and their performance characteristics. The key
takeaway is the importance of choosing the right algorithm based on the dataset size and required
efficiency.

You might also like