John D'Amore
John D'Amore

Reputation: 11

Creating an ascending sorting function

I am trying to figure out how to sort an array of 10000 random integers and plot it into a graph. Technically, the code does sort in ascending order however somewhere during the sorting process it resets a bunch of the values to be the same. Where it looks like a step function where it should just be a continuous line.

 A;  
 for j = 1 : Nx-1    
     minvalue = A(j) ; 
     pointerminvalue = j ;
    for i = j : Nx        
        if (A(i) < minvalue )         
            minvalue = A(i)  ;         
            pointerminvalue = i ;     
        end
    end
    maxvalue = A(j) ;     
    A(j) = A(pointerminvalue) ;    
    A(pointerminvalue) = minvalue ;  
    % dont put the end here 
    A ;  
 end

Any know where I am possibly making the mistake?

Upvotes: 0

Views: 37

Answers (1)

Daniel
Daniel

Reputation: 36710

The error is here:

maxvalue = A(j) ;     
A(j) = A(pointerminvalue) ;    
A(pointerminvalue) = minvalue ; 

You are using two different variable names, change it to

H = A(j) ;     
A(j) = A(pointerminvalue) ;    
A(pointerminvalue) = H ; 

Upvotes: 1

Related Questions