Multi Dimensional Array Selection Sort

Hello, I am trying to do a selection sort of a multidimensional array based on the 2nd column in descending order. But after a number is sorted and moved in the column, so must it's accompanying row. The size is known, as shown. This is the function I am working with. It currently does not work. Any tips?
EX)
Before:
2475
6386
8654
9945
1705
After:
9945
1705
8654
2475
6386


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void selection(int fun[5][4])
{
    int index = 0;
    int temp;
    int rows = 5;
    int column = 4;
    int indexbig;

        for (int j = column; j > 0; j--)
        {
            indexbig = 0;

            for (int i = 0; i < rows; i++)
            {

                if (fun[i][1] < fun[indexbig][1])
                {
                    indexbig = i;
                }
            }

    if (j != indexbig)
    {
        temp = fun[j][1];
        fun[j][1]=fun[indexbig][1];
        fun[indexbig][1]=temp;
    }

        }

    display(fun);//display array
}
Last edited on
did you want the rows to stay the same?
if so, your swap isnt doing that. You might try a memcpy-tmp, memmove (one to the other), memcpy(tmp to spot).

However, without that, it should order just the one column. Does it do that?
My full code with other function calls commented out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>

using namespace std;

int fun [5][4]; // global array

int binsearch(int fun[5][4])
{
    int high = 4, low = 0, mid;
    int input = 0;
    bool found = false;
    cout << "Please input a value to be searched for " << endl;
    cin >> input;

    while (high >= low && !found)
    {
        mid = (high+low) / 2;

        if (input > fun[4][mid])
        {
            low = mid + 1;
        }
        else if (input < fun[4][mid])
        {
            high = mid - 1;
        }
        else
        {
            found = true;
        }
   }
   return found;

}




void display (int fun[5][4])
{
    for (int col = 0; col < 5; col++)
  {
    for (int row = 0; row < 4; row++)
    {
        cout << fun[col][row] << " ";
    }
    cout << endl;
  }
}


void bubble(int fun[5][4], int counter)
{
    int index = 0;
    int temp;
    int x = 0;

    while (x < 4)
    {

      if (fun[x][0] > fun[x+1][0])
      {

        while (index <= 3)
        {
                temp = fun[x][index];
                fun[x][index] = fun[x+1][index];
                fun[x+1][index] = temp;
                index++;
        }

      }

      x++;
      index = 0;//reset index
    }

    if (counter > 0)
    {
        cout << "Bubble Sort:" << endl;
        display(fun);//display array
    }

}


void selection(int fun[5][4])
{
    int index = 0;
    int temp;
    int rows = 5;
    int column = 4;
    int indexbig;

        for (int j = column; j > 0; j--)
        {
            indexbig = 0;

            for (int i = 0; i < rows; i++)
            {

                if (fun[i][1] < fun[indexbig][1])
                {
                    indexbig = i;
                }
            }

    if (j != indexbig)
    {
        temp = fun[j][1];
        fun[j][1]=fun[indexbig][1];
        fun[indexbig][1]=temp;
    }

        }
    cout << "Selection Sort:" << endl;
    display(fun);//display array
}


void insertion(int fun[5][4])
{
    int index = 0;
    int temp;
    int x = 0;

    while (x < 3)
    {

      if (fun[4][x] > fun[4][x+1])
      {

          while(index < 5)
          {

                temp = fun[index][x];
                fun[index][x] = fun[index][x+1];
                fun[index][x+1] = temp;

            index++;

          }

        }
        index = 0;//reset index

    x++;
}
    cout << "Insertion Sort:" << endl;
    display(fun);//display array

    if (binsearch(fun))//begin binary search
        cout << "found" << endl;
    else
        cout << "Not Found" << endl;
}




int main()
{

    int meh = 0;

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 4; j++)
            {
            fun[i][j] = (rand()%10);//store random values in array
            }

    }
        display(fun); //display original array.

    while (meh < 2)
        {
            //bubble(fun,meh); //bubble sort function
            meh++;
        }

    selection(fun);

    //insertion(fun);


return 0;
}
Last edited on
OK, what is it NOT doing? Your example output is sorted descending by column 2.

Last edited on
Use std::copy to swap the rows according to the value of the second column:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <algorithm>

int main()
{
   int ar[5][4] = {{2, 4, 7, 5}, {6, 3, 8, 6}, {8, 6, 5, 4}, {9, 9, 4, 5}, {1, 7, 0, 5}};

   int temp[4]{};

   for (size_t i = 0; i < 5; ++i)
   {
       for (size_t j = i + 1; j < 5; ++j)
       {
           if (ar[i][1] < ar[j][1])
           {
               std::copy(std::begin(ar[i]), std::end(ar[i]), std::begin(temp));
               std::copy(std::begin(ar[j]), std::end(ar[j]), std::begin(ar[i]));
               std::copy(std::begin(temp), std::end(temp), std::begin(ar[j]));
           }
        }
    }
    for (size_t i = 0; i < 5; ++i)
    {
        for (size_t j = 0; j < 4; ++j)
        {
            std::cout << ar[i][j] << " ";
        }
        std::cout << "\n";
    }
}
Use std::copy to swap the rows according to the value of the second column:

