Requesting a review

Mar 9, 2013 at 12:50am
Hello everyone,
This is my second year of learning c++ in college. To be honest, all I would like is a review of this code.

My professor prompted us to create a program that would time various sorting algorithms. I can see that under the conditions of this program, a selection sort is much faster.

Further questions:

-Are there ever circumstances in real-world implementation of these algorithms where a bubble sort would be faster?

-Is there a way to increase the accuracy of my timer functions?

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
#include <iostream>
#include <cstdlib>//for srand, rand() and RAND_MAX
#include <time.h>//for clock_t, clock(), CLOCKS_PER_SECOND

using namespace std;

class Sorting{
public:
    Sorting();
    void Generate();//creates the random numbers to be used for sorting
    void Display();//
    void BubbleSort();
    void SelectiveSort();
    void StartTimer();
    void StopTimer();
private:
    double dataStore[10000];//holds the random numbers
    double numElements;
    clock_t start;
    clock_t stop;
    double elapsed;
};
//======================================================================
Sorting::Sorting(){
    numElements=10000;
    start=0;
    stop=0;
    elapsed=0;
}
//======================================================================
void Sorting::Generate(){
srand((unsigned)time(NULL));//seed the random numbers (unsigned for speed!)
double range;
  for (unsigned i = 0; i < numElements; i++) {
    range = (double)rand() / (double)RAND_MAX;
      dataStore[i] = range;
  }
}
//======================================================================
void Sorting::Display(){
    for(int i=0;i<numElements;++i){
        cout<<dataStore[i]<<endl;
        }
    cout<<endl<<endl<<endl;
}
//======================================================================
void Sorting::StartTimer(){
    start=clock();
}
//======================================================================
void Sorting::StopTimer(){
    stop=clock();
    elapsed = ((double) (stop - start) * 1000) / CLOCKS_PER_SEC;//MILLISECONDS!
    cout<<"ELAPSED TIME: "<<elapsed<<endl;
    start=0;
    stop=0;
    elapsed=0;
}
//======================================================================
void Sorting::BubbleSort(){
double store;
    for (unsigned passes = 0;  passes < numElements - 1;  passes++){
        for (unsigned i = 0;  i < numElements - passes - 1;  i++){
            if (dataStore[i] > dataStore[i+1]){
                store = dataStore[i];
                dataStore[i] = dataStore[i+1];
                dataStore[i+1]=store;
            }
        }
    }
}
//======================================================================
void Sorting::SelectiveSort(){
double store;
    for (unsigned passes = 0;  passes < numElements - 1;  passes++){
        int lowest = passes;
        for(unsigned j=passes+1; j<numElements; j++){
            if(dataStore[lowest] > dataStore[j] ){
                lowest = j;
            }
        }
        store = dataStore[lowest];
        dataStore[lowest] = dataStore[passes];
        dataStore[passes] = store;
    }
}
//======================================================================
int main()
{
    cout << "LETS SORT SOME STUFF!" << endl;
    Sorting numbers;
    numbers.Generate();
    //numbers.Display();
        numbers.StartTimer();
    numbers.BubbleSort();
    //numbers.SelectiveSort();
        numbers.StopTimer();
    return 0;
}
Last edited on Mar 9, 2013 at 1:02am
Mar 9, 2013 at 1:24am
A typical way to get more accurate timer value is by

1. repeating the task to be timed n times, measuring,
2. Measuring the entire processing time of step 1,
3. And finally dividing the result by n.
Mar 9, 2013 at 1:33am
Thank you! That makes perfect sense. All I was doing before was increasing the size of my array.

I did noticed that the results were inconsistent for each time the program was run, but I did not think to run the same sort function multiple times.

I will add a Clear() to my class to clear the array, re-generate the numbers and sort them multiple times instead of just once.

edit: I don't need a clear function, I can just call my generate() multiple times.

Here is what I did with respect to your suggestion (I hope I didn't misinterpret 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
#include <iostream>
#include <cstdlib>//for srand, rand() and RAND_MAX
#include <time.h>//for clock_t, clock(), CLOCKS_PER_SECOND

using namespace std;

class Sorting{
public:
    Sorting();
    void Generate();//creates the random numbers to be used for sorting
    void Display();
    void BubbleSort();
    void SelectiveSort();
    void StartTimer();
    void StopTimer(int);
private:
    double dataStore[1000];//holds the random numbers
    double numElements;
    clock_t start;
    clock_t stop;
    double elapsed;
};
//======================================================================
Sorting::Sorting(){
    numElements=1000;
    start=0;
    stop=0;
    elapsed=0;
}
//======================================================================
void Sorting::Generate(){
srand((unsigned)time(NULL));//seed the random numbers (unsigned for speed!)
double range;
  for (unsigned i = 0; i < numElements; i++) {
    range = (double)rand() / (double)RAND_MAX;
      dataStore[i] = range;
  }
}
//======================================================================
void Sorting::Display(){
    for(int i=0;i<numElements;++i){
        cout<<dataStore[i]<<endl;
        }
    cout<<endl<<endl<<endl;
}
//======================================================================
void Sorting::StartTimer(){
    start=clock();
}
//======================================================================
void Sorting::StopTimer(int n){
    stop=clock();
    elapsed = ((double) (stop - start) * 1000) / CLOCKS_PER_SEC;//MILLISECONDS!
        cout<<"ELAPSED TIME: "<<elapsed/n<<endl;//elapsed is divided by n(the number of times the function is called)
    start=0;
    stop=0;
    elapsed=0;
}
//======================================================================
void Sorting::BubbleSort(){
double store;
    for (unsigned passes = 0;  passes < numElements - 1;  passes++){
        for (unsigned i = 0;  i < numElements - passes - 1;  i++){
            if (dataStore[i] > dataStore[i+1]){
                store = dataStore[i];
                dataStore[i] = dataStore[i+1];
                dataStore[i+1]=store;
            }
        }
    }
}
//======================================================================
void Sorting::SelectiveSort(){
double store;
    for (unsigned passes = 0;  passes < numElements - 1;  passes++){
        int lowest = passes;
        for(unsigned j=passes+1; j<numElements; j++){
            if(dataStore[lowest] > dataStore[j] ){
                lowest = j;
            }
        }
        store = dataStore[lowest];
        dataStore[lowest] = dataStore[passes];
        dataStore[passes] = store;
    }
}
//======================================================================

int main()
{
    unsigned n=100;//arbitrary number to control the number of times a sorting function is called
    char choice;
    Sorting numbers;
        cout << "LETS SORT SOME STUFF!" << endl;
        cout<<"       "<<"Press 1 if you want to time Selection Sort"<<endl<<"       "<<"Press 2 if you would like to time Bubble Sort"<<endl;
        cout<<"       "<<"Press q to quit"<<endl;
        while (cin>>choice){
            if(choice=='q'&&'Q'){
                break;
            }
            if(choice=='1'){
                numbers.StartTimer();
                for(unsigned i=0;i<n;i++){
                    numbers.Generate();
                    numbers.SelectiveSort();
                }
                numbers.StopTimer(n);
            }
            else if(choice=='2'){
                numbers.StartTimer();
                for(unsigned i=0;i<n;i++){
                    numbers.Generate();
                    numbers.BubbleSort();
                }
                numbers.StopTimer(n);
            }

        }
    return 0;
}


I simply modified StopTimer() to take an int and used that int to compute the average time of the multiple sort function calls.
Last edited on Mar 9, 2013 at 4:04am
Topic archived. No new replies allowed.