排序2个大型数组

我还没有采用数据结构和算法类,我在尝试做的事情上遇到了一些麻烦。

我有2个大数组,1个是大约80k-100k字的char ,第二个是具有相同整数量的int数组(例如,用words[502]写的words[502]其出现的数量用integers[502]写成integers[502] )。

我必须对它们进行排序,以便它的相应单词的最大整数是第一个,第二个第二个等等,是否可以不使用冒泡排序(这对于这些数字来说太慢)?

 void bubble_sort(int n) { int i,j,temp; char tempwordy[40]={0}; for(i=1;i< n;i++) { for(j=0;jcounters[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); } } } } 

使用结构

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

使用stdlib.hqsort函数对其进行排序。

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

您可以构建一个结构数组。 第一个条目是计数器,第二个条目是原始位置,并对此进行排序(通过q​​sort())。 因此像

 typedef struct _entry { int cnt; int pos; } entry; entry *entries; ... /* fill the entries table */ ... qsort(...); /* or some other sort */ 

由于你只是移动两个整数,它将比排序字符数组快得多。 之后,通过评估位置,您知道哪个计数属于哪个单词

您调整了bubblesort以在两个arrays上同时工作。 我认为您可以调整许多其他排序算法(例如快速排序 )以同时处理两个arrays。 quicksort的一个中心子程序是swap ,我认为你主要需要调整swap和比较标准,以便它可以在两个数组上运行。

另一种可能性是创建第三个数组,将一个单词与其出现“耦合”。 您可以通过使用以下某种结构来完成此操作:

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

然后,您可以只对word_and_occurence数组进行排序,然后迭代此数组的排序版本并正确地重新调整字符串。