I'm trying to search the minimal element in a adjacency matrix. For get this, I want to sort elements row to row.
To optimize the sorting, I want only sort the significant elements of this vector, as this form:
[0, 5, 9, 7]
[inf, 0, 6, 3]
[inf, inf, 0, 12]
[inf, inf, inf, 0]
The sorted matrix must be this:
[0, 5, 7, 9]
[inf, 0, 3, 6]
[inf, inf, 0, 12]
[inf, inf, inf, 0]
I'm trying to use std::vector<int>
and the C++ sort
function, as this form, but it fails.
When I try to execute with 6 elements, the program show that error:
malloc(): memory corruption (fast): 0x000000000cb12490
And with other quantities, some times produces similar errors or return -1
My matrix is implemented with vector<vector<int> >
//Sort matrix row to row
for(int i = 0; i < numnodes; i++){
sort(matrix[i].begin()+1+i, matrix[i].end());
if(matrix[i][i+1] < minimal){
minimal = matrix[i][i+1];
row = i;
}
}`
How can I solve this?