News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Selection Sort

Started by Locosis, December 09, 2009, 03:12:53 AM

Previous topic - Next topic

Locosis

I am having a problem with sorting the last two elements of my array using selection sort and I cant figure out how o fix it. My code is: http://paste.pocoo.org/show/155855/

Locosis

I really got it this time. Code:

; Selection Sort
; by Jonathan Sims
; 12/8/2009

.386
.MODEL FLAT
INCLUDE io.h
.STACK  4096

.DATA
ssArray       DWORD   10,25,47,69,95,50,16,84,9,20,3,32,63,37,77,83,8,12,28,15
address      DWORD   ?
answer      BYTE   12 DUP (?)   ;Output temps.
line      BYTE   10, 13, 0
exit      BYTE   "push e to exit...", 0
string      BYTE   40 DUP (?)  ;String temps.

.CODE
_start    PROC
        lea   eax, ssArray    ; parameter
        push  eax
      mov     address, eax  ;saves address
      ; printing unsorted array
      mov ecx, 20
      printing_us_loop:
         dtoa answer, [eax]
         output answer
         add eax, 4
      loop printing_us_loop
      
        call  sSort          ; sSort(nbrArray)
        add   esp, 4          ; remove DWORD parameter from stack
      
      output line
      output line
      output line
      
      lea eax, ssArray
      
      ; printing sorted array
      mov ecx, 20
      printing_s_loop:
         dtoa answer, [eax]
         output answer
         add eax, 4
      loop printing_s_loop

      output line
      output   exit      ; push e to exit
      input string, 40    ; wait for e

quit:   mov   eax, 0        ; exit with return code 0
        ret
_start    ENDP

;Using Selection Sort to sort array(sArray)
sSort  PROC
   push     ebp           ; save base pointer
   mov      ebp,esp       ; establish stack frame
   push     eax           ; save registers
   push     ebx
   push     ecx
   push     edx
   push     esi
   push    edi

   mov esi,[ebp+8]   ; get address of array arr
   mov   ecx, esi
   add   ecx, 4
   mov   ah, 0
   mov al, 0

   forLoop1:               ; python code:for i in range(0, len (list2)):
                        ;   min = i
        cmp      ah, 20
      je      endIfLarger    ; skip if not
      mov    al,   ah
      mov      edx, [esi]       ; min = i


      forLoop2:            ;   for j in range(i + 1, len(list2)):
                        ;      if list2[j] < list2[min]:
                        ;         min = j
         cmp      al, 20
         je      endforLoop2 ; skip if not
         inc      al         ; pointer that keeps track of where you are in the array
         
         add      ecx, 4       ; point at next array element
         
         cmp      edx, [ecx]  ; a < min?
         jnl      if_less      ; new min.
         jmp      forLoop2   ; no new min
         if_less:
            mov   edx, [ecx]  ;
            
         xchg     [esi], edx       ; switching smallest with the larger number
         xchg   [ecx], edx
                        ; list2, list2[min] = list2[min], list2 # swap
                        ; placing larger number where smaller number was
         jmp   forLoop2         ; found new min

         
      endforLoop2:
         inc ah
         add esi, 4         ; points at next array element
         mov   ecx, esi      ; puts esi where ecx is
         add ecx, 4           ; point at next array element
         jmp   forLoop1      ; continues first for loop
         
      
endIfLarger:


exitCode:
   pop    esi          ; restore registers
   pop    edx
   pop    ecx
   pop    ebx
   pop    eax
   pop    ebp
   pop      edi
   ret                 ; return
sSort  ENDP
END

dedndave

i dunno
maybe i am missing something - lol
but that looks like a hell of a lot of code to find the min and max values in an array

ecube

yeh all you really need to do is loop through each array entry and compare if it's larger or smaller than the largest or smallest values you have saved prior, if you have none saved or it is larger or smaller then replace respectively, real simple stuff.

sinsi

I thought it was more bubble sort homework.
Light travels faster than sound, that's why some people seem bright until you hear them.

dedndave

i think he is using the min/max search to sort with
which - doesn't sound very efficient
perhaps it is an exercise merely for comparison
even so, it looks like about 2 or 3 times too many instructions

hutch--

it could do with the preservation of EAX, ECX and EDX being removed as well, whoever wrote it did not understand the specification for registers.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

FORTRANS

HI,

   I would double check your index variables.  If you check
against 20, and you start with zero, you are going past the
end of your array.  (At least at first look that is the most
suspicious looking.)

Steve N.