Skip to content

Commit f92ba28

Browse files
authored
Merge pull request #428 from jvdp1/issue_sorting
Issue with stdlib_sorting
2 parents d2ac7ae + 3a4ba03 commit f92ba28

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

src/stdlib_sorting_ord_sort.fypp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ contains
158158
do j=1, size(array, kind=int_size)-1
159159
key = array(j)
160160
i = j - 1
161-
do while( i >= 0 .and. array(i) ${signt}$ key )
161+
do while( i >= 0 )
162+
if ( array(i) ${signoppt}$= key ) exit
162163
array(i+1) = array(i)
163164
i = i - 1
164165
end do
@@ -518,7 +519,8 @@ contains
518519
do j=1, size(array, kind=int_size)-1
519520
key = array(j)
520521
i = j - 1
521-
do while( i >= 0 .and. array(i) ${signt}$ key )
522+
do while( i >= 0 )
523+
if ( array(i) ${signoppt}$= key ) exit
522524
array(i+1) = array(i)
523525
i = i - 1
524526
end do

src/stdlib_sorting_sort_index.fypp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ contains
177177
key = array(j)
178178
key_index = index(j)
179179
i = j - 1
180-
do while( i >= 0 .and. array(i) > key )
180+
do while( i >= 0 )
181+
if ( array(i) <= key ) exit
181182
array(i+1) = array(i)
182183
index(i+1) = index(i)
183184
i = i - 1
@@ -585,7 +586,8 @@ contains
585586
key = array(j)
586587
key_index = index(j)
587588
i = j - 1
588-
do while( i >= 0 .and. array(i) > key )
589+
do while( i >= 0 )
590+
if ( array(i) <= key ) exit
589591
array(i+1) = array(i)
590592
index(i+1) = index(i)
591593
i = i - 1

src/tests/sorting/test_sorting.f90

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ program test_sorting
179179
subroutine test_int_ord_sorts( ltest )
180180
logical, intent(out) :: ltest
181181

182-
logical :: ldummy
182+
integer(int64) :: i
183+
integer, allocatable :: d1(:)
184+
logical :: ldummy
183185

184186
ltest = .true.
185187

@@ -202,6 +204,13 @@ subroutine test_int_ord_sorts( ltest )
202204
call test_int_ord_sort( rand10, "Random 10", ldummy )
203205
ltest = (ltest .and. ldummy)
204206

207+
!triggered an issue in insertion_sort
208+
d1 = [10, 2, -3, -4, 6, -6, 7, -8, 9, 0, 1, 20]
209+
call ord_sort( d1 )
210+
call verify_sort( d1, ldummy, i )
211+
ltest = (ltest .and. ldummy)
212+
213+
205214
end subroutine test_int_ord_sorts
206215

207216

@@ -418,7 +427,9 @@ end subroutine test_string_ord_sort
418427
subroutine test_int_sorts( ltest )
419428
logical, intent(out) :: ltest
420429

421-
logical :: ldummy
430+
integer(int64) :: i
431+
integer, allocatable :: d1(:)
432+
logical :: ldummy
422433

423434
ltest = .true.
424435

@@ -441,6 +452,12 @@ subroutine test_int_sorts( ltest )
441452
call test_int_sort( rand10, "Random 10", ldummy )
442453
ltest = (ltest .and. ldummy)
443454

455+
!triggered an issue in insertion
456+
d1 = [10, 2, -3, -4, 6, -6, 7, -8, 9, 0, 1, 20]
457+
call sort( d1 )
458+
call verify_sort( d1, ldummy, i )
459+
ltest = (ltest .and. ldummy)
460+
444461
end subroutine test_int_sorts
445462

446463
subroutine test_int_sort( a, a_name, ltest )
@@ -623,7 +640,10 @@ end subroutine test_string_sort
623640
subroutine test_int_sort_indexes( ltest )
624641
logical, intent(out) :: ltest
625642

626-
logical :: ldummy
643+
integer(int64) :: i
644+
integer(int32), allocatable :: d1(:)
645+
integer(int64), allocatable :: index(:)
646+
logical :: ldummy
627647

628648
ltest = .true.
629649

@@ -646,6 +666,12 @@ subroutine test_int_sort_indexes( ltest )
646666
call test_int_sort_index( rand10, "Random 10", ldummy )
647667
ltest = (ltest .and. ldummy)
648668

669+
d1 = [10, 2, -3, -4, 6, -6, 7, -8, 9, 0, 1, 20]
670+
allocate( index(size(d1)) )
671+
call sort_index( d1, index )
672+
call verify_sort( d1, ldummy, i )
673+
ltest = (ltest .and. ldummy)
674+
649675
end subroutine test_int_sort_indexes
650676

651677
subroutine test_int_sort_index( a, a_name, ltest )

0 commit comments

Comments
 (0)