- 浏览: 76265 次
- 性别:
- 来自: 西安
文章分类
- 全部博客 (70)
- JavaScript经典例子 (16)
- Java经典例子 (5)
- 其他经典例子 (0)
- 待解决的问题 (1)
- 技术难题 (3)
- 生活与工作 (3)
- 每日学习 (1)
- C++ (18)
- 书籍收藏 (2)
- DDDDDDDDDDD (0)
- Java并发编程 (1)
- hibernate (1)
- Ibatis (1)
- 数据库设计 (2)
- 设计模式 (4)
- Guarded Suspension Pattern (0)
- 线程设计模式 (0)
- Java线程设计模式 (1)
- Web标准 (1)
- Linux (1)
- ubuntu (1)
- JavaEE (1)
- 1.android自定义属性 (1)
- MacOS (1)
- 微信开发 (1)
main.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "tQsort.h"
#define NUM 10
int main(){
double arr[NUM];
fillArray(arr, NUM);
printf("before sort:\n");
showArray(arr, NUM);
qsort(arr, NUM, sizeof(double), sortArray);
printf("after sort:\n");
showArray(arr, NUM);
return 0;
}
tQort.c文件
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define TYPE double
/************************************************************************/
/*generator random number to fill double array */
/************************************************************************/
void fillArray(double arr[], int size)
{
int index;
/*Seed the random-number generator with the current time so that
the numbers will be different every time we run.*/
srand((unsigned)time(NULL));
for (index=0; index<size; index++)
{
arr[index] = (double)rand() / (RAND_MAX + 0.1);
}
}
void showArray(double arr[], int size)
{
int index = 0;
for(; index<size; index++)
{
printf("%9.4f", arr[index]);
if(index%6 == 5)
{
printf("\n");
}/*
if(index%6 != 0)
{
printf("\n");
}*/
}
printf("\n");
}
int sortArray(const void *p1, const void *p2)
{
const TYPE *a1 = p1; //得到正确类型的指针
const TYPE *a2 = p2;
if(*a1 > *a2)
{
return 1; //返回1时升序排序,返回-1时降序排序
}
else if(*a1 == *a2)
{
return 0;
}
else
{
return -1; //返回-11时降序排序,返回1时升序排序
}
}
思考:如何排序结构体数组(如struct name staff[100])
提示:修改比较函数,使用strcmp函数
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "tQsort.h"
#define NUM 10
int main(){
double arr[NUM];
fillArray(arr, NUM);
printf("before sort:\n");
showArray(arr, NUM);
qsort(arr, NUM, sizeof(double), sortArray);
printf("after sort:\n");
showArray(arr, NUM);
return 0;
}
tQort.c文件
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define TYPE double
/************************************************************************/
/*generator random number to fill double array */
/************************************************************************/
void fillArray(double arr[], int size)
{
int index;
/*Seed the random-number generator with the current time so that
the numbers will be different every time we run.*/
srand((unsigned)time(NULL));
for (index=0; index<size; index++)
{
arr[index] = (double)rand() / (RAND_MAX + 0.1);
}
}
void showArray(double arr[], int size)
{
int index = 0;
for(; index<size; index++)
{
printf("%9.4f", arr[index]);
if(index%6 == 5)
{
printf("\n");
}/*
if(index%6 != 0)
{
printf("\n");
}*/
}
printf("\n");
}
int sortArray(const void *p1, const void *p2)
{
const TYPE *a1 = p1; //得到正确类型的指针
const TYPE *a2 = p2;
if(*a1 > *a2)
{
return 1; //返回1时升序排序,返回-1时降序排序
}
else if(*a1 == *a2)
{
return 0;
}
else
{
return -1; //返回-11时降序排序,返回1时升序排序
}
}
思考:如何排序结构体数组(如struct name staff[100])
提示:修改比较函数,使用strcmp函数
发表评论
-
文件夹搜索器
2010-12-21 16:50 1089int Test(char* g_pcDir) { // ... -
封装的createDirectories方法
2010-12-18 10:48 953#include <io.h> #include ... -
堆和栈参考知识
2010-11-10 11:23 886参考知识: 操作系统中的堆和栈 栈:在Windows下,栈是向 ... -
数组和指针的区别
2010-11-10 11:21 942分析一下代码 char *GetString2(void) { ... -
堆和栈(四)
2010-11-10 10:50 9877、对象数组 class Obj { public : ... -
堆和栈(三)
2010-11-10 10:49 8814、计算内存容量 用运算符sizeof可以计算出数组的容量(字 ... -
堆和栈(二)
2010-11-10 10:46 748含有这种错误的函数每被调用一次就丢失一块内存。刚开始时系统的内 ... -
堆和栈(一)
2010-11-10 10:45 8501、内存分配方式有三种: (1)从静态存储区域分配。内存在程 ... -
const关键字
2010-11-05 16:25 802在C++中用const来定义常量,可以替换#define,co ... -
一个简单的算法
2010-11-05 15:18 6411. 设计一个函数:void find1(char array ... -
函数指针与指针函数
2010-11-03 16:06 921假定一个指针指向一个int变量,它保存着这个int变量在内存中 ... -
字符串、数组、指针(二)
2010-11-01 16:36 1136代码二:编译正常,但运行会出现内存错误 char *name ... -
字符串、数组、指针(一)
2010-11-01 16:35 905int iArr[20] = {0}; //将数组中的元素全 ... -
VC插件
2010-10-31 16:11 7791.Visual Assist(强烈推荐 ... -
简单指针的例子
2010-10-30 17:00 864#include <iostream.h> con ... -
C++函数模板
2010-10-26 11:19 837#include<iostream.h> #inc ... -
include指令
2010-10-26 09:28 788#include文件包含指令 关键字: 文件名, #inclu ...
相关推荐
使用qsort函数来对结构体数组进行快速排序,提高效率和简化代码。 使用atoi函数来将字符串形式的生日转换为整数形式,便于比较和计算年龄。 使用结构体数组来存储通讯录的信息,方便进行排序和输出。 使用qsort函数...
比如二维数组,如何根据其中的一维来进行快速排序。 这里可以考虑用结构体来实现
快速排序是一种用的最多的排序算法,在C语言的标准库中也有快速排序的函数,下面说一下详细用法。 qsort函数包含在中 qsort函数声明如下: void qsort(void * base,size_t nmemb,size_t size ,int(*compar)...
简单的介绍如下。 代码如下:/*****************************************************...base:数组首地址 num: 数组元素个数width: 每个数组元素字节数compare:比较函数 注意类型转换*********************************
void qsort(int arr[],int left,int right) //qsort()函数实现快速排序,并且是递归调用,而且,递归调用qsort()函数本身两次,因为要对中值两边的 { //部分分别进行排序。arr是待排序的数组名,left是排序的左边界,...
函数原型:void qsort(void *base,size_t num,size_t width, int (__cdecl *compare )(const void *, const void *) ); 第一个是数组地址,第二是数组大小,第三个是数组中每个元素的字节数,最后一个是个函数指针,...
∷相关函数:QSort函数 1.5.9 一趟快速排序的改进算法 248 范例1-85 一趟快速排序的改进算法 248 ∷相关函数:QuickSort函数 1.5.10 简单选择排序 250 范例1-86 简单选择排序 250 ∷相关函数:SelectSort函数 1.5.11...
∷相关函数:QSort函数 1.5.9 一趟快速排序的改进算法 248 范例1-85 一趟快速排序的改进算法 248 ∷相关函数:QuickSort函数 1.5.10 简单选择排序 250 范例1-86 简单选择排序 250 ∷相关函数:SelectSort函数 1.5.11...
∷相关函数:QSort函数 1.5.9 一趟快速排序的改进算法 248 范例1-85 一趟快速排序的改进算法 248 ∷相关函数:QuickSort函数 1.5.10 简单选择排序 250 范例1-86 简单选择排序 250 ∷相关函数:SelectSort函数 1.5.11...
∷相关函数:QSort函数 1.5.9 一趟快速排序的改进算法 248 范例1-85 一趟快速排序的改进算法 248 ∷相关函数:QuickSort函数 1.5.10 简单选择排序 250 范例1-86 简单选择排序 250 ∷相关函数:SelectSort函数 1.5.11...
∷相关函数:QSort函数 1.5.9 一趟快速排序的改进算法 248 范例1-85 一趟快速排序的改进算法 248 ∷相关函数:QuickSort函数 1.5.10 简单选择排序 250 范例1-86 简单选择排序 250 ∷相关函数:SelectSort函数 ...
∷相关函数:QSort函数 1.5.9 一趟快速排序的改进算法 248 范例1-85 一趟快速排序的改进算法 248 ∷相关函数:QuickSort函数 1.5.10 简单选择排序 250 范例1-86 简单选择排序 250 ∷相关函数:SelectSort函数 ...
∷相关函数:QSort函数 1.5.9 一趟快速排序的改进算法 248 范例1-85 一趟快速排序的改进算法 248 ∷相关函数:QuickSort函数 1.5.10 简单选择排序 250 范例1-86 简单选择排序 250 ∷相关函数:SelectSort函数 ...
快速排序(Quicksort)是对冒泡排序的一种改进。也是最快的一种排序算法。 2.核心思想: 快速排序算法通过多次比较和交换来实现排序,其排序流程如下: (1)首先设定一个分界值,通过该分界值将数组分成左右两部分...
8.15 例子:快速排序 8.16 总结 8.17 练习 第9章 结构和联合 9.1 结构 9.2 访问结构成员 9.3 操作符的优先级和结合性的总结 9.4 在函数中使用结构 9.5 结构的初始化 9.6 例子:玩扑克牌 9.7 联合 9.8 位字段 9.9 ...
快速排序算法在 Matlab 中的简单实现。 它是 O(N log N) 但唯一的优化是枢轴点是中心元素(以防止排序输入的病理行为)。 它需要一个 C 风格的比较函数。 如果要排序的向量是元胞数组,则比较函数对元胞内容起作用。...
1.24 我在一个文件中定义了一个extern数组,然后在另一个文件中使用,为什么sizeof取不到数组的大小? 13 声明问题 14 1.25 函数只定义了一次,调用了一次,但编译器提示非法重声明了。 14 *1.26 main的正确...
6.21 当数组是函数的参数时,为什么sizeof不能正确报告数组的大小? 6.22 如何在一个文件中判断声明为extern的数组的大小(例如,数组定义和大小在另一个文件中)?sizeof操作符似乎不行。 6.23 sizeof返回的大小...