教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 幼儿教育 >

10.4.1简单选择排序

来源:网络收集 时间:2026-09-02
导读: ============================================================ e1:简单选择排序(SelectSort)。 // aaa.cpp : Defines the entry point for the console application. // #include stdafx.h #include iostream.h #include math.h #include stdio.h #inclu

============================================================
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字,全部文档内容请下载后查看。喜欢就下载吧 ……

10.4.1简单选择排序.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/1486720.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)