使用索引数组快速排序 [英] Quick sort with index array
本文介绍了使用索引数组快速排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个 quicksort 算法实现,它在排序前给出元素的原始索引。
ie,
排序前
指数:0 1 2 3 4 5
值:70 60 40 20 30 50
分拣后
指数:3 4 2 5 1 0
值:20 30 40 50 60 70
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
推荐答案
嗨
我建议使用带有值的结构及其初始位置及其操作数组。
然后使用 algoritm 交换数组中的结构。
你不需要新的quicksort实现,你可以使用qsort
原样:
- 将每个数组项存储在包含其值及其原始索引的结构中(最后使用这样的结构数组)
- 定义比较函数以将两个这样的结构作为参数
- 使用标准
qsort
来排序结构数组。
You don''t need a fresh quicksort implementation, you can useqsort
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.
这篇关于使用索引数组快速排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文