⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ѡ

📁 最近对排序算法的复习
💻
字号:
// 选择排序.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream.h"

/*选择排序的思想是:
*首先找到数据清单中的最小的数据,然后将这个数据同第一个数据交换位置
*接下来找第二小的数据,再将其同第二个数据交换位置,以此类推
*N个要排序就排序N-1次
*/
void Swap(int &x,int &y) {
	int temp = x;
	x = y;
	y = temp;
}

void select_px(int array[],int length) {
	for(int i=0;i<length;i++) {
		int m = i;		//用于保存最小值的数组位置
		for(int j=i;j<length;j++) {
			if(array[j]<array[m]) {
				m = j;
			}
		}
		if(i!=m)
			Swap(array[i],array[m]);
	}
}
int main(int argc, char* argv[])
{
	int array[] = {23,54,12,34,97,60,58,7,73,82};
	for(int i=0;i<10;i++) {
		cout<<array[i]<<" ";
	}
	cout<<endl;
	
	select_px(array,10);

	for(i=0;i<10;i++) {
		cout<<array[i]<<" ";
	}
	cout<<endl;
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -