Sorting 2 large arrays

拈花ヽ惹草 提交于 2019-12-12 01:12:11

问题


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);
            }
         }
    }
}

回答1:


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:


You could build an array of structs. First entry is the counter, second entry is the original position, and sort this (by qsort()). Thus something like

typedef struct _entry {
    int cnt;
    int pos;
} entry;

entry *entries;

...
/* fill the entries table */
...
qsort(...); /* or some other sort */

Since you're only moving two integers around, it'll be substantially faster than sorting the character array. Afterwards, you know which count belongs to which word by evaluating the position




回答3:


You adapted bubblesort to work simultaneously on two arrays. I think you can adapt many other sorting algorithms (such as e.g. quicksort) to work on two arrays simultaneously. A central subroutine of quicksort is swap and I think that you mainly have to adjust swap and the comparison criterion such that it works on two arrays.

Another possibility would be to create a third array that "couples" a word with its occurrence. You could do this by using some kind of the following struct:

struct word_and_occurence {
   char** ptr_to_word;
   int occurences;
}

You could then sort just the array of word_and_occurences and afterwards iterate over the sorted version of this array and readjust the strings properly.



来源:https://stackoverflow.com/questions/21258065/sorting-2-large-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!