用C语言对数据进行排序

ANI*_*ANE 0 c sorting

我对下面的要求几乎没有帮助,因为我对C语法知之甚少.

我有这样的文件中的数据

73 54 57 [52]
75 73 65 [23]
65 54 57 [22]
22 59 71 [12]
22 28 54 [2]
65 22 54 73 [12]
65 28 54 73 [52]
22 28 65 73 [42]
65 54 57 73 [22]
22 28 54 73 [4]
Run Code Online (Sandbox Code Playgroud)

括号中的值表示该系列的出现.我需要根据顶部最大元素下降的数据的出现对这些数据进行排序,如下所示.

65 28 54 73 [52]
22 28 65 73 [42]
65 54 57 73 [22]
65 22 54 73 [12]
22 28 54 73 [4]
28 59 71 [122]
73 54 57 [52]
22 28 65 [26]
..
.
.
.
Run Code Online (Sandbox Code Playgroud)

等等...

什么是快速代码?

我正在尝试这个

#include<string.h>
#include <stdio.h>
int main() {

    FILE *infile;
    char fname[40]="resultFile1.txt";
    char line[100];
    int lcount=0;
    if((infile = fopen(fname, "r")) == NULL) {
        printf("Error Opening File.\n");
    }
    char *Arr[23];// need to be dynamic
    while( fgets(line, sizeof(line), infile) != NULL ) {
        stringArray[lcount]=line;
        lcount++;
        Arr[lcount]=line;
    } fclose(infile);
    int i;
    for (i = 0; i < lcount; i++) {
        printf(Arr[i]);// not able to get Arr
    }
}
Run Code Online (Sandbox Code Playgroud)

unw*_*ind 10

我会:

  1. 将文本作为一大块文本加载到内存中.
  2. 找到每一行的开头,创建一个字符串指针数组到数据块.
  3. 编写一个比较函数,通过查找"[number]"模式来比较两行.
  4. qsort()使用比较函数调用我的行指针数组.
  5. 完成.:)

  • @ANIL:这听起来像是家庭作业.如果你展示了你尝试过的东西,并询问它为什么不起作用,那么人们会帮助你.如果你没有表现出任何努力,那么人们将是无情的. (6认同)
  • @Anil:这是作业吗?如果是这样,如果目标是学习C给你的程序不是帮助你.OTOH如果你只是需要结果,使用Perl或Python编写它比C更简单(并且几乎同样快)...为什么C如果你厌倦了语法? (2认同)