05-13 07:28
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[C/C++] Example / Ordering - Descending Algorithms 본문

Programming/C&C++

[C/C++] Example / Ordering - Descending Algorithms

cinema4dr12 2014. 6. 12. 10:39

//
//  main.cpp
//  Test-003
//
//  Created by gchoi on 2014. 5. 8..
//  Copyright (c) 2014년 gchoi. All rights reserved.
//

#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{
    int num[] = {88, 67, 77, 93, 23, 51, 32, 33, 76, 52};
    int nElem = sizeof(num)/sizeof(int);
    int myMin;
    int tmp, dummy;

    tmp = 0;
   
    for(int i = 0 ; i < nElem - 1 ; i++) {
        myMin = num[i];
       
        tmp = 0;
        for(int j = i+1 ; j < nElem ; j++) {
            // find the minimum
            if(myMin > num[j]) {
                myMin = num[j];
                tmp = j;
            }
        }
       
        if(tmp != 0) {
            dummy = num[i];
            num[i] = num[tmp];
            num[tmp] = dummy;
        }
       
    }
   
    for(int i = 0 ; i < nElem ; i++) {
        cout << *(num + i) << endl;
    }

    return 0;
}

'Programming > C&C++' 카테고리의 다른 글

[C/C++] Example / Reading a File  (0) 2014.06.12
[C/C++] Example / Writing a File  (0) 2014.06.12
[C/C++] Example / Derived Class  (0) 2014.06.12
[C/C++] Example / Class Constructor  (0) 2014.06.12
[C/C++] Example / Class  (0) 2014.06.12
Comments