I have an algorithm to sort a 2d array using the selection sort method. I have two variables to store the total number of permutations and the total number of comparisons after the matrix is sorted. Everything almost works well just that the I am getting false values for the variables (selection_count and selection_perm_count) I created. What am I doing wrong?
#include <iostream>
void sort(int **arr, int n, int m, int &selectionCount, int &spermuTation)
{
int min_idx;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m - 1; j++)
{
min_idx = j;
for (int k = j; k < m; k++)
{
if (arr[i][k] > arr[i][min_idx])
{
min_idx = j;
spermuTation++;
}
}
if (min_idx != j)
{
int tempVal = arr[i][min_idx];
arr[i][min_idx] = arr[i][j];
arr[i][j] = tempVal;
}
}
}
cout << endl;
cout << "Selection sort" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << "[" << i << "][" << j << "]: ";
cout << arr[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int **matrix,N,M;
//some code for user to input matrix size and matrix allocation...etc.
int selection_count = 0;
int selection_perm_count = 0;
sort(matrix, N, M, selection_count, selection_perm_count);
cout << endl << "Number of comparisons: " << selection_count;
cout << endl << "Number of permutations: " << selection_perm_count << endl;
}