Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I need a quicksort algorithm implementation which gives the original indices of the elements, before sorting, also.


ie,

Before sorting

index : 0 1 2 3 4 5
values: 70 60 40 20 30 50



After sorting

index : 3 4 2 5 1 0
values: 20 30 40 50 60 70
Posted
Updated 2-May-13 0:43am
v2
Comments
OriginalGriff 2-May-13 6:41am    
And?
What have you tried?
Where are you stuck?

Hi
I would suggest to use a structure with value and its initial location and its array for the operation.
Then using algoritm swap the structure in the array.
 
Share this answer
 
Comments
Kenneth Haugland 2-May-13 6:49am    
Why are you reposting this? 3 times?!?
You don't need a fresh quicksort implementation, you can use qsort as it stands:
  • store every array item in a struct containing its value and its original index (ending up with an array of such structs)
  • define your comparison function for taking two of such structs as parameters
  • use standard qsort for ordering the array of structs.
 
Share this answer
 
Comments
nv3 2-May-13 8:39am    
Definitely the way to do it. Except this is homework and the teacher wants to see the implementation of the quick sort :-) My 5.
CPallini 2-May-13 8:44am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900