C标准图书馆是否有任何图书馆职能?
For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...
C标准图书馆是否有任何图书馆职能?
<>代码>qsort() 是你重新研究的功能。 你把它称作你的系列数据、该阵列中的内容数目、每个要素的规模和比较功能。
它做的是魔法和你阵列。 例如:
#include <stdio.h>
#include <stdlib.h>
int comp (const void * elem1, const void * elem2)
{
int f = *((int*)elem1);
int s = *((int*)elem2);
if (f > s) return 1;
if (f < s) return -1;
return 0;
}
int main(int argc, char* argv[])
{
int x[] = {4,5,2,3,1,0,9,8,6,7};
qsort (x, sizeof(x)/sizeof(*x), sizeof(*x), comp);
for (int i = 0 ; i < 10 ; i++)
printf ("%d ", x[i]);
return 0;
}
C.C++ 标准图书馆<stdlib.h>
<代码>qsort功能。
This is not the best quick sort implementation in the world but it fast enough and VERY EASY to be used... the formal syntax of qsort is:
qsort(<arrayname>,<size>,sizeof(<elementsize>),compare_function);
The only thing that you need to implement is the compare_function, which takes in two arguments of type "const void", which can be cast to appropriate data structure, and then return one of these three values:
页: 1 a. 比较分类:
simply cast a and b to integers
if x < y
,x-y
is negative, x == y
, x-y = 0
, x > y
, x-y
is positive
x-y
is a shortcut way to do it :)
reverse *x - *y
to *y - *x
for sorting in decreasing/reverse order
int compare_function(const void *a,const void *b) {
int *x = (int *) a;
int *y = (int *) b;
return *x - *y;
}
2. 比较显示:
For comparing string, you need strcmp
function inside <string.h>
lib.
strcmp
will by default return -ve,0,ve appropriately... to sort in reverse order, just reverse the sign returned by strcmp
#include <string.h>
int compare_function(const void *a,const void *b) {
return (strcmp((char *)a,(char *)b));
}
3.比较浮动点号码:
int compare_function(const void *a,const void *b) {
double *x = (double *) a;
double *y = (double *) b;
// return *x - *y; // this is WRONG...
if (*x < *y) return -1;
else if (*x > *y) return 1; return 0;
}
4. 比较基于关键的记录:
Sometimes you need to sort a more complex stuffs, such as record. Here is the simplest
way to do it using qsort
library.
typedef struct {
int key;
double value;
} the_record;
int compare_function(const void *a,const void *b) {
the_record *x = (the_record *) a;
the_record *y = (the_record *) b;
return x->key - y->key;
}
确切地说,qsort(>
>)是一种执行(不一定像其名称可能暗示的那样迅速)。
Try man 3 qsort or have a Read at http://linux.die.net/man/3/qsort。
虽然在标准图书馆没有准确,但https://github.com/swenson/sort 你们只有两处主人档案,可以包括获取各种经红的快速通道,例如:
#define SORT_NAME int64 #define SORT_TYPE int64_t #define SORT_CMP(x, y) ((x) - (y)) #include "sort.h" /* You now have access to int64_quick_sort, int64_tim_sort, etc., e.g., */ int64_quick_sort(arr, 128); /* Assumes you have some int *arr or int arr[128]; */
这应至少是标准图书馆<代码>qsort<>/code>的两倍,因为它没有使用功能点,而且还有其他许多类型的算法选择。
在C89,在基本上每个C汇编者中开展工作。
http://www.un.org。
I think you are looking for qsort
.
qsort
function is the implementation of quicksort algorithm found in stdlib.h
in C/C++
.
此处称为<代码>qsort功能:
void qsort(void *base, size_t nmemb, size_t size,int (*compar)(const void *, const void *));
List of arguments:
base: pointer to the first element or base address of the array
nmemb: number of elements in the array
size: size in bytes of each element
compar: a function that compares two elements
此处是使用<条码>qsort<>/条码>来分类一个阵列的代码例子:
#include <stdio.h>
#include <stdlib.h>
int arr[] = { 33, 12, 6, 2, 76 };
// compare function, compares two elements
int compare (const void * num1, const void * num2) {
if(*(int*)num1 > *(int*)num2)
return 1;
else
return -1;
}
int main () {
int i;
printf("Before sorting the array:
");
for( i = 0 ; i < 5; i++ ) {
printf("%d ", arr[i]);
}
// calling qsort
qsort(arr, 5, sizeof(int), compare);
printf("
After sorting the array:
");
for( i = 0 ; i < 5; i++ ) {
printf("%d ", arr[i]);
}
return 0;
}
You can type man 3 qsort
in Linux/Mac terminal to get a detailed info about qsort
.
Link to qsort man page
使用qsort(
in <stdlib.h>
。
@paxdiablo
The qsort()
function conforms to ISO/IEC 9899:1990 (``ISO C90 ).
<代码>stdlib.h有几种分类功能。 您可以做以下工作:
GNU qsort sources in adlib show that it is Immediatesort.
For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...
最好、最小、最快、开放的来源、C/C++ 3d 提供方(在3ds max模型的支持下),而不是通用公平市价,
Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...
I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...
I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...
I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...
Is there anything other than DDD that will draw diagrams of my data structures like DDD does that runs on Linux? ddd is okay and runs, just kind of has an old klunky feeling to it, just wanted to ...
Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...