栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java数组排序:获取数组索引排序列表的快速方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java数组排序:获取数组索引排序列表的快速方法

我将定制快速排序算法以同时在多个数组上执行交换操作:索引数组和值数组。例如(基于此quicksort):

public static void quicksort(float[] main, int[] index) {    quicksort(main, index, 0, index.length - 1);}// quicksort a[left] to a[right]public static void quicksort(float[] a, int[] index, int left, int right) {    if (right <= left) return;    int i = partition(a, index, left, right);    quicksort(a, index, left, i-1);    quicksort(a, index, i+1, right);}// partition a[left] to a[right], assumes left < rightprivate static int partition(float[] a, int[] index, int left, int right) {    int i = left - 1;    int j = right;    while (true) {        while (less(a[++i], a[right]))      // find item on left to swap ;         // a[right] acts as sentinel        while (less(a[right], a[--j]))      // find item on right to swap if (j == left) break;// don't go out-of-bounds        if (i >= j) break;       // check if pointers cross        exch(a, index, i, j);    // swap two elements into place    }    exch(a, index, i, right);    // swap with partition element    return i;}// is x < y ?private static boolean less(float x, float y) {    return (x < y);}// exchange a[i] and a[j]private static void exch(float[] a, int[] index, int i, int j) {    float swap = a[i];    a[i] = a[j];    a[j] = swap;    int b = index[i];    index[i] = index[j];    index[j] = b;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430968.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号