Saturday, August 1, 2020

Sorting array - descending order using 1 and 2 for loops.

Code
module top;
  int a[5] = {1,2,3,4,5};
  int tmp;
 
  initial begin //{
    // 2 loops
    for(int i=0; i< $size(a)-1;i++) begin //{
      for(int j=i+1;j <$size(a);j++) begin //{
        if(a[i] < a[j]) begin //{
          tmp  = a[i];
          a[i] = a[j];
          a[j] = tmp;
        end //}
      end //}
    end //}
    $display("Array:%p",a);
   
    // 1 loop
    a[5] = {1,2,3,4,5};
    for(int i=0; i<$size(a)-1;i++) begin//{
      if(a[i] < a[i+1]) begin //{
        tmp    = a[i];
        a[i]   = a[i+1];
        a[i+1] = tmp;
        i      = -1;
      end //}
    end //}
    $display("Array:%p",a);

  end //}
endmodule:top

Result
CPU time: .230 seconds to compile + .328 seconds to elab + .297 seconds to link
Chronologic VCS simulator copyright 1991-2019
Contains Synopsys proprietary information.
Compiler version P-2019.06-1; Runtime version P-2019.06-1; Aug 2 01:52 2020
Array:'{5, 4, 3, 2, 1}
Array:'{5, 4, 3, 2, 1}
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.640 seconds; Data structure size: 0.0Mb
Sun Aug 2 01:52:17 2020

No comments:

Post a Comment

Generating prime numbers between 1 to 100

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class test ; int prime_q[$]; function voi...