qsort

How to write a compare function for qsort from stdlib?

I have a structure: struct pkt_ { double x; double y; double alfa; double r_kw; }; typedef struct pkt_ pkt; A table of these structures: pkt *tab_pkt; tab_pkt = malloc(ilosc_pkt * sizeof(pkt)); What I want to do is to sort tab_pkt by tab_pkt.alfa and tab_pkt.r: qsort(tab_pkt, ilosc_pkt, sizeof(pkt), porownaj); Where po...

Stabilizing the standard library qsort?

I'm assuming that the good old qsort function in stdlib is not stable, because the man page doesn't say anything about it. This is the function I'm talking about: #include <stdlib.h> void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); I assume that if I change my comparison...

Sorting a list with qsort?

Hello. I'm writing a program in which you enter words via the keyboard or file and then they come out sorted by length. I was told I should use linked lists, because the length of the words and their number aren't fixed. should I use linked lists to represent words? struct node{ char c; struct node *next; }; And then how can...

Error in qsort function in Programming Pearls?

Hello, is it just me or this code in Programming Pearls is wrong (quicksort wants 2 const voids, no?) If so, is my solution right? Apologies, just learning... int wordncmp(char *p, char* q) { int n = k; for ( ; *p == *q; p++, q++) if (*p == 0 && --n == 0) return 0; return *p - *q; } int sortcmp(char **p, char **q) ...

Qsort based on a column in a c-string?

A class project involves sorting an array of strings, with each string containing an equal number of columns like this: Cartwright Wendy 93 Williamson Mark 81 Thompson Mark 100 Anderson John 76 Turner Dennis 56 The program accepts a command-line argument for which column to sort on, and should print ou...

how to implement qsort in C

I need to implement qsort in C and sort in reverse lexicographical order. I'm confused on how to create and call the comparison function. This is what I have so far.. qsort (strArr, numLines, sizeof(char*) , sort); int sort(const void * str1, const void * str2) { return (-1) * strcasecmp((char*) str1, (char*) str2); }; Eclipse is te...

bsearch function in c

If I have two functions: void SortStudents(char *studentList[], size_t studentCount) { qsort(studentList, sizeof(studentList)/sizeof(studentList[0]), sizeof(studentList[0]), Compare); } int Compare(const void *a, const void *b) { return (strcmp(*(char **)a, *(char **)b)); } That sort and compare using the qsort function, ho...

Help with pointers in C using qsort, bsearch

I'm having trouble with some of the pointer/array notation used. I have two lists and am sorting them, and then try to display them. I had 3 comments in my code below as to what the declarations are and why. My code looks like: int Compare(const void *a, const void *b); void SortStudents(char *studentList[], size_t studentCount) {...

Help with segmentation fault in function

I've been trying to solve this bsearch homework problem for awhile now. I try using my code to first search for one entry like so: int Compare(const void *a, const void *b); void SortStudents(char *studentList[], size_t studentCount) { qsort(studentList, studentCount, sizeof(studentList[0]), Compare); } int Compare(const void *a...

Read variable-length records from a buffer - weird memory issues

Hi, I'm trying to implement an i/o intensive quicksort (C++ qsort) on a very large dataset. In the interests of speed, I'd like to read in a chunk of data at a time into a buffer and then use qsort to sort it inside the buffer. (I am currently working with text files but would like to move to binary soon.) However, my data is composed o...

C++ qsort array of pointers not sorting

Hi, I am trying to sort a buffer full of variable-length records alphabetically in C++. I previously asked how to implement this, and was told to sort an array of pointers to the records. I set up an array of pointers, but realized that each pointer points to the beginning of a record, but there is no way of it knowing when the record s...

Report duplicates in array of structures using qsort?

What I'm trying to accomplish: I am making 3000 requests and capturing 8 bytes of that request. I stick the response in: struct negs { int neg_num; char neg_key[9]; }; where neg_num == i (the request number) and memcpy(nego[neg_count].neg_key, recv_data+73,8); I need to find any duplicate neg_keys in the nego struct. I am...

Is qsort thread safe?

I have some old code that uses qsort to sort an MFC CArray of structures but am seeing the occasional crash that may be down to multiple threads calling qsort at the same time. The code I am using looks something like this: struct Foo { CString str; time_t t; Foo(LPCTSTR lpsz, time_t ti) : str(lpsz), t(ti) { } }; class Sort...

Warning when using qsort in C

I wrote my comparison function int cmp(const int * a,const int * b) { if (*a==*b) return 0; else if (*a < *b) return -1; else return 1; } and i have my declaration int cmp (const int * value1,const int * value2); and I'm calling qsort in my program like so qsort(currentCases,round,sizeof(int),cmp); when i compil...

qsort on an array of pointers to Objective-C objects

I have an array of pointers to Objective-C objects. These objects have a sort key associated with them. I'm trying to use qsort to sort the array of pointers to these objects. However, the first time my comparator is called, the first argument points to the first element in my array, but the second argument points to garbage, giving me a...

c++ - Is it possible to cast object of class to void * ?

I am trying to use qsort from STL to sort array of edge: struct edge { int w,v,weight; }; by weight. What I am trying is: int compare_e(const void *a, const void *b) { return ( *(edge *)a->weight - *(edge *)b->weight ); }; But I get: `const void*' is not a pointer-to-object type EDIT: Ok thx, now my code is compil...

quick sort problem

I use qsort from C libary and I have datatype Element_type **pElement and Element_type is struct typedef element_type {int ,char ....} example, and i call quicksor function with qsort(*pElement,iCountElement,(size_t)sizeof(Element_type),compare); and callback function static int compare(const void *p1, const void *p2) { E...

Is stdlib's qsort recursive?

I've read that qsort is just a generic sort, with no promises about implementation. I don't know about how libraries vary from platform to plaform, but assuming the Mac OS X and Linux implementations are broadly similar, are the qsort implementations recursive and/or require a lot of stack? I have a large array (hundreds of thousands of...

Quicksort implementation in C?

I really like the qsort function in C. It's so easy to use and allows me to procrastinate learning C++ template types. I have a few questions about this: Is the algorithm used always a quicksort or is it compiler-implementation-dependent? Would you recommend using this function or is there a real benefit to templates? Are there any thi...

How to qsort an array of pointers to char in C?

Suppose I have an array of pointers to char in C: char *data[5] = { "boda", "cydo", "washington", "dc", "obama" }; And I wish to sort this array using qsort: qsort(data, 5, sizeof(char *), compare_function); I am unable to come up with the compare function. For some reason this doesn't work: int compare_function(const void *name1,...