qsort « Development « C Q&A

Home
C Q&A
1.assembly
2.buffer
3.Card
4.Cast
5.compile
6.console
7.const
8.constructor
9.database
10.Date
11.Debug
12.Design
13.Development
14.DLL
15.encrypt
16.enum
17.eof
18.Event
19.fork
20.Format
21.gcc
22.gdb
23.graph
24.graphics
25.gui
26.Holiday Event
27.image
28.IP
29.iterator
30.macro
31.makefile
32.malloc
33.Menu
34.mysql
35.network
36.openssl
37.operator
38.password
39.pipe
40.preprocessor
41.printf
42.pthread
43.Regular expression
44.scanf
45.semaphore
46.SerialPort
47.server
48.Socket
49.sql
50.SQLserver
51.sscanf
52.std
53.stdin
54.stdout
55.stl
56.strcmp
57.stream
58.switch
59.Template
60.thread
61.timer
62.unix
63.video
64.Virtual
65.visualstudio
66.winapi
67.windows
68.xml
C Q&A » Development » qsort 

1. Stabilizing the standard library qsort?    stackoverflow.com

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:

  ...

2. how to implement qsort in C    stackoverflow.com

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 ...

3. Warning when using qsort in C    stackoverflow.com

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
 ...

4. Is the manpage of qsort(3) right?    stackoverflow.com

The manpage of the qsort(3) library routine gives an example of sorting the words given as arguments on the command-line. The comparison function reads as follows:

static int
     ...

5. Pass extra parameter to comparator for qsort    stackoverflow.com

I'm just wondering if there's a way for me to pass an extra parameter to my comparator which will then be used in my qsort function? For example I have these 2 ...

6. C qsort not working correctly    stackoverflow.com

I don't know what I'm doing wrong but the following code does not sort the array properly.

#include <stdio.h>
#include <stdlib.h>

