Question
I need help creating pseudocode and a structure chart for a sub-list sorting algorithm. No code , this is merely a draft of design. The
I need help creatingpseudocode and a structure chartfor a sub-list sorting algorithm.No code, this is merely a draft of design. The program will prompt the user for the list of names or numbers, sort them using the Sub-List sort design, and then display the sorted list on the screen. Other requirements:
- This algorithm does not create sub-arrays. There are only two arrays: the source and destination.
- This algorithm does not change the size of the arrays. The source array and the destination array are always exactly the same size. There is no append() or push() or insert().
- This algorithm takes advantage of already sorted sequences of numbers. One sub-list may have a single element and another may have ten.
Thanks in advance!
Algorithm in Plain English A teacher is trying to alphabetize a collection of papers. She picks up the papers in her hand and, starting at the top of her stack, works her way down until she finds the first paper out of order. That sub-stack is sorted, and is set aside. She does the same thing to find the next sorted sub-stack. These two sub-stacks are then combined into a single sorted stack that she places on the table. She continues through the original stack in her hand, combining pairs of sorted sub-stacks and putting the results on top of the stack on the table. When she is finished, only the stack on the table remains. Now she picks up the stack on the table and again searches for sorted sub-stacks. When she finds a pair of these, they are combined and placed on the table again. This process continues until again all the papers are on the table. When she picks the stack up off of the table and everything is sorted (there is only one sorted sub-stack), then she is done! Detailed Description of the Algorithm Start at the beginning of the array and continue until you discover the first element that is not in sorted order. This sub- array may consist of one element, or it may consist of hundreds. We will call this sub-array source1. Now, starting in the slot after source1, find the next sub-array. Again, this may consist of one element or hundreds. We will call this sub- array source2. Note that source1 and source2 are sorted individually. These two sub-arrays correspond to the small piles in the previous paragraph. Now we will combine these two sub-arrays to form a single sorted sub-array. We will not do this in place. Instead, we will move them into a new sub-array called destination. Now since the two source arrays are sorted, we can assume that the smallest element in the two arrays is at the beginning of one of the two source arrays. In other words, it is at source1[0] or source2[0]. Therefore destination[@] must be filled with either source1[0] or source2[0]. If, for example, source2 has the lower of the two elements, then that element is moved to destination and the index of source2 (which we will call iSource2) is incremented by one. This process is continued until each element from source1 and source2 is moved into destination. Once the first two sorted sub-arrays are moved to the destination array, then the next two sub-arrays are identified and combined into the destination array. This process continues until each element in the source array is moved into the destination array. Here, we can say that we have completed one pass of the source array. Note that the sort is not finished yet. We have not sorted the array when we have completed just one pass. The only thing that a single pass accomplishes is to double the size of the sorted sub-arrays. We have to do many passes until the entire destination array becomes a single sorted sub-array. At that point, we can say that the array is sorted. Example Consider the following array to be sorted: 31 72 32 10 95 50 25 18 The first sorted sub-array, which we will call source1, consists of 31 and 72. The next sorted sub-array is only one element: 32. 31 72 32 10 95 50 25 18 When we combine sourcel to source, we get the sorted sub-array 31, 32, 72: 31 72 32 10 95 50 25 18 31 32 72 ? ? ? ? ? The next run of sorted numbers is just the numbers (10,95). The run after it has the single number (50). When they are combined, the resulting list is {10, 50,95). 31 72 32 10 95 50 25 18 31 32 72 10 50 95? ? The final run of this iteration will consist of the single item (25) and the single item (18). When they are combined, their slots will be reversed. 31 72 32 10 95 50 25 18 31 32 72 10 50 95 18 25 W Now that we have made one pass through the array, we make another pass. This time we swap the source and destination array. In other words, instead of copying the elements down, we copy them up. We will expect the runs to be bigger this time around. The first run is {31, 32, 70} and the second is {10, 50,95). They merge to (10, 31, 32, 50, 72,95). 10 31 32 50 72 95 25 18 1 31 32 72 10 50 95 18 25 Our next run is {18, 25). However, since there is not another run to combine with it, we need to stop here. This means we just copy that run to the destination array. 10 31 32 50 72 95 25 18 1 10 31 32 50 72 95 25 18 For this final iteration, we are copying elements down again. The first run consists of {10, 31, 32, 50, 72,95) and the second consists of (18,25). When these two runs are combined, we get the sorted list. 10 31 32 50 72 95 18 25 10 18 25 31 32 50 72 95
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started