Sorting Arrays

I am trying to use a method other than the bubble sort to sort my data. I have looked on the Tutorials and Wikipedia, but I am still unsure how to implement it into my program, which takes two, two dimensional arrays. The second function toward the bottom void YearD Line 174 is the place I attempted the selection sort, but very clearly, I don't understand how to do it. Any help?

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <conio.h>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;

void pause();
void TitleA(string [][2],int [][2], int , int );
void MediaA(string [][2], int [][2], int , int );
enum columns {Title=0, Media=1};
enum columns2 {Year=0, Length=1};

int main()
{
    string movies[200][2];
    int numbers[200][2];

    string temp;
    int index=0;
    int maxElements=0;
    int choice;
    int sub=0;
    fstream dataFile;

    cout<<"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"<<endl
        <<"X                                                                              X"<<endl
        <<"X                                                                              X"<<endl
        <<"X                    How would you like to sort your data?                     X"<<endl
        <<"X                                                                              X"<<endl
        <<"X                    1. Sort by Title (ascending)                              X"<<endl
        <<"X                    2. Sort by Year Released (descending)                     X"<<endl
        <<"X                    3. Sort by Media Type (ascending)                         X"<<endl
        <<"X                    4. Sort by Length (descending)                            X"<<endl
        <<"X                                                                              X"<<endl
        <<"X                                                                              X"<<endl
        <<"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"<<endl;






    cin>>choice;
    system("CLS");
    dataFile.open("F:\\SPRECHTCOSC1437\\InClassActivities\\DataFiles\\Movies.dat",ios::in);
    if(!dataFile.is_open())
    {
        cout<<"Unable to Process"<<endl;
        return 999;
    }
    while(!dataFile.eof() && index<200)
    {
        getline(dataFile, movies[index][Title],',');
        getline(dataFile, temp, ',');
        numbers[index][Year] = atoi(temp.c_str());
        getline(dataFile, temp, ',');
        numbers[index][Length] = atoi(temp.c_str());
        getline(dataFile, movies[index][Media],'\n');


        index++;
    }
    maxElements=index;
    dataFile.close();
    if(choice==1)
    {
        cout<<setw(40)<<"Andy's Data"<<endl;
        cout<<left<<setw(40)<<"Title"
            <<setw(8)<<"Media"
            <<setw(17)<<"Year Released"
            <<setw(13)<<"Length (min.)"<<endl;


        cout<<setfill('-')
            <<setw(80)<<'-'<<endl;
        cout<<setfill(' ');

        TitleA( movies , numbers, maxElements ,sub );

        for(int index=0; index<maxElements; index++)
        {
            cout<<setw(40)<<movies[index][Title]
                <<setw(8)<<movies[index][Media]
                <<setw(17)<<numbers[index][Year]
                <<setw(13)<<numbers[index][Length]<<endl;
        }
        pause();
    }
    else
        if(choice==3)
        {
             cout<<setw(40)<<"Andy's Data"<<endl;
             cout<<left<<setw(8)<<"Media"
                       <<setw(40)<<"Title"
                       <<setw(17)<<"Year Released"
                       <<setw(13)<<"Length (min.)"<<endl;


        cout<<setfill('-')
            <<setw(80)<<'-'<<endl;
        cout<<setfill(' ');

        MediaA( movies, numbers ,maxElements ,sub );

        for(int index=0; index<maxElements; index++)
        {
            cout<<setw(8)<<movies[index][Media]
                <<setw(40)<<movies[index][Title]
                <<setw(17)<<numbers[index][Year]
                <<setw(13)<<numbers[index][Length]<<endl;
        }
        pause();
        }




}
//*************************
void pause()
{
     char ch;

     printf("\nPress ENTER to continue..");
     do
     {
          ch = getch();
     }while(ch != 13);

     printf("\n\n");

     return;
}
//*****************************
 void TitleA(string  movies[][2],int numbers[][2], int maxElements, int sub )
{
    int limit = maxElements;
    bool swap = true;
        while(swap)
        {
            swap=false;
            for(int sub=0; sub < limit-1; sub ++)
            {
                if(movies[sub][Title] > movies[sub+1][Title])
                {
                    string temp = movies[sub][Title];
                    movies[sub][Title] = movies[sub+1][Title];
                    movies[sub+1][Title] = temp;

                    temp = movies[sub][Media];
                    movies[sub][Media] = movies[sub+1][Media];
                    movies[sub+1][Media] = temp;

                    int temp2 = numbers[sub][Year];
                    numbers[sub][Year]=numbers[sub+1][Year];
                    numbers[sub+1][Year]=temp2;

                    temp2 = numbers[sub][Length];
                    numbers[sub][Length]=numbers[sub+1][Length];
                    numbers[sub+1][Length]=temp2;
                    swap=true;

                }

            }
            limit--;
        }
    return;
}
//*****************************************
/*void YearD(string movies[][2], int numbers[][2], int sub)
{
    int startScan, minIndex, minValue;
    for(startScan = 0; startScan < (sub - 1); startScan++)
    {
        minIndex = startScan;
        minValue = numbers[startScan];
        for(int index = startScan + 1; index < sub; index ++)
        {
            if(numbers[index] < minValue)
            {
                minValue = numbers[index];
                minIndex = index;
            }
        }
        numbers[minIndex] = numbers[startScan];
        numbers[startScan] = minValue;
    }
}*/
//*************************************
 void MediaA(string  movies[][2],int numbers[][2], int maxElements, int sub )
{

    int limit = maxElements;
    bool swap = true;
        while(swap)
        {
            swap=false;
            for(int sub=0; sub < limit-1; sub ++)
            {
                if(movies[sub][Media] > movies[sub+1][Media])
                {
                    string temp = movies[sub][Media];
                    movies[sub][Media] = movies[sub+1][Media];
                    movies[sub+1][Media] = temp;

                    temp = movies[sub][Title];
                    movies[sub][Title] = movies[sub+1][Title];
                    movies[sub+1][Title] = temp;

                    int temp2 = numbers[sub][Year];
                    numbers[sub][Year]=numbers[sub+1][Year];
                    numbers[sub+1][Year]=temp2;

                    temp2 = numbers[sub][Length];
                    numbers[sub][Length]=numbers[sub+1][Length];
                    numbers[sub+1][Length]=temp2;
                    swap=true;

                }

            }
            limit--;
        }
    return;
}

Last edited on
I've updated the function a little.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void YearD(string movies[][2], int numbers[][2], int sub)
{
    int startScan, minIndex, minValue;
    for(startScan = 0; startScan < (sub - 1); startScan++)
    {
        minIndex = startScan;
        minValue = numbers[sub][startScan];
        for(int index = startScan + 1; index < sub; index ++)
        {
            if(numbers[index][Year] < minValue)
            {
                minValue = numbers[index][Year];
                minIndex = index;
            }
        }
        numbers[minIndex][Year] = numbers[sub][startScan];
        numbers[sub][startScan] = minValue;
    }
}
Topic archived. No new replies allowed.