Tag: 排序

Java比C快吗?

到目前为止我听到的所有内容都是人们说Java通常比C慢,但有一些例外(比如涉及代码什么都不做)。 所以我出去测试它。 我有一个介于0和999,999之间的100,000个整数数组。 我在C和java(在OS X上编译)中使用了一个双循环来从最小到最大排序。 结果是Java通常在一半时间内完成。 在使用不同arrays的5次运行中,Java占用大约17秒,而C占用大约32秒(包括从文件中分配和填充arrays的时间,两者都可以忽略不计)。 那么什么会使Java代码比C运行得更快呢? 是否有我遗漏的东西,或者我听不到的一些基础技术? 编辑:也不确定它是否重要,但我使用time命令计时,而不是任何自定义代码。 例如: $time java SortArray 至于编译器选项,我现在无法访问该命令行,但它是OS X 10.10上的默认gcc选项: gcc sortarray.c -o sortarray 我只是使用默认的javac来编译Java。 javac SortArray.java C: #include #include #define SIZE 32 int main() { FILE* file = fopen(“bigarray.txt”, “r”); int arraySize = 100000; int array[100000] = {}; int i, j, temp; char inputBuffer[SIZE]; for (i = […]

将未排序的连续字符串数组有效地排序到文件中

我有一个包含无序连续数字的字符串数组(范围从0到n),例如[7a, 1b, 2c, 0d, 6e, 5f, 3g, 4h] ,我想将数字按顺序写入文件。 例如: 0d 1b 2c 3g 4h 5f 6e 7a 字符串的长度不尽相同。 我试图找到一种方法,既快速又无需占用太多​​空间。 我找到了一种方法,我可以在O(n)空间复杂度和O(n)性能中做到这一点:我创建一个包含n个单元格的数组,并将每个字符串插入到他的单元格编号中。 for (i = 0; i < n; i++) sortedArray[originalArray[i]] = originalArray[i] …类似的东西(创建原始大小的新数组并在一次运行中填充),然后与另一个for循环将已排序数组的内容写入文件。 但我正在寻找一种更好的方法来做到这一点。

有没有比定时更好的方法来对C程序进行基准测试?

我正在编写一个必须对大型数组进行排序的程序(最多400万个文本字符串)。 似乎我在这方面做得很好,因为radixsort和mergesort的组合已经将原始q(uick)排序执行时间减少了不到一半。 执行时间是主要的一点,因为这是我用来对我的代码进行基准测试的。 我的问题是: 是否有更好的(即更可靠的)基准测试程序的方式,而不仅仅是执行的时间? 它有点工作,但是如果运行两次,相同的程序(运行相同的后台进程)通常具有稍微不同的执行时间。 这有点挫败了检测小改进的目的。 一些小的改进可能会增加一个很大的… 提前感谢任何输入! 结果: 我设法让gprof在Windows下工作(使用gcc和MinGW)。 与我的普通编译器(tcc)相比,gcc表现不佳(考虑执行时间),但它给了我很多洞察力。

为什么这个qsort()不起作用?

我正在排序一个字符串数组(不区分大小写)。 qsort导致分段错误,可能是我的演员不合适。 #include #include #include int compare(const void *string1, const void *string2) { char *a = (char*)(string1); char *b = (char*)(string2); printf(“comparing %s AND %s\n”, a, b); return strcasecmp(a,b); } void sortListName(char **fileList, int noOfFiles) { printf(“Sorting\n”); qsort(fileList, noOfFiles, 260*sizeof(char), compare); return; } ** fileList =字符串数组(文件名) PS main()显而易见,工作正常。

对包含从C中的文件输入的数字的字符串进行排序?

所以我正在研究一个程序,该程序从包含每个行/项目的“项目编号”,“单价”和“购买日期”的文件中读取行。 我已经达到了可以扫描文件并以所需的图表格式组织它的程度,但我无法弄清楚如何按“项目编号”对数据进行排序。 这是我的代码: #include #include int main() { FILE *fp; char ch; fp = fopen(“f.txt”, “r”); //open the file named f.txt if (fp == NULL) //In case we can’t find the file, notify the user printf(“File not found\n”); printf(“Item \t\tUnit Price\tPurchase Date\n”); //set up the header while ((ch = fgetc(fp)) != EOF) { //set the […]

