answersLogoWhite

0


Best Answer

It depends what language you are using and how the function is implemented. However, generally, you need to pass 4 arguments to the function:

1. A reference to the array.

2. The lower bound of the sub-array to be sorted (usually 0).

3. The upper bound of the sub-array to be sorted (usually n-1 for an array of n elements).

4. A binary predicate to perform comparisons between the elements (usually a less-than predicate).

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

In order to sort the name stored in an array of pointer to string, qsort is used. The expression involved is, qsort((void*) list, 3, sizeof(list[0]), sort_function);

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you use qsort function to sort an array of structures?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write c program to find median?

If you are using an array : sort using qsort() then take middle element.


What is sort in C programming pls ans. this... thanks?

There is a built-in qsort function, see stdlib.h


Simple c program for quicksort?

#include<stdio.h> void print(int a[]) { int i; for( i=0;i<=8;i++) { printf("%d ",a[i]); } } int Qsort(int data[], int left, int right) { int mid,tmp,i,j; i = left; j = right; mid = data[(left+right)/2]; do { while (data[i] < mid) i++; while (mid < data[j]) j--; if (i <= j) { tmp = data[i]; data[i] = data[j]; data[j] = tmp; i++; j--; } } while (i <= j); { if (left < j) Qsort(data,left,j); if (i < right) Qsort(data,i,right); } } main() { int array[]={12,99,4,99,12,12,13,10,13}; printf("Before sort:\n\n"); print(array); Qsort(array,0,8); printf("\n\nAfter sort:\n\n"); print(array); printf(""); }


How do you write an algorithm that puts a list of names in ascending order?

In C language, you can use the string comparing method/*try it, you,ll understand, anr this is not a example done by me. this is the example which i used to learn it*/#include #include #include /* qsort int comparison function */int int_cmp(const void *a, const void *b){const int *ia = (const int *)a; // casting pointer typesconst int *ib = (const int *)b;return *ia - *ib;/* integer comparison: returns negative if b > aand positive if a > b */}/* integer array printing function */void print_int_array(const int *array, size_t len){size_t i;for(i=0; iprice - 100.f*ib->price);/* float comparison: returns negative if b > aand positive if a > b. We multiplied result by 100.0to preserve decimal fraction */}/* qsort struct comparision function (product C-string field) */int struct_cmp_by_product(const void *a, const void *b){struct st_ex *ia = (struct st_ex *)a;struct st_ex *ib = (struct st_ex *)b;return strcmp(ia->product, ib->product);/* strcmp functions works exactly as expected fromcomparison function */}/* Example struct array printing function */void print_struct_array(struct st_ex *array, size_t len){size_t i;for(i=0; i


Sorting an array in PHP without using sort function?

plz as soon as possible give me the program for shorting an array in asscending order without using any sort function in c++


Which is the best sorting methad?

There is no one best sorting method. The qsort() function is a good all rounder. The best sorting method depends on what you want to sort and how many items you need to sort and can only be determined by actual testing.


How do you implement a quick sort in Haskell?

-- empty list is already sorted qsort [] = [] -- choose first element as pivot, -- put all elements less than x on the left, -- put all elements greater than x on the right, -- recurse on both sides qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)


How do you sort the given contents of an array?

You would sort the given elements of an array by a bubble sort or heap sort code!!


How do you sort variable in ascending or descending order both in php?

Assuming that the values are stored in an array, you can use the php function sort($array) to sort ascending, and rsort to sort descending. The following link gives a table that lists all of the built in PHP sort functions: http://php.net/manual/en/array.sorting.php


Sorting of array through function in java programming?

// the build in sorting functions in Java will sort pretty much any array // of Comparable objects or primitives Arrays.sort(someArray);


How do you sort array in php without use of sort function?

$arr=array(2,5,4,6,7,8,1); for($i=0;$i<count($arr);$i++) { for($j=$i;$j<count($arr);$j++) { if($arr[$i] > $arr[$j]) { $temp=$arr[$i]; $arr[$i]=$arr[$j]; $arr[$j]=$temp; } } }


When quick sort is preferred?

When you want to sort an array.