排序2大阵列

编程入门 行业动态 更新时间:2024-10-17 07:28:35
本文介绍了排序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大阵列

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

发布评论

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

>www.elefans.com

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