排序2大阵列 [英] Sorting 2 large arrays

查看:175
本文介绍了排序2大阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有考虑数据结构和算法类,我有一些麻烦与我尝试做。

我有2个大型阵列,1 字符有关80K-100K字和第二个是 INT 阵列整数相同的量(例如,字写在字[502] 有写在整数[502] )。

我必须对它们进行排序,以便与它的最大整数是对应的字是第一,第二大第二等等等等,是有可能不使用冒泡排序(这是太慢了这些号码)?

 无效bubble_sort(INT N)
{
    INT I,J,温度;
    焦炭tempwordy [40] = {0};


    为(ⅰ= 1; I&n种;我+ +)
    {
        为(J = 0; J&n种-1; J ++)
        {
            如果(计数器[J] GT;柜台[J + 1])
            {
                TEMP =柜台[J]。
                柜台[J] =柜台[J + 1];
                柜台[J + 1] =温度;

            的strcpy(tempwordy,字[J]);
            的strcpy(字[J],字[J + 1]);
            的strcpy(字[J + 1],tempwordy);
            }
         }
    }
}
 

解决方案

使用结构

 结构字
{
  字符字[100];
  诠释计数;
};

结构字阵列[502];
 

使用的qsort 函数从文件stdlib.h 排序。

  INT比较(常量无效*一,常量无效* B)
{

常量结构数组* IA =(const的结构数组*)一个;
常量结构数组* IB =(const的结构数组*)B:
如果(* ia.count> * ib.count)
    返回1;
否则,如果(* ia.count == * ib.count)
    返回0;
其他
    返回-1;
}
的qsort(阵列,502,的sizeof(数组[0]),比较);
 

I have not yet taken data structures and algorithm class and I am having some troubles with what I try to do.

I have 2 large arrays, 1 is char with about 80k-100k words and second is int array with same amount of integers (for example, word written in words[502] has its number of occurences written in integers[502]).

I have to sort them so the largest integer with it's corresponding word is 1st, second largest 2nd etc etc, is it possible without using bubble sort (which is too slow for these numbers)?

void bubble_sort(int n)
{
    int i,j,temp;
    char tempwordy[40]={0};


    for(i=1;i< n;i++)
    {
        for(j=0;j< n-1;j++)
        {
            if(counters[j]>counters[j+1])
            {  
                temp=counters[j];
                counters[j]=counters[j+1];
                counters[j+1]=temp;

            strcpy(tempwordy,words[j]);
            strcpy(words[j],words[j+1]);
            strcpy(words[j+1],tempwordy);
            }
         }
    }
}

解决方案

Use a structure

struct word
{
  char word[100];
  int count;
};

struct word array[502];

sort it using the qsort function from stdlib.h.

int compare(const void* a, const void* b)
{

const struct array *ia = (const struct array *)a;
const struct array *ib = (const struct array *)b;
if(*ia.count>*ib.count)
    return 1;
else if(*ia.count==*ib.count)
    return 0;
else
    return -1;
}
qsort(array,502,sizeof(array[0]),compare);

这篇关于排序2大阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