#include #include #include #include class ads_item { int data; public: // Counters for assignments and comparisons static size_t assignments_count, comparisons_count; // Constructor. Initialize items with random values. ads_item() { data = rand(); }; // Constructor with copying. ads_item(const ads_item & other) : data(other.data) { ++assignments_count; }; // Reset counters to 0. static void reset() { assignments_count = 0; comparisons_count = 0; } // Overloading comparison operation. bool operator<(const ads_item &other) const { ++comparisons_count; return data & vect) { sort(vect.begin(), vect.end()); } // Stable sort for vector data type. Using std library algorithm. void stablesort(std::vector & vect) { stable_sort(vect.begin(), vect.end()); } using namespace std; int main(int argc, char* argv[]) { // One argument needed (length of the array to be sorted). if (argc != 2) { cout << "Invalid number of arguments: " << argc-1 << " instead of 1." << endl; cout << "Please specify the length of the array to be sorted." << endl; return 1; } // argv[0] = program file name int array_len = atoi(argv[1]); // Initialize random number generator. srand(static_cast(time(NULL))); // Define identical vectors of the specified length. vector v_quick(array_len), v_stable(v_quick); // Display info about operations counts. cout << "Vectors created : " << array_len << " items\n"; cout << "Assignments : " << ads_item::assignments_count << " comparisons : "; cout << ads_item::comparisons_count << endl; // Run quicksort. ads_item::reset(); quicksort(v_quick); cout << "\nQuick sort: assignments : " << ads_item::assignments_count; cout << " comparisons : " << ads_item::comparisons_count << endl; // Run stablesort ads_item::reset(); stablesort(v_stable); cout << "\nStable sort: assignments : " << ads_item::assignments_count; cout << " comparisons : " << ads_item::comparisons_count << endl; // system("PAUSE"); return 0; }