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

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

Embed Size (px)

Citation preview

Page 1: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

ELE22MIC Lecture 23• Search Methods

– Comparison of Integers & Strings

• Sort Methods– Bubble Sort

• Review of Assignment 1 & 2

Page 2: 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.

• 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.

Page 3: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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.

Page 4: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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

Page 5: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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.

Page 6: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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.

Page 7: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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.

Page 8: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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

Page 9: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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

Page 10: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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.

Page 11: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

68HC11 Bubble Sort (1)

; Bubble sort code by Paul Main, www.mainvisions.com

jmp startHerecode equ $2000 ; place this code in RAMeeprom equ $B600 ; place library code in eepromdata equ $3000 ; place data in RAM also

OutChar equ $ffafInChar equ $ffcdTRUE equ 1FALSE equ 0

org code

startHere: ldx #promptString jsr outStringX

Page 12: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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

Page 13: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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

Page 14: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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 bubbleItemnext_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.

Page 15: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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

Page 16: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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

Page 17: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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

Page 18: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

68HC11 Bubble Sort (8)

org data

myString fcc 'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!' fcb 0promptString fcc 'Enter a number to seek to:' fcb 0readString fcc 'CONSOLE INPUT BUFFER' fcb 0crlf fcb $0d, 0 ; CRbeep fcb 7, 0 ; ASCII BELL

sorted rmb 1 ; boolean flag to indicate when list is sorted

Page 19: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

Executing Bubble Sort (1)

g 2000THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!HE QTICK BROUN FOW JUMPS OVER THE LAXY DOG!ZE 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

Page 20: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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

Page 21: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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

Page 22: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

Bubble Sort

Page 23: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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)

Page 24: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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.

Page 25: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

Shaker Sort

Page 26: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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)

Page 27: ELE22MIC Lecture 23 Search Methods –Comparison of Integers & Strings Sort Methods –Bubble Sort Review of Assignment 1 & 2

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.