I am supposed to write a program that let the user enter the amount of students => enter the height of the students => sort the height accordingly, female first (with even indexes: 2 4 6 8 etc) and then male (indexes 1 3 5 7 etc)
Problem is if some students' heights are the same, sometimes my program put the latter index first
(ex: 3 158cm people with index 1+3+9, the program sorts it as 9-3-1) and I have to do the opposite.
I'm not sure what should I do to sort those, here's my code so far:
the thing you seek is called a stable sort, which leaves items with the same value in their original relative order. It requires more logic and is slower than a normal sort that can put them in any order. You can look into this online, its a bit to go into here.
OR, if you just want to keep the boys & girls in even/odd setup, you can split the data into 2 arrays, sort both, and merge back together afterwards, such that the boys are sorted, and the girls are sorted, but the boys and girls together are not assured to be sorted. not sure if this is what you are asking.