2011.04.06 10个数排序问题
来源:互联网 发布:淘宝店铺贷款平台 编辑:程序博客网 时间:2024/06/03 17:44
给定的10个数字排序,这是个最基本的问题了。
最基本的方法:
#include <iostream>
using namespace std;
int main()
{
int a[10] = {1,5,9,8,2,3,4,6,7,10};
for (int i=0; i<10; i++)
{
for (int j=i+1; j<10; j++)
{
if (a[j] < a[i])
{
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
for (int i=0; i<10; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
输出:1 2 3 4 5 6 7 8 9 10
这个是最基本的方法,利用指针作比较,就是传说中的冒泡法。
后来看stl,发现用vector真的很简单,毕竟人家已经把算法封装好了。
并且,用stl的时候还不用担心,溢出问题,容器自动增长。
初试stl:
#include <iostream>
using namespace std;
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
int a[10] = {1,5,9,2,4,6,8,3,7,10};
vector<int> vectorInt(a, a+10);
sort(vectorInt.begin(), vectorInt.end());
vector<int>::iterator vectorIt;
for (vectorIt=vectorInt.begin(); vectorIt!=vectorInt.end(); ++vectorIt)
{
cout<<*vectorIt<<" ";
}
cout<<endl;
return 0;
}
输出:1 2 3 4 5 6 7 8 9 10
放在这里留着自己看,以后面试题可能会在此碰见。
高手看见,千万别笑话我的菜啊~~
(vs05下编译通过)
- 2011.04.06 10个数排序问题
- day3 排序问题(10个数)
- 对10个数进行排序
- 对10个数进行排序
- 对10个数进行排序
- 对10个数进行排序
- C++对10个数选择排序的2个问题分享
- 对随机的10个数进行排序
- 用起泡法对10个数排序。
- 冒泡对10个数排序,函数调用。
- 选择对10个数排序,函数调用。
- 【程序37】对10个数进行排序
- 编程珠玑笔记~排序10^7个数
- VB随机生成10个数并排序
- 起泡法对10个数排序
- 冒泡排序(随机生成10个数排序)
- 随意生成10个数并排序(选择排序法)
- 随意生成10个数并排序(冒泡排序法)
- MVC模式
- MVC模式
- 4.2.1 加载和解析数据
- sed使用(zl)
- ereg_repace.
- 2011.04.06 10个数排序问题
- inside source/destination和outside source/destination区别详解
- C语言缺陷与陷阱(笔记)
- android 屏幕旋转 重新调用onCreate的解决方法
- ExtJs中的xtype
- 关于一个优秀的Jquery插件
- C语言缺陷与陷阱(笔记)
- setup factory使用方法
- $.weeboxs打开窗口属性设置