Queue sorting

in the below code , if negatuve values are present in the queue, the output is not getting sorted. Why is this happening eventhough i have declared a signed array?

int array[$] = '{10,-1,2,3,4};

initial begin
array.sort();
$display(“sorted fixed”,array);
end

In reply to rakesh reddy:
Please use

(https://verificationacademy.com/forums/announcements/please-format-your-code-code-and/code-tags) to format your code. It also helps to make the code complete so that someone can just copy and paste it into a file an run it. For example:


``` verilog
module top;
   int array[$] = '{10,-1,2,3,4};
   
   initial begin
      array.sort();
      $display("sorted fixed-%0p",array);
   end
endmodule

Then you should show the results you are getting versus what you expected to see. In this case, I’m seeing exactly what was expected

[code]# sorted fixed-1 2 3 4 10

module top;
   int array[$] = '{10,-1,2,3,4};
  int array_sort [$] ; 
 
   initial begin
     array_sort=  array.sort();
     $display("sorted fixed",array_sort);
   end
endmodule

why can’t I store the sorted array in array_sort queue memory ?

In reply to MP:

The “sort” method operates on the original array, and doesn’t “return” a sorted array. It just sorts the original array in place. You will have to break down your above code to something like below.


module top;
  int array[$] = '{10,-1,2,3,4};
  int array_sort [$] ;

  initial begin
    //array_sort= array.sort();

    array_sort = array; 
    array_sort.sort();
   
    $display("sorted fixed = %p",array);
    $display("sorted fixed = %p",array_sort);
  end
endmodule

Thank you @killSteal .

I want to further play and understand with the queue no .

Say I have a queue = '{10,-1,1,1,4,2,3,4,6,4,3,2,4,6,7} a
and want to first the fourth largest no .

Can you help me with how to use array locator ?

module top;
int array[] = '{10,-1,1,1,4,2,3,4,6,4,3,2,4,6,7}; int array_sort [] ;
int array_uni [$];
int third_largest;

initial begin

array_uni = array.unique();
array_sort = array_uni ; 
array_sort.rsort(); 

// third_largest  = array_sort.find_index(item) with ( item.index ==3); 

$display("sorted fixed = %p",array);
$display("sorted fixed = %p",array_sort);
$display("unique = %p",array_uni) ; 
$display("third_largest =%0d",third_largest); 

end
endmodule

You are un-necessarily making it more complicated. You are over-thinking :)

When you sort an array, you are guaranteed to have the sorted array in order, so all you need is :

third_largest = array[2];

The idea of using a “find_index”, is that you don’t know the index. If you alread know the index (which is what you are doing in your above code), why do you even need a find_index ?
You would use find_index, when you know the elements you are looking for:

//Example
//index_of_10 = array_sort.find_index(item) with (item==3);

Hi Dave,

What is the internal sorting algorithm used in system verilog?
Similarly, i was curious to know the algorithms for most of the array manipulation methods and how they stand w.r.t space and time complexity? ( like reverse, reverse sort etc)

In reply to nithinbhargav:

SystemVerilog does not define the algorithms for anything—just the behavior the user is supposed to get. Tools that implement the standard have the freedom to use whatever algorithms they want (presumably ones that give the best performance for their users).