You are using a version without Ads of this website. Please, consider donating:

Quick Sort

Hi,
I am having trouble getting my quick sort to work. Any Help would be appreciated.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170`` ``````#include #include #include // for exit(1). using namespace std; class rational { friend ostream& operator<<(ostream &, rational &); friend istream& operator>>(istream &, rational &); public: rational(); rational(int, int); rational operator+(const rational &) const; rational operator-(const rational &) const; // post-condition: result of (calling object - explicitly passed object) is returned void operator*=(const rational &); bool operator<(const rational &); // post-condition: if calling obj is smaller, returns true; returns false otherwise; num and denom can be positive or negative private: int GCD(); int num; int denom; }; rational::rational() { num = 0; denom = 1; } rational::rational(int n, int d) { if (d == 0) { cout << "Denominator cannot be zero, program is terminated." << endl; exit(1); } num = n; denom = d; } rational rational::operator+(const rational &obj2) const { rational sum; // rational type! stores sum of two rational numbers or objects sum.num = (*this).num * obj2.denom + obj2.num * (*this).denom; sum.denom = (*this).denom * obj2.denom; return sum; } rational rational::operator-(const rational &obj) const { rational diff; // rational type! stores "difference" of two rational numbers or objects diff.num = (*this).num * obj.denom - obj.num * (*this).denom; diff.denom = (*this).denom * obj.denom; return diff; } void rational::operator*=(const rational &obj) { rational temp; // for clarity; the below code can be simpler temp.num = num * obj.num; temp.denom = denom * obj.denom; *this = temp; } int rational::GCD() { int n1 = abs(num), n2 = abs(denom), remainder; remainder = n1 % n2; while(remainder != 0) { n1 = n2; n2 = remainder; remainder = n1 % n2; } return n2; } bool rational::operator<(const rational &obj) { rational diff = *this - obj; // makes use of the overloaded "-" operator if (static_cast(diff.num) / static_cast(diff.denom) < 0) return true; else return false; } ostream& operator<<(ostream &out, rational &obj) // return an ostream object by reference allows calls to be cascaded { // notice that parameter obj cannot be const because it values will be changed int gcd = obj.GCD(); obj.num /= gcd; obj.denom /= gcd; out << obj.num << "/" << obj.denom; return out; } istream& operator>>(istream &in, rational &obj) { in >> obj.num >> obj.denom; return in; } void quickSort(rational r[], int left,int right); // pre-condition: unsorted array and # of elements in the array are passed to the function // post-condition: array is sorted in ascending order void quickSort(int arr[], int left, int right) { int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2]; /* partition */ while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } }; /* recursion */ if (left < j) quickSort(arr, left, j); if (i < right) quickSort(arr, i, right); } int getInput(rational [], int); int getInput(rational r[], int n) { int i = 0; char ans; cout << "Each rational number consists of two integers: an numerator and a denominator." << endl; cout << "You may enter up to " << n << " rational numbers:" << endl; do { cout << "Enter a rational number: "; cin >> r[i++]; cout << "More rational numbers to enter? (Y/N): "; cin >> ans; } while (ans == 'Y' || ans == 'y'); return i; } int main() { const int SIZE = 10; rational r[SIZE]; int n; n = getInput(r, SIZE); cout << "\nYou have entered " << n << " ratrional numbers." << endl; cout << "\n\nBefore sorting, the list of rationals are: " << endl; for (int i = 0; i < n; i++) cout << r[i] << " "; quickSort(r, n); cout << "\n\nAfter sorting, the list of rationals are: " << endl; for (int i = 0; i < n; i++) cout << r[i] << " "; cout << endl; system("pause"); return 0; }``````
What is the trouble you are facing ?
Topic archived. No new replies allowed.