reset password
Author Message
stcastro13
Posts: 5
Posted 01:10 Nov 16, 2015 |

This lab is asking for two ways to sort things, sortStrategyA and sortStrategyB.

SortStrategyA

 

starting from the end of the list:
 
for each element in the list going backwards
	find the maximum element in the sublist beginning with the first element and ending with the current element
	exchange the maximum element with the current element

NOTE: When finding the maximum, you should only search from your current position, to the beginning of the list. Write and use a separate method that finds the greatest element in a stated section of a list of Circles. The method should take a list and two indices as arguments. It should find the largest element in the section of the list that starts with the first index and ends with the second index.

SortStrategyB

starting from the end of the list:

repeat until there are no swaps in one iteration 
	for each item in the list going backwards
		if the item at (index - 1) is greater than the item at the current index, swap them
end repeat
 

The sort methods must keep track of how many comparisons and swaps have been made while sorting. Think carefully about where to put the code that tracks these values. In SortStrategyB, be careful not to get the number of swaps in one iteration of the outer loop, which will always be zero in the last pass, confused with the total number of swaps. Note that, in either algorithm, the number of swaps cannot be larger than the number of comparisons. Once the list is sorted, the swap method should create a SortResult using the sorted list and the counts of comparisons and swaps. Note that SortResult has a constructor that conveniently takes these values as arguments.

this is very confusing as they are both sorts asking for swaps but i am not sure as to what the difference is in both of these. I understand sortStrategyB but I am not sure what sortStrategyA is asking of me. 

jhurley
Posts: 207
Posted 08:51 Nov 16, 2015 |

When you are coding by following pseudocode, sometimes trying to understand the algorithm just gets in the way, because you code what you think should come next, rather than what the pseudocode actually says. Try coding it first without trying to understand it.  Then work it out with a pencil and paper for an array with five values to see what is going on.