10.4.1简单选择排序
============================================================
e1:简单选择排序(SelectSort)。
// aaa.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#include "process.h"
#include "stdarg.h"
#include "string.h"
//
typedef int Status;
typedef int KeyType;
typedef int ElemType;
#define FALSE 0
#define TRUE 1
#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)<(b))
#define LQ(a,b) ((a)>(b))
#define LH +1 //左高。
#define EH 0 //等高。
#define RH -1 //右高。
//
#define INT_MAX 200
#define MAXSIZE 20 //一个用作示例的小顺序表的最大长度。
typedef int KeyType; //定义关键字类型为整数类型。
typedef char InfoType;
typedef struct
{
KeyType key; //关键字项。
InfoType otherinfo; //其它数据项。
}RedType; //记录类型。
typedef struct
{
RedType r[MAXSIZE+1]; //r[0]闲置或用作哨兵单元。
int length; //顺序表长度。
}SqList; //顺序表类型。
/////////////////
void SelectSort(SqList &L)
//对顺序表L作简单选择排序。
{
int i,k,j;
for(i=1;i<L.length;++i) //选择第i小的记录,并交换到位。
{
//j=SelectMinKey(L,i); //在L.r[i..L.length]中选择key最小的记录。
//
k=i;
for(j=i+1;j<=L.length;j++)
if(L.r[j].key<L.r[k].key) k=j;
//
printf("-i=%d,k=%d,L.r[k].key=%d-",i,k,L.r[k].key);
//
//
//if(i!=k) L.r[i]<-->L.r[j]; //与第i个记录交换。
//
if(i!=k)
{
L.r[0].key=L.r[k].key;
L.r[k].key=L.r[i].key;
L.r[i].key=L.r[0].key;
}
//
for(int x=0;x<=L.length;x++)
printf("%3d",L.r[x]);
printf("--\n");
//
//
}//for(i=1;i<L.length;++i)
}// SelectSort
///////
int main(int argc, char* argv[])
{
printf("-----beg-----\n");
int data[]={0,49,38,65,97,76,13,27,49};
SqList main_l;
printf("--main_l.length=%d--\n",main_l.length);
for(int i=0;i<12;i++)
printf("--i=%2d,key=%d,otherinfo=%d--\n",i,main_l.r[i].key,main_l.r[i].otherinfo);
printf("------------------after_set_[main_l.length]--\n");
main_l.length =8;
printf("--main_l.length=%d--\n",main_l.length);
for(i=0;i<12;i++)
printf("--i=%2d,key=%d,otherinfo=%d--\n",i,main_l.r[i].key,main_l.r[i].otherinfo);
printf("------------------after_set_[main_l.r[i].key]--\n");
for(i=0;i<=8;i++)
main_l.r[i].key=data[i];
printf("--main_l.length=%d--\n",main_l.length);
for(i=0;i<12;i++)
printf("--i=%2d,key=%d,otherinfo=%d--\n",i,main_l.r[i].key,main_l.r[i].otherinfo);
printf("----------------------------------------------before_SelectSort--\n");
for(int x=0;x<=8;x++)
{
printf("%3d",main_l.r[x].key);
}
printf(
"\n");
::SelectSort(main_l);
//////////
printf("----------------------------------------------after_SelectSort--\n");
for( x=0;x<=8;x++)
{
printf("%3d",main_l.r[x].key);
}
printf("\n");
//////////
printf("-----end-----\n");
return 0;
}
////////////////////////////////////////////////
-----beg-----
--main_l.length=-858993460--
--i= 0,key=-858993460,otherinfo=-52--
--i= 1,key=-858993460,otherinfo=-52--
--i= 2,key=-858993460,otherinfo=-52--
--i= 3,key=-858993460,otherinfo=-52--
--i= 4,key=-858993460,otherinfo=-52--
--i= 5,key=-858993460,otherinfo=-52--
--i= 6,key=-858993460,otherinfo=-52--
--i= 7,key=-858993460,otherinfo=-52--
--i= 8,key=-858993460,otherinfo=-52--
--i= 9,key=-858993460,otherinfo=-52--
--i=10,key=-858993460,otherinfo=-52--
--i=11,key=-858993460,otherinfo=-52--
------------------after_set_[main_l.length]--
--main_l.length=8--
--i= 0,key=-858993460,otherinfo=-52--
--i= 1,key=-858993460,otherinfo=-52--
--i= 2,key=-858993460,otherinfo=-52--
--i= 3,key=-858993460,otherinfo=-52--
--i= 4,key=-858993460,otherinfo=-52--
--i= 5,key=-858993460,otherinfo=-52--
--i= 6,key=-858993460,otherinfo=-52--
--i= 7,key=-858993460,otherinfo=-52--
--i= 8,key=-858993460,otherinfo=-52--
--i= 9,key=-858993460,otherinfo=-52--
--i=10,key=-858993460,otherinfo=-52--
--i=11,key=-858993460,otherinfo=-52--
------------------after_set_[main_l.r[i].key]--
--main_l.length=8--
--i= 0,key=0,otherinfo=-52--
--i= 1,key=49,otherinfo=-52--
--i= 2,key=38,otherinfo=-52--
--i= 3,key=65,otherinfo=-52--
--i= 4,key=97,otherinfo=-52--
--i= 5,key=76,otherinfo=-52--
--i= 6,key=13,otherinfo=-52--
--i= 7,key=27,otherinfo=-52--
--i= 8,key=49,otherinfo=-52--
--i= 9,key=-858993460,otherinfo=-52--
--i=10,key=-858993460,otherinfo=-52--
--i=11,key=-858993460,otherinfo=-52--
----------------------------------------------before_SelectSort--
0 49 38 65 97 76 13 27 49
-i=1,k=6,L.r[k].key=13- 13 13 38 65 97 76 49 27 49--
-i=2,k=7,L.r[k].key=27- 27 13 27 65 97 76 49 38 49--
-i=3,k=7,L.r[k].key=38- 38 13 27 38 97 76 49 65 49--
-i=4,k=6,L.r[k].key=49- 49 13 27 38 49 76 97 65 49--
-i=5,k=8,L.r[k].key=49- 49 13 27 38 49 49 97 65 76--
-i=6,k=7,L.r[k].key=65- 65 13 27 38 49 49 65 97 76--
-i=7,k=8,L.r[k].key=76- 76 13 27 38 49 49 65 76 97--
----------------------------------------------after_SelectSort--
76 13 27 38 49 49 65
…… 此处隐藏:3179字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [幼儿教育]【完整版】2019-2025年中国药物发现外
- [幼儿教育]2018-2019年初中信息技术广东初一竞赛
- [幼儿教育]最新外研版(一起)小学英语五年级上册《
- [幼儿教育]农业推广与创新管理专业 -中农大毕业论
- [幼儿教育]2017-2022年中国更年期用药行业市场深
- [幼儿教育]数学1.1.2第1课时棱柱、棱锥和棱台的结
- [幼儿教育]二年级群文阅读课例欣赏
- [幼儿教育]2010-2015年中国保险行业投资分析及深
- [幼儿教育]厄运打不垮的信念第一课时
- [幼儿教育]巧用文本,让表达在言语中绽放论文
- [幼儿教育]中学生百科知识竞赛题及答案
- [幼儿教育]八大菜系英文简介
- [幼儿教育]中国男装牛仔裤市场发展研究及投资前景
- [幼儿教育]远程数字视频监控系统在银行的应用
- [幼儿教育]光纤光缆制造工艺及设备
- [幼儿教育]国家安全法试题及答案
- [幼儿教育]2011高中提前招生及竞赛试题(物理卷1)
- [幼儿教育]宁夏第三产业房地产业、科学研究和技术
- [幼儿教育]中兴通讯 ME3000模块用户硬件设计手册_
- [幼儿教育]紫外线灯管的辐照强度问题
- 苏联东欧剧变的原因和历史教训浅析
- 人工智能导论实验报告(学生)
- 思科ITE章考试原题及答案
- 《学习雷锋好榜样》主题班会教案
- 加油站建设项目安全评价报告
- 剖析社保卡管理系统
- 2017-2018年影视剧新媒体版权运营行业
- 2017-2018学年四川省成都市高一上学期
- 2019最新高中数学 第三章 3.2.1 几类不
- 2011-2015年中国基酸市场调查及行业前
- 人教版新课标选修八Unit 1 课件Warming
- 郭溪燎原小学辅导学生记录表
- 教师资格证统考综合素质写作秘笈
- 国外校园绿色建筑研究方向与建设实践
- 15.1 动物运动的方式 课件(北师大版八
- 民用飞机空调系统
- 长安侠文化传统与唐诗的任侠主题
- 《中国近现代史纲要》名词解释
- 11金本《保险学概论》复习资料
- 民用建筑机电安装工程专业施工图图纸会




