Hi I am working on the code stable selection sort,and I have been able to get the correct result, but I am not sure if there are corner cases in the code.The data I am sorting like this
a[0]=new Data(1,’d’);
a[1]=new Data(2,’c’);
a[2]=new Data(3,’a’);
a[3]=new Data(4,’b’);
a[4]=new Data(5,’d’);
a[5]=new Data(6,’c’);
a[6]=new Data(8,’a’);
a[7]=new Data(9,’a’);
a[8]=new Data(10,’a’);
as you can see it is sorted by numbers and I am supposed to sort it by characters now.
So the logic of sort of Data objects I have used is like this:
in the loop of finding the smallest element, we will not just find the smallest element but smallest element with smallest int. That way order of the elements will remain the same
even though it is working just fine, are there any corner cases I have missed here?
for ex : lets take up itunes, first we sort by the ids of the songs and after that we want to sort by their names. I hope it makes every thing clear
No, you have not missed anything. This is the standard technique to make any unstable algorithms stable: impose a total ordering! Any ties are resolved by the second key – which is the input order. I assume you correctly implemented lexicographical ordering here, it’s not entirely clear from your description.