dimanche 29 avril 2018

Why is my sort shuffling the vector

typedef struct{
    long unsigned int line;
    long unsigned int column;
    double  value;
}sparse;



    void bubblec(int l, int r){
        int i,j, done;
        sparse t;
        for(i = l; i<r; i++){
            done = 1;
            for(j = r; j> i;j-- ){
                if(matrix[j].column < matrix[j-1].column){
                    t = matrix[j-1];
                    matrix[j-1] = matrix[j];
                    matrix[j] = t;
                    done = 0;
                }
            }
            if (done){
                break;
            }
        }
    }


void aux(int l, int r){
    int i;
    int auxx = 0;
    for(i = l; i<r; i++){
        if(matrix[i].line == matrix[i+1].line){auxx += 1;}
        else{
            if(auxx !=0){
            bubblec(auxx -i, i);
            auxx = 0;
            }
        }
    }   
}

aux(0, 7);

Guys I have this code that should sort my unidimensional vector that contains elements represented by sparse struct, first I sort by line ( tested it a million times and everything looked fine), after sorting by line I need to go to each line and sort it by column, My bubblec is bubble sort for the columns of the vector. Facing this problem i did an aux function to discover the position that lines are equal (after being sorted by lines) and sort that by columns.

The problem is that he doesn't sort it right, and shuffles my vector.

Imagine this matrix witch the first column are the lines of the struct and the second column are the columns of my struct

[0;3]
[0;1]
[2;2]
[2;0]   
[4;3]               <----- This is my input matrix,after sorted by lines I 
[4;1]                       Call the aux function with 0 (first element) 
[6;0]                       and 7 (the last element)

This is the matrix after i call the aux function that should sort each line by column

Aucun commentaire:

Enregistrer un commentaire