Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sort Techniques.

Similar presentations


Presentation on theme: "Sort Techniques."— Presentation transcript:

1 Sort Techniques

2 Place your returned checks into check number sequence
Why Sort? Place your returned checks into check number sequence Arrange your personal phone book into sequence by last name Put your grade book into sequence by grade (A’s, B’s, etc.) And many other reasons that you can probably think of You have possibly already written a SORT routine or two in the past and in some other programming language class. The concepts are exactly the same using Assembler language. However, writing the code may a bit more of a challenge. But you are up to that challenge if you remember the techniques. In this topic will we take a refresher look at those techniques (some of them anyway) and use one of those techniques to apply our skills.

3 Sorting Techniques Selection Sort Bubble Sort Binary Sort Merge Sort Tag Sort and many more that are probably not as popular and most often used for particular circumstances.

4 Selection Sort Search a single-level array looking for the smallest element value. Place that value into the first element of the new array and “lock” the found entry

5 Bubble Sort Compare first 2 elements ( & ). If 2nd element is less, swap elements (into the yellow box in our example). Click Compare 2nd & 3rd elements ( & ). If 3rd element is less, swap elements. Click Compare 3rd & 4th elements ( & ). If 4th element is less, swap elements and so forth all the way to the bottom. Click for each comparison End of pass 1.

6 Bubble Sort – Pass 2 In Pass 2, just go through the same process again, beginning at the top of the table and continuing to the bottom of the table. We began with the gray table on the left, which was the output of the first pass – ended up with the yellow table on the right. If you compare the first, second, and third tables, you can see that the smaller values are working their way to the tap while the larger values are working their way to the bottom.

7 Bubble Sort – Pass 3 In pass 3, just do the same thing again. This process allows us to establish a loop in our program to sort the table. This time, let’s go a bit slower each comparison, so you will need to click your mouse a few times slowly to watch the amazing animation… . Again, note that the smaller values are bubbling up the table while the larger values are bubbling down.

8 Bubble Sort – Pass 4 By now, perhaps, you noticed that the value beginning with ‘11’ is beginning to move up the table, as is the value beginning ’22’. Again the first couple of comparisons are done slowly so you can see the effect of each comparison. The rest of the table is done together with the magic of animation, of course, otherwise you would be wearing out your mouse button.

9 Bubble Sort – Pass 5 It took to the 5th pass to begin moving the lowest value up the table (‘ ’). When it reaches the top, we should be all done, especially since we are really bubbling the higher values to the bottom. Notice now that most of the values are already in ascending sequence.

10 Bubble Sort – Pass 6

11 Bubble Sort – Pass 7 At the end of Pass 7, all the values are in ascending sequence with the exception of the first two values, so we know there is only one pass remaining.

12 Bubble Sort – Pass 8 Finally, the sort is complete after 8 passes. Determining the number of passes so you can control your loop with a BCT instruction can be difficult. The table in the example is small so it is fairly easy to determine the number of passes. Go back to the original table. The lowest value (‘ ’) is the 9th entry in the table so it needs to move 8 times (one step at a time) to rise to the top. That’s the number of passes. There are many examples on the Internet. Search: computer bubble sort

13 Merge Sort 1976 19 76 Continue to divide elements in half until you can no longer divide in half Merge the elements back together – in order – starting at the left of each list 1 9 7 6 SORT 1 9 6 7 19 67 MERGE 1679 Example from:

14 Tag Sort One way to work around the problem of large records or fields, which works well when complex records (such as in a relational database) are being sorted by a relatively small key field, is to create an index into the array and then sort the index, rather than the entire array. (A sorted version of the entire array can then be produced with one pass, reading from the index, but often even that is unnecessary, as having the sorted index is adequate.) Because the index is much smaller than the entire array, it may fit easily in memory where the entire array would not, effectively eliminating the disk-swapping problem. This procedure is sometimes called "tag sort“. 1 2 3 4 5 6 334.30 776.40 112.58 445.91

15 Tag Sort Sort the white table using selection or bubble method The tag array retains the original sequence Therefore, the best use of a tag sort is when the original order of a table must be kept in tact. 2 5 1 3 4 112.58 334.30 445.91 776.40

16 End of SORT topic


Download ppt "Sort Techniques."

Similar presentations


Ads by Google