对一个整形数组进行顺序排列
生活随笔
收集整理的這篇文章主要介紹了
对一个整形数组进行顺序排列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
#include <assert.h>
#pragma warning(disable:4996)
int cmp_int(void *x, void *y)//升序排列
{
int *_x = (int*)x;
int *_y = (int*)y;
if (*_x > *_y)
{
return 1;
}
else if (*_x < *_y)
{
return -1;
}
else
return 0;
}
int main()
{int arr[] = { 1, 5, 63, 52, 74, 33, 1, 21, 56, 5, 4 };int size = sizeof(arr) / sizeof(arr[0]);qsort(arr, size, sizeof(char *), cmp_int);system("pause");return 0;
}
這里用到一個函數 qsort:
它可以對任意類型進行排序,返回值為int類型。
而上述代碼中的cmp_int就是qsort的回調函數。
上述代碼為升序排列,若想變為降序排列,讀者只需將cmp_int 函數中返回值1和-1調換位置即可。
總結
以上是生活随笔為你收集整理的对一个整形数组进行顺序排列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 爬虫技术(05)神箭手爬虫回调函数
- 下一篇: java自由职业者_自由职业者的7个重要