使用索引数组快速排序

编程入门 行业动态 更新时间:2024-10-26 12:25:19
本文介绍了使用索引数组快速排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一个 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 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.

更多推荐

使用索引数组快速排序

本文发布于:2023-11-28 22:38:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644221.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   索引   快速

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!