现在的位置: 首页 > 综合 > 正文

简单算法1

2018年08月31日 ⁄ 综合 ⁄ 共 1678字 ⁄ 字号 评论关闭

// 功能:选择排序 vc 2005 express

#include <iostream>
#include <vector>
using namespace std;

template<class T> void selectSort(vector<T> & t1)
{
    int i, k,j;
    T temp;
    for( i=0; i<t1.size();i++)
    {
        k = i;

        for(j=i; j<t1.size();j++)
        {
            if(t1[j]<t1[k])
                k = j; // 保证k拥有最小值的下标
        }
       
        temp = t1[i];
        t1[i] = t1[k];
        t1[k] = temp;
    }
}

int main()
{
    float farr[10] = {3.5, 6.8, 2.1, 1.0, 5.6, 8.9,11.0,23.2,4.3, 7.3};
    vector<float> vec1(farr, farr+10);
    selectSort( vec1 );
    for(int i=0; i<vec1.size(); i++)
    {
        cout << vec1[i] << " " ;
    }
    cout << endl;
     system("pause");
     
     return 0;

 }

/////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 顺序搜索,返回元素的位置
// ia,要搜索的数组,n数组的大小,x要搜索的值
int seqSearch(int ia[], const int n, const int x)
{
    int i=0;
    while( i<n && ia[i] !=x )
    {
        i++;
    }
    if( i==n )
      return -1;

    return i;
}

void main()
{
    int arr[6] = {2,6,3,9,8,7};
    cout << seqSearch(arr, 6, 9);
    int desit = 9;
    int * is_found = find(&arr[0],&arr[5],
        desit);
    cout << "element " << desit
        << "is" << (is_found ? "found" : "not found");

     system("pause");
}

/////////////////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

template<class T>  class szcl {
    T e;
public:
    szcl () { e=0; }
    szcl ( T value ) { e = value; }
    T get_value() { return e; }
};

int main() {
    int i;
    szcl<int> a1[3] = { 3,5,7 }/*对象数组*/, *elem;
    for ( i=0; i<3; i++ )
        cout << a1[i].get_value() << endl; //打印静态数组

    elem = a1;
    for ( i=0; i<3; i++ )
    {
    cout << elem->get_value() << endl; //打印动态数组
    elem++;
    }
    system("pause");
    return 0;
}

////////////////////////////////////////////////////////////////////////////////////////

 

抱歉!评论已关闭.