📄 demo_sort.cpp
字号:
// Demo_Sort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Demo_Sort.h"
#include <afxtempl.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
// 该函数模板使用冒泡法对集合元素进行排序,参数说明:
// collection 集合对象,集合对象必须提供 [] 操作。
// element 集合元素,该参数的作用仅仅是确定集合元素类型,
// 参数的值没有用,建议取集合的第一个元素。集合
// 元素必须提供复制、赋值和比较操作。
// count 集合元素的数目
// ascend 表明排序时使用升序(true)还是降序(false)
// 该函数模板支持C++数组以及MFC集合CStringArray、CArray。
template <typename COLLECTION_TYPE, typename ELEMENT_TYPE>
void BubbleSort(COLLECTION_TYPE& collection, ELEMENT_TYPE element, int count, bool ascend = true)
{
for (int i = 0; i < count-1; i++)
for (int j = 0; j < count-1-i; j++)
if (ascend)
{
// 升序
if (collection[j] > collection[j+1])
{
ELEMENT_TYPE temp = collection[j];
collection[j] = collection[j+1];
collection[j+1] = temp;
}
}
else
{
// 降序
if (collection[j] < collection[j+1])
{
ELEMENT_TYPE temp = collection[j];
collection[j] = collection[j+1];
collection[j+1] = temp;
}
}
}
void DemoSort()
{
printf("演示排序函数 BubbleSort 的用法\n\n");
printf("使用升序排序整型数组\n");
int arrayInt[] = {45, 23, 76, 91, 37, 201, 187};
int count = sizeof(arrayInt) / sizeof(int);
printf("排序前 ");
for (int i = 0; i < count; i++) printf("%d ", arrayInt[i]);
printf("\n");
printf("排序后 ");
BubbleSort(arrayInt, arrayInt[0], count);
for (i = 0; i < count; i++) printf("%d ", arrayInt[i]);
printf("\n\n");
printf("使用升序排序整数集合 CArray <int, int>\n");
CArray <int, int> collectionInt;
collectionInt.Add(45);
collectionInt.Add(23);
collectionInt.Add(76);
collectionInt.Add(91);
collectionInt.Add(37);
collectionInt.Add(201);
collectionInt.Add(187);
count = collectionInt.GetSize();
printf("排序前 ");
for (i = 0; i < count; i++) printf("%d ", collectionInt[i]);
printf("\n");
printf("排序后 ");
BubbleSort(collectionInt, collectionInt[0], count);
for (i = 0; i < count; i++) printf("%d ", collectionInt[i]);
printf("\n\n");
printf("使用降序排序字符串数组\n");
CString arrayString[] = {"eagle", "hawk", "falcon"};
count = sizeof(arrayString) / sizeof(CString);
printf("排序前 ");
for (i = 0; i < count; i++) printf("%s ", arrayString[i]);
printf("\n");
printf("排序后 ");
BubbleSort(arrayString, arrayString[0], count, false);
for (i = 0; i < count; i++) printf("%s ", arrayString[i]);
printf("\n\n");
printf("使用降序排序字符串集合 CStringArray\n");
CStringArray collectionString;
collectionString.Add("eagle");
collectionString.Add("hawk");
collectionString.Add("falcon");
count = collectionString.GetSize();
printf("排序前 ");
for (i = 0; i < count; i++) printf("%s ", collectionString[i]);
printf("\n");
printf("排序后 ");
BubbleSort(collectionString, collectionString[0], count, false);
for (i = 0; i < count; i++) printf("%s ", collectionString[i]);
printf("\n\n");
printf("使用降序排序浮点数组\n");
double arrayDouble[] = {-0.5, -0.9, 13.7, 10.4, -20.6};
count = sizeof(arrayDouble) / sizeof(double);
printf("排序前 ");
for (i = 0; i < count; i++) printf("%.1f ", arrayDouble[i]);
printf("\n");
printf("排序后 ");
BubbleSort(arrayDouble, arrayDouble[0], count, false);
for (i = 0; i < count; i++) printf("%.1f ", arrayDouble[i]);
printf("\n\n");
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
// CString strHello;
// strHello.LoadString(IDS_HELLO);
// cout << (LPCTSTR)strHello << endl;
DemoSort();
}
return nRetCode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -