Searching and Sorting
来源:互联网 发布:linux卸载jdk1.5 编辑:程序博客网 时间:2024/05/17 01:19
前言
在逆向cm时, 看到人家排序用到了qsort.
从来没用过…, 做个试验.
记录
// test1958.cpp : Defines the entry point for the console application.// Searching and Sorting - qsort#include "stdafx.h"#include <windows.h>/* QSORT.C: This program reads the command-line * parameters and uses qsort to sort them. It * then displays the sorted arguments. */#include <stdlib.h>#include <string.h>#include <stdio.h>int compare1(const void* arg1, const void* arg2);void testcase1();int compare2(const void* arg1, const void* arg2);void testcase2();int main(int argc, char* argv[]){ // testcase1(); testcase2(); system("pause"); return 0;}void testcase2(){ int i = 0; int iAry[6] = { 1, 2, 3, 4, 5, 6, }; /* Output original list: */ for (i = 0; i < (sizeof(iAry) / sizeof(iAry[0])); ++i) { printf("%d ", iAry[i]); } printf("\n"); /* Eliminate argv[0] from sort: */ /* Sort remaining args using Quicksort algorithm: */ qsort((void*)iAry, (size_t)(sizeof(iAry) / sizeof(iAry[0])), sizeof(iAry[0]), compare2); /* Output sorted list: */ for (i = 0; i < (sizeof(iAry) / sizeof(iAry[0])); ++i) { printf("%d ", iAry[i]); } printf("\n");}int compare2(const void* argR, const void* argL){ // argL为list靠左边的数据 // argr为list靠右边的数据 // 这里传来的是元素的引用 /* Compare all of both strings: */ int iR = *(int*) argR; int iL = *(int*) argL; if (iL > iR) { return 1; } else if (iL == iR) { return 0; } else { return -1; } /** 送来的数据次序 1,2 1,3 1,4 1,5 1,6 6,2 2,3 2,4 2,5 6,5 5,3 3,4 6,5 5,4 6,5 */ // 1,2,3,4,5,6 => 6,5,4,3,2,1}void testcase1(){ int i = 0; const char* szMsg[6] = { "msg6", "msg5", "msg4", "msg3", "msg2", "msg1", }; /* Output original list: */ for (i = 0; i < (sizeof(szMsg) / sizeof(szMsg[0])); ++i) { printf("%s ", szMsg[i]); } printf("\n"); /* Eliminate argv[0] from sort: */ /* Sort remaining args using Quicksort algorithm: */ qsort((void*)szMsg, (size_t)(sizeof(szMsg) / sizeof(szMsg[0])), sizeof(szMsg[0]), compare1); /* Output sorted list: */ for (i = 0; i < (sizeof(szMsg) / sizeof(szMsg[0])); ++i) { printf("%s ", szMsg[i]); } printf("\n");}int compare1(const void* arg1, const void* arg2){ // 这里传来的是元素的引用 /* Compare all of both strings: */ return _stricmp(* (char**) arg1, * (char**) arg2);}
0 0
- Sorting and Searching
- Searching and Sorting
- Searching and Sorting
- Searching and Sorting
- Searching and Sorting
- Sorting and Searching (1)----bubbleSort
- CareerCup Chapter 9 Sorting and Searching
- uva450-sorting&searching
- uva10420--sorting&searching
- 【索引】Sorting/Searching
- 【索引】Sorting/Searching
- 【索引】Sorting/Searching
- 【索引】Sorting/Searching
- 【索引】Sorting/Searching
- 比较,排序,搜索字符串对象( Comparing, Sorting, and Searching String Objects )CFString
- Sorting&Searching 旋转数组二分法查找 @CareerCup
- log and searching
- Searching and Ranking
- 图解Linux命令之--mkfs命令
- python日常
- Hadoop渐进四:MapRaduce流程简述
- CDOJ 我要长高 (单调队列优化DP)
- [勇者闯LeetCode] 111. Minimum Depth of Binary Tree
- Searching and Sorting
- C语言学习历程——Training01整数算法训练04
- ROS学习笔记(四) 先锋机器人跟随
- Linux内核TCP/IP参数分析与调优
- jQuery阻止事件冒泡
- javaweb项目在tomcat中去掉端口去掉项目名访问
- CSDN日报20170429 ——《程序修行从“拔刀术”到“万剑诀”》
- [编程题] 买帽子
- Resource Descriptors for ACPI