## News:

Message to All Guests
NB: Posting URL's See here: Posted URL Change

## How to sort counted(occurrence) numbers in increasing order?

Started by xcbart, April 27, 2019, 02:23:41 PM

#### xcbart

I have been given this question to create 50 random numbers from 1 to 15 and count how many times each occurred. I need to print them out in increasing order, but I couldn't. So far, I can only print out the numbers randomly with an increasing counter next to them. Any idea how I can print them out in order?

Thank you

`INCLUDE Irvine32.inc.datablank byte " ",0count byte 16 dup (0)    dates dword 50 dup (?).codemain PROC call randomizemov edx, offset blank    mov edi,0   ;count indexmov ecx,50 printnum:    mov eax,15    call randomRange        add eax,1    call writedec        mov dates[edi],eax;        add edi,4;    mov esi, eax    inc count[esi]        call writestring        movzx eax,count[esi]        call writedec    call writestring        mov eax,ecxloop printnumcall crlf exitmain ENDPend main`

#### aw27

Google for bubble sort numbers MASM

#### jimg

How would you do it with a pencil and the list of numbers on a piece of paper?
Do it that way.
The point of the exercise is to figure out which instructions to use to do the same thing manually.

#### Vortex

Hi xcbart,

You can use the qsort function supplied by the MS VC run-time library. As an exercise, try to study the rand() function, the pseudorandom number generator :

https://docs.microsoft.com/en-us/previous-versions/398ax69y(v%3Dvs.140)

`include qsort.incCompareProc PROTO C :DWORD,:DWORDNUMB_OF_ELEMENTS equ 10.datanumbers     dd 29,12,32,65,58,7,12,11,11,97format1     db '%d',13,10,0.codestart:    invoke  crt_qsort,ADDR numbers,\            NUMB_OF_ELEMENTS,\            SIZEOF DWORD,ADDR CompareProc    call    PrintArray    invoke  ExitProcess,0    CompareProc PROC C arg1:DWORD,arg2:DWORD    mov     ecx,arg1    mov     edx,arg2    mov     eax,DWORD PTR [ecx]    sub     eax,DWORD PTR [edx]    retCompareProc ENDPPrintArray  PROC uses esi ebx    mov     ebx,NUMB_OF_ELEMENTS    mov     esi,OFFSET numbers@@:    invoke  crt_printf,ADDR format1,\            DWORD PTR [esi]    add     esi,4    dec     ebx    jnz     @b    retPrintArray ENDPEND start`