📄 selectsort.cpp
字号:
/************************************
* Copyright (c) 2008,LDCI
*
* 文件名称: SelectSort.cpp
* 摘要:
* 使用 C 语言实现对顺序存储结构的线性表进行选择排序
************************************/
#include <stdlib.h>
#include <stdio.h>
#define LISTSIZE 100 // 表的长度,根据实际情况而定
typedef int DataType; // 数据,根据实际情况而定
typedef struct
{
DataType data[LISTSIZE]; // 存放所有数据的空间
int nLength; // 当前表的长度
}SeqList;
void Show(SeqList* _pList)
{
for(int i=0; i<_pList->nLength; i++)
{
printf("%d, ", _pList->data[i]);
}
printf("\n");
}
/**************************************************
* 函数名: SelectSort
* 参数:
* 形参:
* _pList : 顺序存储结构的线性表
* 返回值: 无
* 功能:
* 对顺序存储结构的线性表进行选择排序
* 作者: 左建华
* 编写明细:
* 2008-06-02 Created 左建华
*
**************************************************/
void SelectSort(SeqList* _pList)
{
int nMinIndex; // 用于记录找到的最小元素的下标值
for(int i=0; i<_pList->nLength-1; i++)
{
nMinIndex = i;
for(int j=i+1; j<_pList->nLength; j++) // 在无序区中,查找最小元素,并记录下标值
{
if(_pList->data[j] < _pList->data[nMinIndex])
{
nMinIndex = j;
}
}
int nTemp = 0;
if(nMinIndex != i) // 将最小元素依次放在有序区
{
nTemp = _pList->data[i];
_pList->data[i] = _pList->data[nMinIndex];
_pList->data[nMinIndex] = nTemp;
}
// 显示
// Show(_pList);
}
}
int main(int argc, char** argv)
{
SeqList* pList = (SeqList*)malloc(sizeof(SeqList));
pList->nLength = 8;
pList->data[0] = 49;
pList->data[1] = 38;
pList->data[2] = 65;
pList->data[3] = 97;
pList->data[4] = 76;
pList->data[5] = 13;
pList->data[6] = 27;
pList->data[7] = 49;
Show(pList);
SelectSort(pList);
Show(pList);
free(pList);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -