Jump to content

Dutch national flag problem: Difference between revisions

m
no edit summary
mNo edit summary
mNo edit summary
Line 27:
generate_unsorted_sequence
importing
i_length lenght_of_sequence type int4
returning
value(r_unsorted_sequenceunsorted_sequence) type string,
 
sort_sequence
importingchanging
i_sequence sequence_to_be_sorted type string,
returning
value(r_sorted_sequence) type string,
 
is_sorted
importing
i_sequence sequence_to_check type string
returning
value(r_sortedsorted) type abap_bool.
endinterface.
 
Line 53 ⟶ 51:
constants:
begin of dutch_flag_colors,
red type char1 value `'R`',
white type char1 value `'W`',
blue type char1 value `'B`',
end of dutch_flag_colors.
endclass.
Line 67 ⟶ 65:
max = 2 ).
 
do i_lengthlenght_of_sequence - 1 times.
data(random_sequence) = ``.
 
do i_length - 1 times.
data(random_int) = random_int_generator->get_next( ).
 
Line 77 ⟶ 73:
when random_int eq 2 then dutch_flag_colors-blue ).
 
random_sequenceunsorted_sequence = |{ random_sequenceunsorted_sequence }{ next_color }|.
enddo.
 
if strlen( random_sequenceunsorted_sequence ) > 0.
random_int = random_int_generator->get_next( ).
 
Line 87 ⟶ 83:
when random_int eq 1 then dutch_flag_colors-white ).
 
random_sequenceunsorted_sequence = |{ random_sequenceunsorted_sequence }{ next_color }|.
endif.
 
r_unsorted_sequence = random_sequence.
endmethod.
 
 
method sorting_problem~sort_sequence.
data(sequence) = i_sequence.
data(low_index) = 0.
data(middle_index) = 0.
data(high_index) = strlen( sequencesequence_to_be_sorted ) - 1.
 
while middle_index <= high_index.
data(current_color) = sequencesequence_to_be_sorted+middle_index(1).
 
if current_color eq dutch_flag_colors-red.
data(buffer) = sequencesequence_to_be_sorted+low_index(1).
 
sequencesequence_to_be_sorted = replace(
val = sequencesequence_to_be_sorted
off = middle_index
len = 1
with = buffer ).
 
sequencesequence_to_be_sorted = replace(
val = sequencesequence_to_be_sorted
off = low_index
len = 1
Line 122 ⟶ 115:
middle_index = middle_index + 1.
elseif current_color eq dutch_flag_colors-blue.
buffer = sequencesequence_to_be_sorted+high_index(1).
 
sequencesequence_to_be_sorted = replace(
val = sequencesequence_to_be_sorted
off = middle_index
len = 1
with = buffer ).
 
sequencesequence_to_be_sorted = replace(
val = sequencesequence_to_be_sorted
off = high_index
len = 1
Line 141 ⟶ 134:
endif.
endwhile.
 
r_sorted_sequence = sequence.
endmethod.
 
 
method sorting_problem~is_sorted.
r_sortedsorted = abap_true.
 
do strlen( i_sequencesequence_to_check ) - 1 times.
data(current_character_index) = sy-index - 1.
data(current_color) = i_sequencesequence_to_check+current_character_index(1).
data(next_color) = i_sequencesequence_to_check+sy-index(1).
 
r_sortedsorted = cond abap_bool(
when ( current_color eq dutch_flag_colors-red and
( next_color eq current_color or
Line 166 ⟶ 157:
( current_color eq dutch_flag_colors-blue and
current_color eq next_color )
then r_sortedsorted
else abap_false ).
 
check r_sortedsorted eq abap_false.
return.
enddo.
Line 181 ⟶ 172:
dutch_national_flag_problem = new dutch_national_flag_problem( ).
 
data(unsorted_sequencesequence) = dutch_national_flag_problem->generate_unsorted_sequence( 20 ).
 
data(sorted_sequence)write:|{ =sequence }, is sorted? -> { dutch_national_flag_problem->sort_sequenceis_sorted( unsorted_sequencesequence ) }|, /.
 
write:|{ unsorted_sequence }, is sorted? -> { dutch_national_flag_problem->is_sortedsort_sequence( unsorted_sequencechanging )sequence_to_be_sorted }|,= /sequence ).
 
write:|{ sorted_sequencesequence }, is sorted? -> { dutch_national_flag_problem->is_sorted( sorted_sequencesequence ) }|, /.
</lang>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.