That works, but there's a std::swap_ranges() which does this with no extra memory.
http://en.cppreference.com/w/cpp/algorithm/swap_ranges

std::swap_ranges(std::begin(ar[i]), std::end(ar[i]), std::begin(ar[j]))
Last edited on
There also is the overload for std::swap() which swaps arrays of swappable types.
In practice, it calls std::swap_ranges() after deducing the size.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <utility>

template < typename T, std::size_t NROWS, std::size_t NCOLS >
void sort_on_col( T (&array) [NROWS][NCOLS], std::size_t col )
{
    // if( col >= NCOLS ) throw std::out_of_range ...

    for( std::size_t i = 0 ; i < NROWS ; ++i )
    {
        for( std::size_t j = i+1 ; j < NROWS ; ++j )
        {
            if( array[j][col] < array[i][col] )
            {
                using std::swap ;
                // http://en.cppreference.com/w/cpp/algorithm/swap overload (2)
                swap( array[i], array[j] ) ;

                // could also use std::iter_swap #include <algorithm>
                // which in turn calls unqualified swap on the pointed to arrays
                // http://en.cppreference.com/w/cpp/algorithm/iter_swap
                // std::iter_swap( array+i, array+j ) ;
            }
        }
    }
}

http://rextester.com/LQST3675
Did it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

#include <iostream>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>

using namespace std;

int fun [5][4]; // global array

int binsearch(int bin[5][4])
{
    int high = 4, low = 0, mid;
    int input = 0;
    bool found = false;
    cout << "Please input a value to be searched for " << endl;
    cin >> input;

    while (high >= low && !found)
    {
        mid = (high+low) / 2;

        if (input > bin[4][mid])
        {
            low = mid + 1;
        }
        else if (input < bin[4][mid])
        {
            high = mid - 1;
        }
        else
        {
            found = true;
        }
   }
   return found;

}


void display (int fun[5][4])
{
    for (int col = 0; col < 5; col++)
  {
    for (int row = 0; row < 4; row++)
    {
        cout << fun[col][row] << " ";
    }
    cout << endl;
  }
}


void bubble(int bub[5][4], int counter)
{
    int index = 0;
    int temp;
    int x = 0;

    while (x < 4)
    {

      if (bub[x][0] > bub[x+1][0])
      {

        while (index <= 3)
        {
            temp = bub[x][index];
            bub[x][index] = bub[x+1][index];
            bub[x+1][index] = temp;
            index++;
        }

      }

      x++;
      index = 0;//reset index
    }

    if (counter > 0)
    {
        cout << "Bubble Sort:" << endl;
        display(bub);//display array
    }

}


void selection(int sel[5][4])
{
    int counter = 0;
    int temp;
    int rows = 5;
    int column = 4;
    int indexbig;

        for (int j = column; j > 0; j--)
        {
            indexbig = 0;

            for (int i = 0; i <= j; i++)
            {
                if (sel[i][1] < sel[indexbig][1])
                {
                    indexbig = i;
                }
            }

                if (j != indexbig)
                {
                    while(counter < 4)
                    {
                        temp = sel[j][counter];
                        sel[j][counter]=sel[indexbig][counter];
                        sel[indexbig][counter]=temp;
                        counter++;
                    }

                }

        }
    cout << "Selection Sort:"<< endl;
    display(sel);//display array
}


void insertion(int inser[5][4])
{
    int index = 0;
    int temp;
    int x = 0;

    while (x < 3)
    {

      if (inser[4][x] > inser[4][x+1])
      {

          while(index < 5)
          {

                temp = inser[index][x];
                inser[index][x] = inser[index][x+1];
                inser[index][x+1] = temp;

            index++;

          }

        }
        index = 0;//reset index

    x++;
}
    cout << "Insertion Sort:" << endl;
    display(inser);//display array

    if (binsearch(inser))//begin binary search
        cout << "Found" << endl;
    else
        cout << "Not Found" << endl;
}




int main()
{

    int meh = 0;

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 4; j++)
            {
            fun[i][j] = (rand()%10);//store random values in array
            }

    }
        cout << "Orignal array" << endl;
        display(fun); //display original array.

    while (meh < 2)
        {
            bubble(fun,meh); //bubble sort function
            meh++;
        }

    selection(fun);

    insertion(fun);


return 0;
}



Last edited on
Topic archived. No new replies allowed.