How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Fortran programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Fortran programming language

Table of Contents

Problem Statement

In general, sleep sort works by starting a separate task for each item to be sorted, where each task sleeps for an interval corresponding to the item's sort key, then emits the item. Items are then collected sequentially in time. Task: Write a program that implements sleep sort. Have it accept non-negative integers on the command line and print the integers in sorted order. If this is not idomatic in your language or environment, input and output may be done differently. Enhancements for optimization, generalization, practicality, robustness, and so on are not required. Sleep sort was presented anonymously on 4chan and has been discussed on Hacker News.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Fortran programming language

Source code in the fortran programming language

program sleepSort
    use omp_lib
    implicit none
    integer::nArgs,myid,i,stat
    integer,allocatable::intArg(:)
    character(len=5)::arg

    !$omp master
    nArgs=command_argument_count()
    if(nArgs==0)stop ' : No argument is given !'
    allocate(intArg(nArgs))
    do i=1,nArgs
        call get_command_argument(i, arg)
	read(arg,'(I5)',iostat=stat)intArg(i)
	if(intArg(i)<0 .or. stat/=0) stop&
        &' :Only 0 or positive integer allowed !'
    end do
    call omp_set_num_threads(nArgs)
    !$omp end master
 

    !$omp parallel private(myid)
    myid =omp_get_thread_num()
    call sleepNprint(intArg(myid+1))
    !$omp end parallel

  contains
	subroutine sleepNprint(nSeconds)
	    integer::nSeconds
            call sleep(nSeconds)
	    print*,nSeconds
	end subroutine sleepNprint
end program sleepSort


  

You may also check:How to resolve the algorithm Least common multiple step by step in the SenseTalk programming language
You may also check:How to resolve the algorithm Sokoban step by step in the Phix programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the Picat programming language
You may also check:How to resolve the algorithm Particle fountain step by step in the Perl programming language
You may also check:How to resolve the algorithm GUI enabling/disabling of controls step by step in the Julia programming language