Sort Techniques
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.
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.
Selection Sort 6656768 8867940 3305983 5540729 7798734 1175943 4497530 2239757 0086420 9923679 0086420 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 1175943 2239757 3305983
Bubble Sort Compare first 2 elements (6656768 & 8867940). If 2nd element is less, swap elements (into the yellow box in our example). Click Compare 2nd & 3rd elements (8867940 & 3305983). If 3rd element is less, swap elements. Click Compare 3rd & 4th elements (8867940 & 5540729). If 4th element is less, swap elements and so forth all the way to the bottom. Click for each comparison End of pass 1. 6656768 8867940 3305983 5540729 7798734 1175943 4497530 2239757 0086420 9923679 6656768 8867940 3305983 8867940 5540729 8867940 7798734 8867940 1175943 8867940 4497530 8867940 2239757 8867940 0086420 8867940 9923679
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. 6656768 3305983 5540729 7798734 1175943 4497530 2239757 0086420 8867940 9923679 3305983 6656768 5540729 6656768 1175943 4497530 2239757 0086420 7798743 8867940 9923679 7798734
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. 3305983 5540729 6656768 1175943 4497530 2239757 0086420 7798734 8867940 9923679 3305983 5540729 1175943 6656768 6656768 4497530 2239757 0086420 6656768 7798734 8867940 9923679
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. 3305983 5540729 1175943 4497530 2239757 0086420 6656420 7798734 8867940 9923679 3305983 5540729 1175943 5540719 4497530 5540729 2239757 0086420 5540729 6656420 7798734 8867940 9923679
Bubble Sort – Pass 5 It took to the 5th pass to begin moving the lowest value up the table (‘0086420’). 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. 3305983 1175943 4497530 2239757 0086420 5540729 6656420 7798734 8867940 9923679 1175943 3305983 2239757 0086420 4497530 5540729 6656420 7798734 8867940 9923679
Bubble Sort – Pass 6 1175943 3305983 2239757 0086420 4497530 5540729 6656420 7798734 8867940 9923679 1175943 2239757 0086420 3305983 4497530 5540729 6656420 7798734 8867940 9923679
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. 1175943 2239757 0086420 3305983 4497530 5540729 6656420 7798734 8867940 9923679 4497530 5540729 6656420 7798734 8867940 9923679 1175943 0086420 2239757 3305983
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 (‘0086420’) 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 1175943 0086420 2239757 3305983 4497530 5540729 6656420 7798734 8867940 9923679 0086420 3305983 4497530 5540729 6656420 7798734 8867940 9923679 1175943 2239757
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: www.cprogramming.com
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 1252.31 3998.45 445.91
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 1252.31 3998.45
End of SORT topic