Sorting and Searching Arrays

I was given a code and was asked to explain certain things, but I find it rather confusing.

So here's the code:

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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <iostream>
#include <fstream>
#include <cstdlib>




using std::cin; 
using std::cout; 
using std::endl; 
using std::fstream;
using std::ios;
using std::string;




const int MAX_SCORES = 100;
int read (string filename, int finalExams[], int maxSize); 
void write (string filename, ios::openmode mode, string description, const int finalExams[], int numberOfExams);
int descendingBubbleSortV1 (int finalExams[], int numberOfExams);
int descendingBubbleSortV2 (int finalExams[], int numberOfExams);
int descendingBubbleSortV3 (int finalExams[], int numberOfExams);
int unorderedLinearSearch (int searchValue, const int finalExams[], int numberOfExams);
int binarySearch (int searchValue, const int finalExams[], int numberOfExams);



int main()
{

    int finalExams[MAX_SCORES];
    
    int numberOfExams = read ("dataInLab.txt", finalExams, MAX_SCORES); 

    cout << "number of compares: " << descendingBubbleSortV1(finalExams, numberOfExams) << endl;
         
    write ("outputInLab.txt", ios::out|ios::app, "post V1 sort", finalExams, numberOfExams);


    write ("outputInLab.txt", ios::out, "scores as read", finalExams, numberOfExams);
    
    numberOfExams = read ("dataInLab.txt", finalExams, MAX_SCORES);
    cout << "number of compares: " 
         << descendingBubbleSortV2(finalExams, numberOfExams) << endl;

    write ("outputInLab.txt", ios::out|ios::app, "post V2 sort", finalExams,                     numberOfExams);
    
    numberOfExams = read ("dataInLab.txt", finalExams, MAX_SCORES);
    cout << "number of compares: " 
         << descendingBubbleSortV3(finalExams, numberOfExams) << endl;

    write ("outputInLab.txt", ios::out|ios::app, "post V3 sort", finalExams,                     numberOfExams);
    
    numberOfExams = read("dataInLab.txt", finalExams, MAX_SCORES);




   int searchScore;

   cout << "Enter score for searching: ";
   cin >> searchScore;

   int location = unorderedLinearSearch (searchScore, finalExams, numberOfExams);

   cout << "unordered linear search results for " << searchScore << endl;

   if (location >= 0) 
   {
      cout << "score " << searchScore << " found in array location ["
           << location << "]\n";
   }

   else 
     cout << "score " << searchScore << " not found by libear search\n";
     
   descendingBubbleSortV3 (finalExams, numberOfExams);

   for(int i = 0; i<numberOfExams; i++){
       cout << finalExams[i] << endl;
   }
   
   location = binarySearch (searchScore, finalExams, numberOfExams);
   
   cout << "binary search results for " << searchScore << endl;
   
   if (location >= 0)
   {
      cout << "score " << searchScore << " found in array location [" 
           << location << "]\n";
   }
   else
      cout << "score " << searchScore << " not found by binary search\n";

   return 0;
} 


int read (string filename, int finalExams[], int maxSize){ 

    fstream infile (filename.c_str(),ios::in);
    if (!infile){ 
    
       cout << "Unable to open " << filename << " file. Exiting...\n"; exit(1); 
    }


    int numberOfExams = 0; 

    while (numberOfExams < maxSize && infile >> finalExams[numberOfExams]) { 
    
          numberOfExams++; 
    } 

    infile.close (); 
    
    return numberOfExams; 
} 

void write (string filename, ios::openmode mode, string description, const int finalExams[], int numberOfExams) { 

   fstream outfile (filename.c_str(), mode); 
   
   outfile << description << endl; 

   for (int i = 0; i <= numberOfExams-1; i++){
   
       outfile << i << " " << finalExams[i] << "\n";
   } 
   
   outfile << "\n"; outfile.close ();  
}

int descendingBubbleSortV1 (int finalExams[], int numberOfExams) {

    int compares = 0;
    
    for (int pair = 0; pair <= numberOfExams - 2; pair++) {
        
        compares++;
    
        if (finalExams[pair] < finalExams[pair + 1]) {
        
           int temp             = finalExams[pair];
           finalExams[pair]     = finalExams[pair + 1];
           finalExams[pair + 1] = temp;
        }
    }
    
    return compares;
}

int descendingBubbleSortV2 (int finalExams[], int numberOfExams)
{
   int compares = 0;

   for (int sink = 0; sink <= numberOfExams -2; sink++) 
   {
    for (int pair = 0; pair <= numberOfExams - 2; pair++) 
    {
       compares++;
    
       if (finalExams[pair] < finalExams[pair + 1]) 
       {
           int temp = finalExams[pair];
           finalExams[pair] = finalExams[pair + 1];
           finalExams[pair + 1] = temp;
       }

    }

   }
   
   return compares;
}

int descendingBubbleSortV3 (int finalExams[], int numberOfExams)
{
   int sink     = 0;
   int compares = 0;
   bool swapOccured;

do {
   swapOccured = false;
   
   for (int pair = 0; pair <= numberOfExams - 2 - sink; pair++) 
   {
        compares++;
       
        if (finalExams[pair] < finalExams[pair + 1]) 
        { 
            swapOccured = true;
            int temp = finalExams[pair];
            finalExams[pair] = finalExams[pair + 1];
            finalExams[pair + 1] = temp;
        }
   }
    sink++;

   }while (swapOccured);

 return compares;
}

int unorderedLinearSearch (int searchValue, const int finalExams[], int numberOfExams)
{
    int location = -1;
    bool found = false;
    
    for (int score = 0; score <= numberOfExams - 1 && ! found; score++) 
    {
       if (searchValue == finalExams[score]) 
       {
       location = score;
       found = true;
       }

    }
    return location;
}

int binarySearch (int searchValue, const int finalExams[], int numberOfExams)
{
    int mid;
    int left = 0;
    int right  = numberOfExams - 1;
    int location = -1;
    bool found = false;
    
    while (left < right && ! found)
    {
        mid = (left + right) / 2;
      
        if (searchValue > finalExams[mid])
           right = mid - 1;
        else if (searchValue<finalExams[mid])
           left = mid + 1;
        else 
           found = true;
    }
      
    if (found) 
    {
        location = mid;
    }
    
    
    
    return location;
}


First question: Note that the exam scores are read from the file into the array again so that this version of the sort starts under the same conditions. Execute the program and verify that the number of comparisons has indeed dropped. And demonstrate it (explain why)

Second question: Execute the program and verify that the number of comparisons has dropped further still. Demonstrate your finished in-lab program.

Third question: Execute the program and verify that the search works correctly for scores in the data and not in the data. Repeat the search for 80 and note its location reported by the program. Demonstrate one successful search and one unsuccessful search. Explain why the program reports a different location for the exam score 80.
Topic archived. No new replies allowed.