int compare(const void* a, const void* b)
{
    return (*(int*)a - ...

7. Segfault using qsort    stackoverflow.com

I need to sort an array of characters in order to iterate over it and print out the unique data points and their count. This array is held inside a ...

8. Deciphering qsort behavior    stackoverflow.com

I need the functionality of qsort for my program to run, and so far hasn't been doing its job. I'm essentially sorting an array of single character values in order to clump ...

9. MPI Gather and qsort    stackoverflow.com

I am trying to do a simple sort, where every non-zero proc reads a file (filename = proc#) into a buffer. The zero proc gathers all these bufs, sorts it and ...

10. qsort with key value pairs    stackoverflow.com

I am using the C library function qsort to sort a bunch of integer keys. Any ideas, suggestions, pointers on how I can extend it to sort key-value pairs, where the ...

11. calling qsort    bytes.com

Ron Ford K&R has three different versions of qsort, and the ultimate one is supposed to be like the one in the std library. I'm trying to implement the first, which is in 4.10. I think I'm pretty close with this: void qsort(int v[], int left, int right) { int i, last; void swap(int v[], int i, int j); if (left ...

12. qsort    bytes.com

I'm trying to figure out qsort(). I haven't seen any practical examples, only synopsis. In the code below, the array is not sorted. Can someone give me some help? #include #include int compare(const void* a, const void* b); int main(void) { int idx; int array[] = {243, 12, 99, 106, 122, 77, 242}; qsort(array, 7, 4, &compare); for(idx=0; idx<7; ...

13. Re: qsort problem    bytes.com

I wrote a compare function for qsort. I tested version 1 and version 2 (below). Version 1 worked but version 2 did not work. Why? /* * Version 1 * Compare two numbers, used in qsort. */ int compare(const void *a, const void *b) { int *val1, *val2; val1 = (int *) a; val2 = (int *) b; return (*val1 *val2) ...

14. qsort from kernighan book-question    bytes.com

I read from the kernighan and ritchie book about this function: [PHP]/* qsort: sort v[left]...v[right] into increasing order */ void qsort(int v[], int left, int right) { int i, last; void swap(int v[], int i, int j); if (left >= right) /* do nothing if array contains */ return; /* fewer than two elements */ swap(v, left, (left + right)/2); /* ...

15. qsort descending order    bytes.com

In the ISO document http://www.open-std.org/jtc1/sc22/wg...docs/n1124.pdf in the detail of the qsort library function in 7.20.5.2, the description says that, the contents of the array are sorted into ascending order. MY QUESTION: Does it mean to include descending order also ? If not, suppose we write the comparison function in such a way that it returns less than, equal to, or greater ...

16. non-standard behaviour of qsort on SunOS 5.8?    bytes.com

P: n/a arne.muller@gmail.com Hello, I just had to find out that a program that runs fine under linux and True64 alpha crashed with a segmentation fault under SunOS 5.8. I drilled down and it appears that if the sun qsort gets a vector in which all elements are equivalent, my comparsion function raises SEGSEGV. The problem seems to be that all ...

17. qsort - Segmentation Fault    bytes.com

KS wrote: I'm writing some c++ code after a few years with Java and your help is appreciated. I am getting a core dump on a call to qsort. Can you take a look at the code and see if there are any obvious errors? (The function main is at the bottom of the file.) [...] Please follow the recommendations in ...

18. Qsort help needed    cboard.cprogramming.com

#include #include int compare (const void * a, const void * b); int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main(){ char values[500]; char buffer[500]; int size = 100; int i = 0; int n; FILE *fp; fp = fopen("nums.bin", "r"); while(fgets(buffer, size, fp) != NULL) { values[i] ...

19. qsort help    cboard.cprogramming.com

Hey guys, I'm having trouble with qsort. I just want to sort 10 numbers in ascending order. I've even copied in the code from the examples on this and other sites. The problem is that when I enter the numbers from 1-10 in that order, it sorts fine. But sometimes for no apparent reason(usually if I start with 10) it sorts ...

21. qsort example    cboard.cprogramming.com

Hi, i found this piece of code in K&R book, but i have not understood how it works, page 74, for example, what represent "int left, int right" variables and how should i use it? I know this is not the complete qsort code inside standard library. Code: /* qsort: sort v[left]...v[right] into increasing order */ void qsort(int v[], int left, ...

22. HELP me with my Qsort Program    cboard.cprogramming.com

Code: #include #define maxsize 10 void quicksort (int a, int b); //void mergesort (); float ES26[11]; int i, j, k, x=1, mean, median, mode; float *ptr, sum; int main () { printf("Input at most 10 floating point numbers, each entry followed by ENTER. \nInput 0 if you want to stop:\n"); for (i=1, j=1; j==1 || i==10; i++) { printf("%d) ", ...

23. qsort -> please explain-it    cboard.cprogramming.com

/* qsort: sort v[left]...v[right] into increasing order */ void qsort(int v[], int left, int right) { int i, last; void swap(int v[], int i, int j); if (left >= right) /* do nothing if array contains */ return; /* fewer than two elements */ swap(v, left, (left + right)/2); /* move partition elem */ last = left; /* to v[0] */ ...

24. Help with qsort    cboard.cprogramming.com

Code: #include #include int main() { /*Promot user to enter file name*/ char infile_name[20]; printf("File:"); scanf("%s", infile_name); printf("\n"); /* Open the file */ FILE *infile = fopen(infile_name, "r"); if (!infile) { /* If the operation was unsuccessful */ printf("unable to open %s\n", infile_name); exit(1); } /* Read the file */ int in; char instr; do { fscanf(infile, "%d", &in); ...

25. Urgent Help - Qsort    cboard.cprogramming.com

26. how to use qsort    cboard.cprogramming.com

zcrself 12-28-2009 12:45 PM why no print? train.h Code: //PARSEPARAarse para by step by step int parseP(const char *a,float *p) { float start,end,step; int pCount = 0; int pNum = 0; pNum = sscanf(a,"%f-%f-%f",&start,&end,&step); //printf("start:[%f]\n",start); if ( pNum < 3 ) { printf("Error:format of windowSize,K-value,noequalPunish and percentRatio must be start-end-step.\n"); exit(0); } if ( end < start ) { printf("Error: end ...

27. qsort question    cboard.cprogramming.com

void sortrows(double** pMatrix,int nStartRow, int nStopRow, int nColumn) { sortrowsCompareFcn(NULL, &nColumn); // Set up our sort column. qsort((void*) (pMatrix+nStartRow), nStopRow-nStartRow+1, sizeof(double*), &sortrowsCompareFcn); } int sortrowsCompareFcn(const void * a, const void * b) { static int nSortColumn= 0; if (a == NULL) { nSortColumn = *(int*)b; return 0; } double** val1 = (double**) a; double** val2 = (double**) b; if (val1[0][nSortColumn] < ...

28. Using Library qsort() to Compare doubles    cboard.cprogramming.com

29. Problems with qsort    cboard.cprogramming.com

To understand why the curious pointer conversions in a qsort comparison function are necessary (and why a cast of the function pointer when calling qsort can't help), it's useful to think about how qsort works. qsort doesn't know anything about the type or representation of the data being sorted: it just shuffles around little chunks of memory. (All it knows about ...

30. How would I qsort in this case    cboard.cprogramming.com

#define MAX_USERS 10 struct utmp info[MAX_USERS]; int compare (const void *a, const void *b) { const struct utmp *ma = a; const struct utmp *mb = b; int32_t time1, time2; time1 = ma->ut_time; time2 = mb->ut_time; if(time1 < time2) return -1; else if(time1 > time2) return 1; else return 0; } void print_session(void) { int i; for(i=0; i < counter; i++) ...

31. trouble with qsort    cboard.cprogramming.com

I have written this to test qsort. The array takes the values, but qsort isn't working in my code. I think that there may also be a problem with calloc, becasue I am able to enter more entries than supp;ied by calloc, what do you think? Code: /*A program that reads into an array created to hold x amount of ints, ...

32. How to write my own qsort routine    cboard.cprogramming.com

Hi All, This is my first post to this group so bear with me please. I've got a homework assignment to write my own version of the ANSI C qsort function. My qsort should mimic the ANSI C version and have the same prototype and behavior as qsort. The prototype is as follows in my header file: Code: extern void qsort_of( ...

33. qsort problem    cboard.cprogramming.com

Code: #include #include #include #define MAXINPUT 80 #define MAXLINES 200 int compare(const void *name1, const void *name2) { return strcmp((char *)name1, (char *)name2); } int main(void) { FILE *fp; char *p; char input[MAXINPUT]; char *lineptr[MAXLINES]; int i; int nlines = 0; printf("Name of file with names to sort: "); fgets(input, MAXINPUT, stdin); if ((p = strchr(input, '\n')) != ...

34. problem with qsort()    cboard.cprogramming.com

#include #include #include #include #include int sort_function( const void *a, const void *b); int list[8] = { 12,34,56,2023, 3445,223, 5454,4 }; int main(void) { int x; clrscr(); qsort((void *)list, 8, 2, sort_function); for (x = 0; x < 8; x++) printf("%d\n", list[x]); // return 0; getch(); } int sort_function(const void *a, const void *b) { int ...

35. Throw away members in qsort    cboard.cprogramming.com

Code: /* I need help How can I move away from qsort all members that are not have enought points */ /* I wanna do some filer function (base on qsort) that throw away all field elements that doesent satisfy criterion (what is incoming argument of function filer)*/ /* In function does_have_enought_points all members with let's say udenr 100 points. drop ...

36. Help here with qsort!    cboard.cprogramming.com

#include #include struct DATA { char name[10]; char last_name[10]; }data[1024]; int main() { int i = 0, n; char temp[30]; printf("How many elements you wanna insert : "); scanf("%d", &n); for(i = 0; i < n; i++) { printf("\nInsert name : "); scanf("%s", &temp); strcpy(data[i].name, temp); printf("Insert lastname : "); scanf("%s", &temp); strcpy(data[i].last_name, temp); } for(i = 0; i ...

37. bsearch( ) and qsort( ) not agreeing    cboard.cprogramming.com

38. qsort problem    cboard.cprogramming.com

39. qsort    cboard.cprogramming.com

prelude, you aren't being nearly as helpful as usual! Anyway, recursion can be used and is an implementation specific detail. I'll loosely explain quicksort but just because I have family over and I am trying to escape them. quicksort basically does this.... take array, choose number which can be logical "half way" point. move all less than to the left of ...

40. qsort and linked-list    cboard.cprogramming.com

41. qsort    cboard.cprogramming.com

42. Question About Using qsort    cboard.cprogramming.com

43. qsort    cboard.cprogramming.com

I'll assume you mean you're using a two dimensional character array? Yes? Ok, pass the array name as the object to be sorted, and write your sort function so that it copies the contents of one row to a temp variable, copy the other row over top of the first, and then the temp array over the second: char temp[BUFSIZ]; strcpy( ...

44. qsort problems... help!    cboard.cprogramming.com

Hi im new to this board but ive heard great things about it, and hopefully someone can help me out here: Im trying to use the built in c function qsort to sort my table of characters and their frequencies (writting the Huffman's Algorithm) . But for some reason my code core dumps inside my compare function. I debugged it and ...

45. qsort Explanation    cboard.cprogramming.com

A type in parenthesis is known as a "cast". This is used to tell the computer that you want to use one type of variable as another. In this case, the function takes void pointers. A pointer is a variable that points to another one. A void pointer can point to any data type. The values are type cast to tell ...

46. qsort and unions    cboard.cprogramming.com

I have a binary file that contains vaious length records that are held in structures. Here is a sample of the file :- I10014 010006 0002 I10006 010006 0001 I10006 049301 0009 I10022 012807 0008 I10049 012106 0099 I10065 054402 0100 C10081 COVER SYSTEMS LTD ALBOY RD;KINVER;SOUTH STAFFS; ; 000234500 0010000 I10103 020109 0507 I13005 030309 0007 I14001 013404 0044 I20001 ...

47. what's wrong with this qsort code?    cboard.cprogramming.com

48. qsort please help. Thanks    cboard.cprogramming.com

49. help with c project using qsort    cboard.cprogramming.com

50. qsort problem    cboard.cprogramming.com

here is an example of qsort with array of structures, copy and run on your compiler... Code: #include #include #include typedef struct { char name[20]; char number[5]; } COMPANY; int compare1 (const void *a, const void *b); int compare2 (const void *a, const void *b); void main () { COMPANY customer[5]={ {"GREENS PAINTS", "2812"}, {"ALBANY STORES", "5612"}, {"STATIC ...

51. qsort    cboard.cprogramming.com

the program gets 6 random numbers. i've been trying to use the qsort function to print them in ascending order. it seems to be printing the location in memory where they are stored rather than the numbers themselves.. #include #include #include //for seeding randomiser #define MAX 6 int rnd(int range); void seedrnd(void); int intcmp(const void *v1, const void ...

52. Qsort    cboard.cprogramming.com

There is a question here. Write a function which, given an array of structures specified by: struct item { int ref_num,minutes,cost_per_min; float total_weight; }; recorders the array(in place) using the qsort() function in stdlib.h so that the array elements have the 'sort_field'(th) field arrange in ascending order after the sort(sort_field has valid values 1 to 4). The 'num_items' parameter indicates the ...

53. Qsort    forums.devshed.com

Hey, i want to sort an array by using 4 processes in parallel. i have 4 processes that should sort the same array. i have to create 4 fork, and make a sort on the same array. i am having an array, that i read from file-fd, in size. the number of elements is n. i send it to a qsort ...

54. Qsort    forums.devshed.com

I have the following: Code: #include #include #include #include static int compare (const void *p1, const void *p2) { /* The actual arguments to this function are "pointers to pointers to char", but strcmp() arguments are "pointers to char", hence the following cast plus dereference */ return strcmp(* (char * const *) p1, * (char * const ...

55. C Qsort Problems    forums.devshed.com

Hi Everyone, I'm new to C and I'm creating a program for indexing words in a given text file. I'm storing each word in an (char **) array then trying to sort these in alphabetical order. Problem is when I pass this array into qsort it sorts the words into what seems like random order. Any ideas on why this doesn't ...

56. Help with Qsort    forums.devshed.com

Nevermind. I did it without qsort but I think I was pretty clear what I need to do in my original post. Array1= {1,4,5,8,9,10,11} Array2={9,11,4,5} Array2 is always a subset of Array1. Sort Array2 in the order that Array1 is set up. Simple really. So array2 would be array2={4,5,9,11} I just wanted a fast method with qsort but I have another ...

57. qsort explanation    forums.devshed.com

58. another qsort question    forums.devshed.com

I get IPs from a file and I do a formula to find out where it comes from. I find where it comes from from a text database file. Now I copy over the name of the country to a 2d array, then sort it and find out how many duplicates of each there are to find the total hits from ...

59. qsort question    forums.devshed.com

First off here is my code: Code: #include #include #include int FindTotalLines(const char *PathToFile) { FILE *fp; int lines = 0; fp = fopen(PathToFile,"rb"); if(fp) { char line[512]; while(fgets(line, sizeof(line), fp) != NULL) { lines++; } } fclose(fp); return lines; } int compare(const void *a, const void *b) { return ( strcmp( (char *)a, (char *)b ) ); ...

60. Qsort, once more    forums.devshed.com

hi all is there a way to do this, sort a linked list (including a struct) using elements of that struct ? see the example code below. i googled for hours and als consulted the FAQ, i found nothing applicable. thanks. Code: /* INPUT: aaa xxx aaa q3q zzz bbb xxx bbb EXPECTED OUTPUT: using one_cmp(), sort by 1 (and 2 ...

61. bsearch( ) and qsort( ) not agreeing    forums.devshed.com

I'm making a program that's supposed to have a binary search of an array that has already been sorted by qsort( ). The qsort( ) function worked perfectly and outputted the correct sorted array, but the bsearch( ) using the same "compare" function does not seem to work correctly. I've been reading that these two functions use the same compare function ...

62. qsort and bsearch    forums.devshed.com

void logcache_addentry(logcache_user *user, unsigned long ip, double cost, unsigned long ltime, char *domain) { . . . printf("\n\n UNSORTED User: %s\n", user->user); for (i=0;icount && !data;i++) printf("Domain[%i]: %s\n",i,user->data[i]->domain); if (user->dcount >= 1) { printf("\n\n SORTED User: %s DCOUNT: %i\n", user->user,user->dcount); qsort(user->data[0]->domain,user->dcount,sizeof(logcache_user *),comp); for (i=0;idcount && !data;i++) printf("Domain[%i]: %s\n",i,user->data[i]->domain); } user->dcount = user->dcount+1; } }

63. Help with qsort    daniweb.com

64. qsort question    daniweb.com

Hi, I am sorting an double array, using qsort() #include #include ... int compar( const void *arg1, const void *arg2 ) { return ( * ( double **) arg1 >= * (double** ) arg2 ); } .... int main(int argc,char *argv[]){ ... qsort(array, size_ct,sizeof(double),compar); ... } It turns out that the following array: 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.727436 0.000000 ...

65. qsort    daniweb.com

#include int compare(const void *a, const void *b) { const char* a1 = *(const char**)a; const char* b1 = *(const char**)b; return strcmp(a1,b1); } int main() { int c = 0; char* _array[] = {"now ","is ","the ","time","for ","all ","good"}; c = sizeof(_array)/sizeof(_array[0]); qsort(_array,c, sizeof(char*),compare); cout << "\n\nThe alphabetized words are:\n"; for (int i = 0; i < c; i++) ...

66. Qsort    daniweb.com

#include #include #include int main( void ) { int fifty[50]; int i; srand( (unsigned ) time( NULL ) ); for( i = 0 ; i < 50; i++ ) { fifty[i] = rand(); } for( i = 0; i < 50; i++ ) { printf( "%d\n", fifty[i] ); } getchar(); return 0; }

67. Why is qsort not initialised here!!??    daniweb.com

//(Database Management System(DBMS), user interface and report generator for results. //Also a diagnostic tool that allows access to the database metrics. //This is to show the efficiency of the sort algorithms used. #include #include #include #include #include /******************************************************************************************/ //Structure struct CdRecords { char Artist[20]; char Album[20]; char Year[8]; char Label[20]; char Genre[20]; }; /******************************************************************************************/ void naive_sort ...

68. qsort question    tek-tips.com

int sproxy(const void* pe1, const void* pe2) { return strcmp(*(const char**)pe1,*(const char**)pe2);}int main() { char mystr[5][50]; strcpy(mystr[0], "bbbb"); strcpy(mystr[1], "aaaa"); strcpy(mystr[2], "cc"); strcpy(mystr[3], "2222"); /* I thought I can send pointer to first element of my array to qsort, but I thoutght wrong. Below code doesn't work. Produces ...

69. qsort and unions    tek-tips.com

AndyDoyle (Programmer) 31 Jan 03 9:28 I have a binary file that contains vaious length records that are held in structures.Here is a sample of the file :-I10014 010006 0002 I10006 010006 0001 I10006 049301 0009 I10022 012807 0008 I10049 012106 0099 I10065 054402 0100 C10081 COVER SYSTEMS LTD ALBOY RD;KINVER;SOUTH STAFFS; ; ...

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.