15,962,733 members
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
OriginalGriff 2-May-13 6:41am
And?
What have you tried?
Where are you stuck?

## Solution 1

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.

Kenneth Haugland 2-May-13 6:49am
Why are you reposting this? 3 times?!?

## Solution 4

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.

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)

Top Experts
Last 24hrsThis month
 OriginalGriff 40 RickZeeland 15 Dave Kreskowiak 10 Pete O'Hanlon 10
 OriginalGriff 218 Pete O'Hanlon 150 merano99 80 den2k88 50 Andre Oosthuizen 50

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