Namespaces
Variants
Views
Actions

Difference between revisions of "c/algorithm/qsort"

From cppreference.com
< c‎ | algorithm
Line 41: Line 41:
  
 
Until {{tt|qsort_s}}, users of {{tt|qsort}} often used global variables to pass additional context to the comparison function.
 
Until {{tt|qsort_s}}, users of {{tt|qsort}} often used global variables to pass additional context to the comparison function.
 
===Example===
 
{{example
 
| code=
 
pr = raw_input().split()
 
n = int(pr[0])
 
p = int(pr[1])
 
lista = raw_input().split()
 
for j in range(0,n):
 
    lista[j]=float(lista[j])
 
afi = ""
 
newlista = []
 
curr = 0
 
for i in range(0, n):
 
    curr = curr + lista[i]
 
medie=0
 
for zile in range(0,p):
 
    if zile != 0:
 
        curr = curr - lista[zile-1] + medie
 
    medie = curr
 
    medie = float(medie/n)
 
    lista.append(medie)
 
    afi = "{:.2f}".format(medie)
 
    if zile == 0:
 
        max = medie
 
        min = medie
 
    else:
 
        if max < medie:
 
            max = medie
 
        if min > medie:
 
            min = medie
 
    if zile != (p-1):
 
        print afi,
 
    else:
 
        print afi
 
 
dif = max - min
 
afi = "{:.2f}".format(dif)
 
print afi
 
| output=
 
-2147483648 -743 -2 0 2 4 99
 
}}
 
 
 
 
Daca iti da eroare adauga-o pe asta!
 

Revision as of 00:10, 1 September 2019

Defined in header <stdlib.h>
void qsort( void *ptr, size_t count, size_t size,
            int (*comp)(const void *, const void *) );
(1)
errno_t qsort_s( void *ptr, rsize_t count, rsize_t size,

                 int (*comp)(const void *, const void *, void *),

                 void *context );
(2) (since C11)
1) Sorts the given array pointed to by ptr in ascending order. The array contains count elements of size bytes. Function pointed to by comp is used for object comparison.
2) Same as (1), except that the additional context parameter context is passed to comp and that the following errors are detected at runtime and call the currently installed constraint handler function:
  • count or size is greater than RSIZE_MAX
  • ptr or comp is a null pointer (unless count is zero)
As with all bounds-checked functions, qsort_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <stdlib.h>.

If comp indicates two elements as equivalent, their order in the resulting sorted array is unspecified.

Parameters

ptr - pointer to the array to sort
count - number of element in the array
size - size of each element in the array in bytes
comp - comparison function which returns ​a negative integer value if the first argument is less than the second, a positive integer value if the first argument is greater than the second and zero if the arguments are equivalent.

The signature of the comparison function should be equivalent to the following:

 int cmp(const void *a, const void *b);

The function must not modify the objects passed to it and must return consistent results when called for the same objects, regardless of their positions in the array.

context - additional information (e.g., collating sequence), passed to comp as the third argument

Return value

1) (none)
2) zero on success, non-zero if a runtime constraints violation was detected

Notes

Despite the name, neither C nor POSIX standards require this function to be implemented using quicksort or make any complexity or stability guarantees.

Unlike other bounds-checked functions, qsort_s does not treat arrays of zero size as a runtime constraint violation and instead returns successfully without altering the array (the other function that accepts arrays of zero size is bsearch_s).

Until qsort_s, users of qsort often used global variables to pass additional context to the comparison function.