2017-12-19 23 views
0

나는 처리해야 할 실제 문제가 있습니다. 정렬을 내림차순으로 정렬해야합니다 [4] [x].C++에서 한 열로 2 차원 배열을 정렬

{121,120,203,240} 
{0.5,0.2,3.2,1.4} 
{1.3,1.5,1.2,1.8} 
{3 ,2 ,5 ,4 } 

모든 값은 4 행으로 분류 보해야한다 : 예에서 내가 좋아하는 값을 얻을합니다. 따라서 다음과 같은 결과가 필요합니다.

{203,240,121,120} 
{3.2,1.4,0.5,0.2} 
{1.2,1.8,1.3,1.5} 
{5 ,4 ,3 ,2 } 

버블 정렬 방법으로 시도했지만 제대로 작동하지 않습니다.

+2

버블 정렬이 어떻게 제대로 작동하지 않습니까? 당신은 어떤 행동을 보았으며 그것이 기대와 다른 점은 무엇입니까? 네가 한 짓을 보여줘. 이상적으로는 [MCVE]를 제공하십시오. –

+0

@Mateusz Staniaszek이 {2, 3, 4, 5}는 ​​오름차순입니다. –

+0

오, 내 잘못, 나는 그것을 바로 잡아야한다. 나는 하강에 대해 생각했다. –

답변

0

다음과 같은 방법을 볼 수 있습니다 버블 정렬을 사용하여 배열을 정렬하는 간단한 방법은

#include <iostream> 
#include <iomanip> 
#include <utility> 

int main() 
{ 
    const size_t N = 4; 

    double a[][N] = 
    { 
     { 121, 120, 203, 240 }, 
     { 0.5, 0.2, 3.2, 1.4 }, 
     { 1.3, 1.5, 1.2, 1.8 }, 
     { 3, 2, 5, 4 } 
    }; 

    for (const auto &row : a) 
    { 
     for (double x : row) std::cout << std::setw(3) << x << ' '; 
     std::cout << '\n'; 
    } 
    std::cout << std::endl; 

    // The bubble sort 
    for (size_t n = N, last = N; not (n < 2); n = last) 
    { 
     for (size_t i = last = 1; i < n; i++) 
     { 
      if (a[N - 1][i - 1] < a[N - 1][i]) 
      { 
       for (size_t j = 0; j < N; j++) 
       { 
        std::swap(a[j][i - 1], a[j][i]); 
       } 
       last = i; 
      } 
     } 
    } 

    for (const auto &row : a) 
    { 
     for (double x : row) std::cout << std::setw(3) << x << ' '; 
     std::cout << '\n'; 
    } 
    std::cout << std::endl; 

    return 0; 
} 

프로그램 출력은 당신이 필요로하는 모든 거품 정렬의 코드를 추출하는 것입니다

121 120 203 240 
0.5 0.2 3.2 1.4 
1.3 1.5 1.2 1.8 
    3 2 5 4 

203 240 121 120 
3.2 1.4 0.5 0.2 
1.2 1.8 1.3 1.5 
    5 4 3 2 

입니다 main에서 추출한 다음 모든 2D 배열과 정렬 기준으로 사용되는 모든 행에 대해 별도의 함수로 다시 작성합니다.

+0

그다지 효율적이지 않습니다. 나는 쌍 (4 번째 행의 값, 인덱스) 배열을 소개하고, 정렬 된 쌍의 인덱스에 따라 배열을 재정렬한다. – Michael

+0

@Michael 임의의 크기의 배열을 만드는 것은 이미 비효율적입니다. –

0

병렬 벡터 대신에 병렬 값을 포함하는 구조를 사용하면 문제를 쉽게 풀 수 있습니다.

그런 구조로 돌아 가기는 쉽습니다. 분류 키와 색인을 포함하는 중간 벡터를 만들고 정렬하십시오.

색인을 정렬하면 올바른 순서로 모든 개별 벡터의 순서를 직접 바꿀 수 있습니다.

다음과 같이 할 것입니다. (Boost Unit Test에 입력했는데, 분명히해야합니다.)

#define BOOST_AUTO_TEST_MAIN 
#define BOOST_TEST_MODULE TestPenta 
#include <boost/test/auto_unit_test.hpp> 

#include <iostream> 
#include <vector> 

std::vector<int> v1 = {121,120,203,240}; 
std::vector<float> v2 = {0.5,0.2,3.2,1.4}; 
std::vector<float> v3 = {1.3,1.5,1.2,1.8}; 
std::vector<int> v4 = {3 ,2 ,5 ,4 }; 

std::vector<int> expected_v1 = {203,240,121,120}; 
std::vector<float> expected_v2 = {3.2,1.4,0.5,0.2}; 
std::vector<float> expected_v3 = {1.2,1.8,1.3,1.5}; 
std::vector<int> expected_v4 = {5 ,4 ,3 ,2 }; 

BOOST_AUTO_TEST_CASE(TestFailing) 
{ 
    // First create an index to sort containing sort key and initial position 
    std::vector<std::pair<int,int>> vindex{}; 
    int i = 0; 
    for (auto x: v4){ 
     vindex.push_back(std::pair<int,int>(x,i)); 
     ++i; 
    } 

    // Sort the index vector by key value 
    struct CmpIndex { 
     bool operator() (std::pair<int, int> & a, std::pair<int, int> & b) { 
      return a.first > b.first ; 
     } 
    } cmp; 

    std::sort(vindex.begin(), vindex.end(), cmp); 

    // Now reorder all the parallel vectors using index 
    // (of course in actual code we would write some loop if several vector are of the same type). 
    // I'm using parallel loops to avoid using too much memory for intermediate vectors 

    { 
     std::vector<int> r1; 
     for (auto & p: vindex){ 
      r1.push_back(v1[p.second]); 
     } 
     v1 = r1; 
    } 
    { 
     std::vector<float> r2; 
     for (auto & p: vindex){ 
      r2.push_back(v2[p.second]); 
     } 
     v2 = r2; 
    } 
    { 
     std::vector<float> r3; 
     for (auto & p: vindex){ 
      r3.push_back(v3[p.second]); 
     } 
     v3 = r3; 
    } 
    { 
     std::vector<int> r4; 
     for (auto & p: vindex){ 
      r4.push_back(v4[p.second]); 
     } 
     v4 = r4; 
    } 

    // Et voila! The vectors are all sorted as expected 
    i = 0; 
    for (int i = 0 ; i < 4 ; ++i){ 
     BOOST_CHECK_EQUAL(expected_v1[i], v1[i]); 
     BOOST_CHECK_EQUAL(expected_v2[i], v2[i]); 
     BOOST_CHECK_EQUAL(expected_v3[i], v3[i]); 
     BOOST_CHECK_EQUAL(expected_v4[i], v4[i]); 
     ++i; 
    } 
}