1 / 27

ELE22MIC Lecture 23

ELE22MIC Lecture 23. Search Methods Comparison of Integers & Strings Sort Methods Bubble Sort Review of Assignment 1 & 2. Searching an unsorted array. Searching is the activity of finding one or more items in a list that match a given search criteria.

sauda
Download Presentation

ELE22MIC Lecture 23

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ELE22MIC Lecture 23 • Search Methods • Comparison of Integers & Strings • Sort Methods • Bubble Sort • Review of Assignment 1 & 2

  2. Searching an unsorted array • Searching is the activity of finding one or more items in a list that match a given search criteria. • If the data is unsorted, then you can only be sure after testing each item that no match can be found. • If there are N items, then N comparisons must be made.

  3. Searching a sorted array • If the array is sorted, then once you have found the neighbouring keys - one greater and one lesser you know there is no match. • Also, you can use a binary search method, reducing the time to find an item from N to log2N comparisons. • Unsorted: N=1000, 1000 comparisons. • Sorted: N =1000, 10 comparisons..much faster.

  4. Search Key Comparison • The item we are searching for is called the key. • The remaining data in each record is called satellite data which is associated with the key. Example record • Student Number - The primary Key • Family Name - Satellite data • Given Name - Satellite data • Date of Birth… - Satellite data

  5. Search keys • A primary key must be unique. • A secondary key may have duplicates. • Of course a key doesn’t have to be numeric, it could equally well be a string - eg Ascending Alphabetic order: surname. AARDVARK, ABBOT, ARSENIC, ... • The important part is to be able to arrange all keys (and associated satellite data) in an ascending or descending order.

  6. Search Key Comparison • In sorting & searching we would like to know if the value we are looking for is: • less-than, • equal-to or • greater-than the current item. • If the item is equal, a match has been found • If the data is ordered on the item you are searching on, a time-saving can be made.

  7. Sorting Key Permutation • When an algorithm is sorting - the algorithm permutes the keys until they are sorted. • In practice the data must accompany the key and is permuted as well. • If the record contains large amounts of data then instead of moving the data, we can move the pointers to the data instead. • Permuting only the pointers saves time - by decreasing the time spent in data movement.

  8. Sorting Methods • There is a huge number of different sorting methods: • Bubble Sort • Shaker Sort • Bin Sort • Insertion Sort • Selection Sort • Quick Sort • Heap Sort

  9. Bubble Sort • Bubble sort involves iterating through a data set, checking keys with the next key, and swapping those keys if they are not in order. • IMPLEMENTATION: • We set a boolean variable sorted to TRUE at the start of each pass through the list. • We traverse the data from top to bottom swapping pairs of data that are out of order

  10. Bubble Sort • When we swap data we also set sorted to FALSE to indicate the list was not sorted. If we swap a key we must also swap the satellite data. • When we can traverse from top to bottom of list without swapping keys, then the list is sorted, and the flag sorted is true. • Improvements: After each iteration the unsorted list length decreases by at least 1. i.e. at least the last item is in order so we don’t need to check it in future.

  11. 68HC11 Bubble Sort (1) ; Bubble sort code by Paul Main, www.mainvisions.com jmp startHere code equ $2000 ; place this code in RAM eeprom equ $B600 ; place library code in eeprom data equ $3000 ; place data in RAM also OutChar equ $ffaf InChar equ $ffcd TRUE equ 1 FALSE equ 0 org code startHere: ldx #promptString jsr outStringX

  12. 68HC11 Bubble Sort (2) ldx #myString jsr bubbleSort ldx #crlf jsr outStringX swi ; exit() back to operating system bubbleSort: ; bubble sorts the ASCIIZ string pointed to by X psha pshb pshx ; save list start on stack ldaa 0, x ; if first character = 0, then return. cmpa #0 ; are we at 0 terminator? bne bubbleList ; else sort the data. rts ; list is too short to sort

  13. 68HC11 Bubble Sort (3) bubbleList: ; listStart = Start of null terminated list ldaa #TRUE staa sorted ; sorted = TRUE; pulx ; get list start from stack, pshx ; then save it again. x = pointer to list_start; jsr outStringX ; show the changes as they happen, pass by pass pshx jsr InChar pulx

  14. 68HC11 Bubble Sort (4) bubbleItem: ldaa 1, x cmpa #0 ; are we at Null terminator? beq next_pass ; then break - we don’t want to bubble NULL cmpa 0, x ; comparing adjacent items here. bhs dont_swap ; if (item[x] > item [x+1]) bsr byteSwapX ; { swap items. Sorted = FALSE } dont_swap: inx ; x ++; bra bubbleItem next_pass: ldaa sorted ; if (sorted <> TRUE) => not sorted cmpa #TRUE bne bubbleList ; not sorted, so keep bubbling. pulx pulb pula rts ; else the list is sorted.

  15. 68HC11 Bubble Sort (5) ; ------------------------------------------------------------------- ; Swap bytes pointed to at by Index register X and X+1. ; ------------------------------------------------------------------- byteSwapX: ldab 0, x ; b = mem[x+0] ldaa 1, x ; a = mem[x+1] staa 0, x ; mem[x+0] = a stab 1, x ; mem[x+1] = b clr sorted ; sorted = FALSE; rts

  16. 68HC11 Bubble Sort (6) ; ------------------------------------------------------------------- ; Console Output - an ASCIIZ string pointed to by Index register X. ; ------------------------------------------------------------------- outStringX: pshx again: ldaa 0, x ; get the character pointed to by X cmpa #0 ; Compare with null - character 0 beq foundNull ; found null character so break out of loop jsr OutChar ; output character in Acc. A inx bra again foundNull: pulx rts

  17. 68HC11 Bubble Sort (7) ; ------------------------------------------------------------------- ; Console Input ; Reads an ASCIIZ string pointed to by Index register X. ; Maximum length to read is specified by Accumulator B. ; Pressing carriage return also terminates input. ; ------------------------------------------------------------------- inStringX: pshx ; X = address of buffer to save string into pshb ; B = max # chars to read psha getAgain: jsr InChar ; output character in Acc. A cmpa #13 beq foundEnter ; null character - break out of loop staa 0, x ; save read char in buffer inx ; point to next char position decb ; decrement the limit counter bne getAgain ; max count not = 0, loop again foundEnter: clr 0, x ; ensure string is terminated pula pulb pulx rts

  18. 68HC11 Bubble Sort (8) org data myString fcc 'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!' fcb 0 promptString fcc 'Enter a number to seek to:' fcb 0 readString fcc 'CONSOLE INPUT BUFFER' fcb 0 crlf fcb $0d, 0 ; CR beep fcb 7, 0 ; ASCII BELL sorted rmb 1 ; boolean flag to indicate when list is sorted

  19. Executing Bubble Sort (1) g 2000 THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG! HE QTICK BROUN FOW JUMPS OVER THE LAXY DOG!Z E HQICK BROTN FOU JUMPS OVER THE LAWX DOG!YZ EHICK BQORN FOT JUMPS OUER THE LAVW DOG!XYZ EHCI BKOQN FOR JTMPS OUER THE LAUV DOG!WXYZ ECH BIKON FOQ JRMPS OTER THE LAUU DOG!VWXYZ CE BHIKN FOO JQMPR OSER THE LATU DOG!UVWXYZ C BEHIK FNO JOMPQ ORER SHE LATT DOG!UUVWXYZ BCEHI FKN JOMOP OQER RHE LAST DOG!TUUVWXYZ BCEH FIK JNMOO OPEQ RHE LARS DOG!TTUUVWXYZ BCE FHI JKMNO OOEP QHE LARR DOG!STTUUVWXYZ BC EFH IJKMN OOEO PHE LAQR DOG!RSTTUUVWXYZ B CEF HIJKM NOEO OHE LAPQ DOG!RRSTTUUVWXYZ BCE FHIJK MNEO OHE LAOP DOG!QRRSTTUUVWXYZ BC EFHIJ KMEN OHE LAOO DOG!PQRRSTTUUVWXYZ

  20. Executing Bubble Sort (2) B CEFHI JKEM NHE LAOO DOG!OPQRRSTTUUVWXYZ BCEFH IJEK MHE LANO DOG!OOPQRRSTTUUVWXYZ BCEF HIEJ KHE LAMN DOG!OOOPQRRSTTUUVWXYZ BCE FHEI JHE KALM DNG!OOOOPQRRSTTUUVWXYZ BC EFEH IHE JAKL DMG!NOOOOPQRRSTTUUVWXYZ B CEEF HHE IAJK DLG!MNOOOOPQRRSTTUUVWXYZ BCEE FHE HAIJ DKG!LMNOOOOPQRRSTTUUVWXYZ BCE EFE HAHI DJG!KLMNOOOOPQRRSTTUUVWXYZ BC EEE FAHH DIG!JKLMNOOOOPQRRSTTUUVWXYZ B CEE EAFH DHG!IJKLMNOOOOPQRRSTTUUVWXYZ BCE EAEF DHG!HIJKLMNOOOOPQRRSTTUUVWXYZ BC EAEE DFG!HHIJKLMNOOOOPQRRSTTUUVWXYZ B CAEE DEF!GHHIJKLMNOOOOPQRRSTTUUVWXYZ BACE DEE!FGHHIJKLMNOOOOPQRRSTTUUVWXYZ ABC DEE!EFGHHIJKLMNOOOOPQRRSTTUUVWXYZ AB CDE!EEFGHHIJKLMNOOOOPQRRSTTUUVWXYZ

  21. Executing Bubble Sort (3) A BCD!EEEFGHHIJKLMNOOOOPQRRSTTUUVWXYZ ABC!DEEEFGHHIJKLMNOOOOPQRRSTTUUVWXYZ AB!CDEEEFGHHIJKLMNOOOOPQRRSTTUUVWXYZ A!BCDEEEFGHHIJKLMNOOOOPQRRSTTUUVWXYZ !ABCDEEEFGHHIJKLMNOOOOPQRRSTTUUVWXYZ P-200C Y-FFFF X-3082 A-00 B-1E C-D4 S-0041 > !ABCDEEEFGHHIJKLMNOOOOPQRRSTTUUVWXYZ P-200C Y-FFFF X-3082 A-00 B-1E C-D4 S-0041

  22. Bubble Sort

  23. Improving Bubble Sort • Bubble sort Pseudo-Code: • UnsortedLength = sizeof (item); • do { sorted = TRUE; • for (x = list_start, x < list_start+UnsortedLength-2, x++) • { if (item[x] > item [x+1]) • { // swap keys • temp = item [x]; • item[x] = item [x+1]; • item [x+1] = temp; • sorted = FALSE; // not sorted yet • }; UnsortedLength --; • } • } while (not sorted or UnsortedLength > 0)

  24. Shaker Sort • Similar to Bubble sort, but list is traversed from top to bottom, then bottom to top. • In the event that only one or two items are out of order then shaker sort performs very well, completing within two or three passes of the list. • Iterations proceed between boundaries flagging sorted/unsorted areas. The boundaries close in on the unsorted data as the sort continues.

  25. Shaker Sort

  26. Order • Sorting algorithm speed performance is dependent upon the number of comparisons and moves. • Unsorted: N=1000, 1000 comparisons. • Order N • Sorted: N =1000, 10 comparisons..much faster. • Order Log (N)

  27. Acknowledgments • Motorola M68HC11 Reference Manual • HC11 images derived from Motorola 11rm.pdf - HC11 Reference Manual • Floppy Disk images from IEEE Electronics Engineers Handbook, 4th Edition, Donald Christiansen, ISBN 0-07-021862-5.

More Related