Tag: 排序

如何在C中按升序对字符串数组进行排序

问题 我已经制作了与其他类似的排序程序 C Program to Sort set of strings in alphabetical order 但我做的程序不起作用。 我认为两者都是一样的,但我的计划给了我废物输出。 另外我想知道其他程序计数设置为5例如它应该从0开始输入6但是它只获得5,如何? 我的计划 #include #include int main() { char str[4][10],temp[10]; int i,j; printf(“Enter strings one by one : \n”); for(i=0;i<5;i++) scanf("%s",str[i]); for(i=0;i<5;i++) for(j=i+1;j0){ strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } printf(“\nSorted List : “); for(i=0;i<5;i++) printf("\n%s",str[i]); printf("\n\n"); return 0; }

在链表上实现mergesort

我的任务是在用C / C ++编写的列表上实现合并排序算法。 我有一般的想法,编写我的代码并成功编译它。 但是,当我运行它时,它会开始正常,但然后挂起“准备好的列表,现在开始排序”而不会出现任何错误。 我试图查看我的代码,但我完全不知道问题是什么。 我也非常业余的调试,所以使用gdb尽我最大的能力导致我没有在哪里。 任何建议或提示都将是一个巨大的帮助,谢谢大家! #include #include struct listnode { struct listnode *next; int key; }; //Finds length of listnode int findLength (struct listnode *a) { struct listnode *temp = a; int i = 0; while (temp != NULL) { i++; temp = temp->next; } return i; } struct listnode * […]

冒泡排序中的分段错误

试图编写一个冒泡排序算法,对任何数据类型进行排序,其工作方式类似于C中stdlib中的qsort。 这是我编写的代码,编译它给我一个“分段错误”错误尝试使用-g编译gdb调试,这让错误更多 as: In function `testcmp’: (.text+0x21a): multiple definition of `testcmp’ /tmp/cc9ULHuO.o:new.c:(.text+0x12d): first defined here as: In function `_fini’: (.fini+0x0): multiple definition of `_fini’ /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 0 has invalid symbol index 7 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 1 has invalid symbol index 8 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 2 has invalid symbol index 9 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_ranges): relocation 0 […]

使用qsort排序字符串以检查它们是否是Anagram

static int myCompare (const void * a, const void * b) { return strcmp (*(const char **) a, *(const char **) b); } void sort1(const char *str1[],int n1) { qsort (str1,n1,sizeof (const char *), myCompare); } void sort2(const char *str2[], int n2) { qsort( str2, n2, sizeof (const char *),myCompare); } int main () { […]

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

C中更快IO的其他选择是什么?

我实现了合并排序,并将其用作此codechef问题的解决方案。 这是提交的内容 。 代码放在下面。 我认为导致执行缓慢的问题是我的IO在main函数中很慢。 我知道输入的元素数量,因此必须有一些更快的方式来读取输入而不是我正在做的方式。 是否有更快的IO方法而不是我在main函数中使用的方法? 我听说过使用buffer, fgets和sscanf但我不知道它们是否更快。 任何代码示例都会有所帮助。 #include #include void merge_parts(int arr[], int length) { int *ans; int i, j, k; int temp = length/2; ans = malloc(sizeof(int) * length); //This while and next if-else puts the merged array into temporary array ans for (j = temp, i = k = 0; […]

用指针在C中排序结构

我刚刚从C开始,对幕后发生的事情一无所知。 我正在为数据结构类动态学习它,这使得事情变得更加艰难。 更新:我已经取消了该程序,并开始使用内存并启动。 我在那里有allocate和deallocate函数,我得到一个malloc错误:Q1(9882)malloc: *对象0x7fff59daec08的错误:没有分配被释放的指针*在malloc_error_break中设置一个断点来调试 Update2这里是我的修改后的代码,它仍然缺少一些东西,我的几个printf语句没有出现: #include #include #include #include static int size = 10; struct student{ int id; int score; }; struct student* allocate(){ /*Allocate memory for ten students*/ struct student *s = malloc(size*(sizeof(struct student))); assert(s != 0); /*return the pointer*/ return s; } void generate(struct student* students){ /*Generate random ID and scores for […]

数组随机排序

我有下面的代码,我希望从文件中读取文本,在数组中存储单词,然后以随机顺序打印出来。 最终的数组是int,但应该是char,它不会给我正确的答案。 #include #include #include #include int main() { char message[10][150], buffer[150]; int i = 0; int cntr = 9; char freeArray[9]; srand(time(NULL)); freeArray[i] = rand() % cntr; FILE *file_in; file_in = fopen(“test.txt”, “r”); while (fgets(buffer, 150, file_in)) { i = rand() % cntr; strcpy(message[freeArray[i]], buffer); } while (cntr >= 0) { i = rand() […]

如何在C中对非常大的数组进行排序

我想在C中排序四百万long long s。通常我只是malloc()一个缓冲区用作数组并调用qsort()但是四百万* 8字节是一大块连续内存。 最简单的方法是什么? 为此,我对速度感到轻松。 我不想使用任何库,结果将需要在Windows和Linux下的适度上网本上运行。

从文件中读取行并创建按字母顺序排序的数组

我正在学习C,我想做这个特定的任务。 我知道有许多类似的问题和答案,但仍然……我会尝试更具体。 可以说,我有一个包含以下行的文件: program01 programs aprogram 1program prog 5program 我现在想要一个数组: 1program 5program aprogram prog program01 programs 因此,字符串中只有拉丁文小写字母和数字,没有空格。 我知道如何执行一些单独的步骤,但想要获得并感受整个(和正确的)概念,所以说。 可能它首先从文件中读取时可以做出一些排序决定? 对于我的特定情况,手动排序是首选,只是为了更好的学习和可能的优化。 可以说,一行的最大长度为256,最大行数为256.在此先感谢。