'2-dimensional array, merge sort
I tried to sort the array, but I need to sort by merge sort, but it doesn't work correctly.
Please can anyone help me with that?
before I don't see any examples for merge-sort with a 2-dimensional array in turbo c++, so it is hard for me.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
void swap(int * a, int * b) {
int buf = * a;
* a = * b;
* b = buf;
}
int partition(int ** A, int l, int r, int c) {
int i;
int * pivot_val = A[l / c, l % c];
swap(A[l / c, l % c], A[r / c, r % c]);
int j = l;
for (i = l; i < r; i++) {
if (A[i / c, i % c] <= pivot_val) {
swap(A[i / c, i % c], A[j / c, j % c]);
j++;
}
}
swap(A[j / c, j % c], A[r / c, r % c]);
return j;
}
void quicksort_r(int ** A, int l, int r, int c) {
if (l < r) {
int pivot = partition(A, l, r, c);
quicksort_r(A, l, pivot - 1, c);
quicksort_r(A, pivot + 1, r, c);
}
}
void quicksort(int ** A, int rows, int columns) {
quicksort_r(A, 0, rows * columns - 1, columns);
}
void main() {
int rows, columns;
int i, j;
clrscr();
cout << "Write count of rows: ";
cin >> rows;
cout << "Write count of columns: ";
cin >> columns;
int ** A = new int[rows, columns];
randomize();
for (j = 0; j < columns; j++) {
for (i = 0; i < rows; i++) {
A[rows, columns] = random(100);
cout << "A[" << i << "][" << j << "] = " << A[rows, columns] << "\n";
}
}
quicksort( * A, rows, columns);
getch();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|