Sort Techniques Why Sort Place your returned checks

  • Slides: 16
Download presentation
Sort Techniques

Sort Techniques

Why Sort? • • Place your returned checks into check number sequence Arrange your

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

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 1175943

Selection Sort 6656768 8867940 3305983 5540729 7798734 1175943 4497530 2239757 0086420 9923679 0086420 1175943 2239757 3305983 • 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

Bubble Sort 6656768 8867940 3305983 5540729 7798734 1175943 4497530 2239757 0086420 9923679 6656768 8867940

Bubble Sort 6656768 8867940 3305983 5540729 7798734 1175943 4497530 2239757 0086420 9923679 6656768 8867940 3305983 5540729 8867940 7798734 8867940 1175943 4497530 8867940 2239757 8867940 0086420 8867940 9923679 • Compare first 2 elements (6656768 & 8867940). If 2 nd element is less, swap elements (into the yellow box in our example). Click • Compare 2 nd & 3 rd elements (8867940 & 3305983). If 3 rd element is less, swap elements. Click • Compare 3 rd & 4 th elements (8867940 & 5540729). If 4 th element is less, swap elements and so forth all the way to the bottom. Click for each comparison • End of pass 1.

Bubble Sort – Pass 2 In Pass 2, just go through the same process

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 7798734 4497530 2239757 0086420 7798743 8867940 9923679

Bubble Sort – Pass 3 In pass 3, just do the same thing again.

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 4497530 2239757 0086420 6656768 7798734 8867940 9923679

Bubble Sort – Pass 4 By now, perhaps, you noticed that the value beginning

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 4497530 5540719 2239757 5540729 0086420 5540729 6656420 7798734 8867940 9923679

Bubble Sort – Pass 5 It took to the 5 th pass to begin

Bubble Sort – Pass 5 It took to the 5 th 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

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

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 1175943 0086420 2239757 3305983 4497530 5540729 6656420 7798734 8867940 9923679

Bubble Sort – Pass 8 Finally, the sort is complete after 8 passes. Determining

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 9 th 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 1175943 2239757 3305983 4497530 5540729 6656420 7798734 8867940 9923679

Merge Sort 1976 • Continue to divide elements in half until you can no

Merge Sort 1976 • 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 19 SORT 76 1 9 7 6 1 9 6 7 19 67 MERGE 1679 Example from: www. cprogramming. com

One way to work around the problem of large records or fields, which works

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“. Tag Sort 1 334. 30 2 776. 40 3 112. 58 4 1252. 31 5 3998. 45 6 445. 91

Tag Sort • Sort the white table using selection or bubble method • The

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 112. 58 0 334. 30 5 445. 91 1 776. 40 3 1252. 31 4 3998. 45

End of SORT topic

End of SORT topic