BubbleSorting C语言

我们正在学习数组,我只是围绕着泡泡排序。 我编写了以下代码来按升序对数组进行排序,但是存在问题。 我找不到它,但我知道有问题。 我找到了正确的代码,但我仍然不明白为什么这不起作用。 我的代码 int a; int i; int temp; int value[5] = { 5, 4, 3, 2, 1 }; for (i = 0; i value[i + 1]) { temp = value[i]; value[i] = value[i + 1]; value[i + 1] = temp; } } for (a = 0; a < 5; a++) { printf(" […]

如何从具有userID和pageID的大型日志文件中查找最常访问的3网页序列

给定访问的网页日志文件: Userid PageID A 1 A 2 A 3 B 2 B 3 C 1 B 4 A 4 查找最常访问的页面ID访问顺序: for A : 1-2-3, 2-3-4 for B : 2-3-4 所以,2-3-4是最常见的。 我的想法: 将文件的每个项目放入map1<key:user_id, list > 。 当list.size() == 3 ,创建一个新的struct three_hits来保存三个pageID。 将它放入map2 。 然后,在map2中找到具有最大计数器值的项目。 声明: struct three_hits { int f_page; int s_page; int t_page; }; map<string, […]

改变C中数组的大小

我编写了一个生成随机数组的程序,并使用insert和quicksort算法对其进行排序。 该程序还测量每个函数的运行时间。 arrays的大小在前导码中定义为参数化宏L 我的问题是: 如何在一次执行中使用各种大小的数组测试两种排序算法? 我希望我的程序在一次执行中对大小为L=10, 100, 1000, 5000和10000数组进行排序。 我的程序代码详述如下。 #include #include #include //Random Array Length #define MAX 100 #define L 10 void naive_sort(int[]); void smarter_sort(int[],int,int); void swap(int[],int,int); int choose_piv(int[],int,int); int main(){ int i, a[L], b[L]; clock_t tic, toc; //Generate an array of random numbers for(i=0; i<L; i++) a[i]= rand() % (MAX+1); //Define b identical […]

stdlib qsort对结构的指针数组进行排序

我试图根据存储在我知道的“桶”结构的void *中存储的值对结构的指针数组(下面的定义)进行排序。 它编译并打印出我的数组桶及其值,没有任何错误或警告,但实际上并没有对数组进行排序。 我使用asserts试图找到任何可能导致qsort错误的地方。 结构定义: typedef struct _bucket{ void* val; char *word; }bucket; typedef struct _root{ bucket **list; int hashTableLength; }root; 要传递给qsort函数的Sort函数: int sortFunc(const void *a, const void *b){ bucket *bucketA=(bucket*)a; bucket *bucketB=(bucket*)b; int bucketAVal = *((int*)bucketA->val); int bucketBVal = *((int*)bucketB->val); assert((bucketAVal&&bucketBVal)!=0); return bucketAVal-bucketBVal; } 对数组进行排序并打印: void sort(root* inRoot, int(*sortFunc)(const void *a, const void *b)){ […]

使用c语言中的递归进行合并排序

#include #include int arr[20]; void main() { int n,i; clrscr(); printf(“\n\t\t\t——Merge Sorting——\n\n”); printf(“Enter the size of array\n”); scanf(“%d”,&n); printf(“Enter the elements:\n”); for(i=0; i < n; i++) { scanf("%d",&arr[i]); } merge_sort(arr,0,n-1); printf("\n\n\t\t\t—–Merge Sorted Elements—–\n\n"); printf("Sorted array:\t"); for(i=0; i < n; i++) { printf("\t%d",arr[i]); } getch(); } int merge_sort(int arr[],int low,int high) { int mid; if(low < […]