运行 ❯
获得您
自己的
网站
×
更改方向
更改主题,深色/浅色
转至 Spaces
#include <stdio.h> #include <stdlib.h> // Comparing function: // Returns a positive number if a is greater than b // Returns a negative number if a is less than b // Returns 0 if a is equal to b int compare(const void *a, const void *b) { int *valA = a; int *valB = b; return *valA - *valB; } int main() { // Create an array int myArray[] = {20, 32, 5, 2, 24, 15}; int size = sizeof(myArray) / sizeof(myArray[0]); // Sort the values in the array qsort (myArray, size, sizeof(myArray[0]), compare); // Display the values of the array for(int i = 0; i < size; i++) { printf("%d ", myArray[i]); } return 0; }
2 5 15 20 24 32