Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct qtys {
- int comparisons;
- int assignments;
- };
- void bubbleSort(vector <int> &array, int passesQty, qtys &_qtys) {
- for (int i = array.size() - 1, j = 0; j != passesQty; i--, j++) {
- for (int k = 0; k < i; k++) {
- _qtys.comparisons++;
- if (array[k + 1] < array[k]) {
- int temp = array[k + 1];
- array[k + 1] = array[k];
- array[k] = temp;
- _qtys.assignments += 3;
- }
- }
- }
- }
- void sortInterval (vector <int> &array, int limit, int l, int r, qtys &_qtys) {
- if (r - l + 1 > limit) {
- int pivot = array[(r + l) / 2],
- i = l,
- j = r;
- do {
- _qtys.comparisons += 2;
- while (array[i] < pivot) {
- _qtys.comparisons++;
- i++;
- }
- while (array[j] > pivot) {
- _qtys.comparisons++;
- j--;
- }
- if (i <= j) {
- int temp = array[j];
- array[j] = array[i];
- array[i] = temp;
- i++;
- j--;
- _qtys.assignments += 3;
- }
- } while (i <= j);
- if (l < j) {
- sortInterval(array, limit, l, j, _qtys);
- }
- if (r > i) {
- sortInterval(array, limit, i, r, _qtys);
- }
- }
- }
- void quickSort(vector <int> &array, int limit, qtys &_qtys) {
- sortInterval(array, limit, 0, array.size() - 1, _qtys);
- bubbleSort(array, limit, _qtys);
- }
Advertisement
Add Comment
Please, Sign In to add comment