1

I'm working on an activity in which I have to determine what sort is used in a set of given methods, which I can't actually view as it's run from a .jar file which only contains .class files. All I can see is timing information (so I can determine what sort it could be based on time complexity). However, the elements being sorted are randomly generated integers, and duplicates are allowed. In order to properly identify the sorts, I'll have to somehow identify which methods are not sorting in a stable manner--without being able to view the source code or the array of integers. For example, I could have an array [1,2,3,9,5,64,8,5,1], which when sorted would be [1,1,2,3,5,8,9,64]. However, if something like a non-stable Selection Sort were used to sort this, the two 1 values would be switched in relative order to each other. How could I detect if something like this has occurred?

  • 2
    "How could I detect if something like this has occurred?" If the array you are sorting is always `int[]` and you cannot feed any other type of array in this method(s), I am affraid you cannot. – Turing85 Sep 26 '16 at 21:37
  • decompil the .class – Tokazio Sep 26 '16 at 21:40
  • And how do you propose to tell the difference between, say, insertion sort and bubble sort? Or between Quicksort and Heap sort? – Jim Mischel Sep 26 '16 at 22:04
  • 1
    The sorting of an `int[]` does not guarantee the use of a stable sort, because there is no need, given that equal values cannot be distinguished. – Andreas Sep 26 '16 at 22:28

2 Answers2

2

If you're constrained to using ints, there's no way you can test this as there is no way to distinguish two different 1s.

However, if you have the option of using Integers, and you specifically use new Integer() (rather than the autoboxing that comes with Java), you can compare the references in the original array with the new one.

Joe C
  • 15,324
  • 8
  • 38
  • 50
  • How would I go about doing that? Would I have to save both arrays as Integer arrays? – Graham Mcclelland Sep 26 '16 at 22:25
  • @GrahamMcclelland - I think the idea here is that Java would end up effectively with an array of pointers to Integers (references to Integer objects). This is somewhat violating the constraints by using an array of pointers. Also it could only work if the pointers start off in memory address order, and that a sort function would sort the pointers as opposed to sorting values (no rearrangement of pointers), and that there is some way to check the values of the pointers after a sort. – rcgldr Sep 27 '16 at 02:54
1

As mentioned by Joe C, you would have to use some sort of wrapper class as there is no way to determine the difference between two primitive values. For example the Integer class could be used.

However there is a bigger issue here and it is an issue with logic. Just because a sorting algorithm is not guaranteed to be stable does not mean that given a particular input it will be unstable. Thus, measuring instability of an algorithm is ultimately an unreliable way of determining which algorithm is being used.

You could use instability as a way to narrow down your set of potential algorithms however to begin with I would recommend that you focus on the time it takes for different algorithms to process different kinds of inputs. Start with the obvious which is the time complexity of the algorithms: for example a bogosort will on average take much longer than a quicksort. Then delve into the inner workings of the algorithms that you are considering. Some algorithms with the same time complexities (eg: mergesort and heapsort) will both behave differently based on the type of input that you pass into them. For example one may be very slow to process a completely reversed input while another may do this very quickly.

MattC
  • 847
  • 6
  • 15