Search code examples
c++algorithmbubble-sort

Trying to not swap with bubble sort if values are the same


i have my program working fine except for one minor problem that i can't seem to figure out. At the beginning, i want the user to input a sorting method, then enter values into an array of pointers until the user ends input with ^D. After the user ends input, my program prints the user input from highest to lowest based on the desired sort method. Again, i have everything working and sorting does happen, but what i want to do is print based on order of user input if two values of the sorting methods are the same. Here is my code, if any additional clarification is needed, just ask.

main.cpp

5 #include<iostream>
6 using namespace std;
7
8 #include "video.h"
9
10 #include<string>
11
12 int main()
13 {
14  const int MAX = 100; // maximum number of things stored in the array
15  Video *vids[MAX]; // Video pointer for Video objects
16  int num_vids = 0; // counter for number of videos
17
18  string title, url, comment; // for title, url, and comment respectively
19  float length; // for length
20  int rating; // for rating
21
22  string sort; // for sorting method
23
24  cout << "How would you like to sort these videos, by rating, length, or title? " <<     endl;
25  cin >> sort;
26  cin.ignore();
27
28  if( (sort != "length")  && (sort != "rating") && (sort != "title") )
29  {
30   cerr << sort << " is not a legal sorting method, giving up." << endl;
31   return 1;
32  }
33
34  cout << "Enter a Title: " << endl;
35  while(getline(cin,title))
36  {
37   cout << "Enter the URL: " << endl;
38   getline(cin,url);
39
40   cout <<  "Enter a comment: " << endl;
41   getline(cin,comment);
42
43   cout << "Enter the length: " << endl;
44   cin >> length;
45
46   cout << "Enter a rating of 1-5: " << endl;
47   cin >> rating;
48   cin.ignore();
49
50   vids[num_vids] = new Video(title,url,comment,length,rating);
51   num_vids++;
52
53   cout << "Enter a Title: " << endl;
54  }
55   cout << endl;
56
57  if( (num_vids > MAX) )
58  {
59   cerr << "Too many video entries, giving up." << endl;
60   return 1;
61  }
62
63  if( (sort == "length") )
64  {
65   for(int last = num_vids-1; last > 0; last--)
66    for(int cur = 0; cur < last; cur++)
67     if( (vids[cur]->longer(vids[cur+1]) == false) )
68      swap(vids[cur], vids[cur+1]);
69  }
70
71  else if( (sort == "rating") )
72  {
73   for(int last = num_vids-1; last > 0; last--)
74    for(int cur = 0; cur < last; cur++)
75     if( (vids[cur]->largerRating(vids[cur+1]) == false) )
76    // if( (vids[cur] != vids[cur+1]) )
77       swap(vids[cur], vids[cur+1]);
78  }
79  else if( (sort == "title") )
80  {
81   for(int last = num_vids-1; last > 0; last--)
82    for(int cur = 0; cur < last; cur++)
83     if( (vids[cur]->alphabetical(vids[cur+1]) == false) )
84      swap(vids[cur], vids[cur+1]);
85  }
86
87
88  for(int i = 0; i < num_vids; i++)
89  {
90   vids[i]->print();
91  }
92  return 0;
93  }

video.cpp

 5 #include<iostream>
 6 using namespace std;
 7
 8 #include<string>
 9
 10 #include "video.h"
 11
 12 using namespace std;
 13
 14  Video::Video(string title, string url, string comment, float length, int rating)
 15  {
 16   m_title = title;
 17   m_url = url;
 18   m_comment = comment;
 19   m_rating = rating;
 20   m_length = length;
 21  }
 22
 23  Video::~Video()
 24  {
 25   cout << "object is desructing" << endl;
 26  }
 27
 28  bool Video::longer(Video *other)
 29  {
 30   return m_length > other->m_length;
 31   if( (other->m_length > m_length) )
 32   {
 33    return true;
 34   }else
 35   {
 36    return false;
 37   };
 38  }
 39
 40  bool Video::largerRating(Video *other)
 41  {
 42   return m_rating > other->m_rating;
 43   if( (other->m_rating > m_length) )
 44   {
 45    return true;
 46   }
 47   else
 48   {
 49   return false;
 50   };
 51  }
 52
 53  bool Video::alphabetical(Video *other)
 54  {
 55   return other->m_title > m_title;
 56   if( (other->m_title > m_title) )
 57   {
 58    return true;
 59   }else
 60   {
 61    return false;
 62   };
 63  }
 64
 65  void Video::print()
 66  {
 67   cout << m_title << ", " << m_url << ", " << m_comment << ", " << m_length << ", ";
 68   for(int i = 0; i < m_rating; i++)
 69  {
 70   cout << "*";
 71  }
 72  cout << endl;
 73  }
 74

video.h

5 #ifndef VIDEO_H
6 #define VIDEO_H
7
8 #include<iostream>
9 using namespace std;
10
11 #include<string>
12
13 class Video
14 {
15  public:
16   Video(string title, string url, string comment,float length, int rating);
17   ~Video();
18   bool longer(Video *other);
19   bool largerRating(Video *other);
20   bool alphabetical(Video *other);
21   void print();
22
23  private:
24   string m_title;
25   string m_url;
26   string m_comment;
27   float m_length;
28   int m_rating;
29 };
30
31 #endif

Again, the majority of it is working, but i dont want my program to swap in bubble sort if two values of the desired sort method are the same, i have tried things such as:

71  else if( (sort == "rating") )
72  {
73   for(int last = num_vids-1; last > 0; last--)
74    for(int cur = 0; cur < last; cur++)
75     if( (vids[cur]->largerRating(vids[cur+1]) == false) )
76      if( (vids[cur] != vids[cur+1]) )
77       swap(vids[cur], vids[cur+1]);
78  }

But still, no luck. Any input would be greatly appreicated.


Solution

  • Just change your swap condition like this:

    for(int last = num_vids-1; last > 0; last--) {
        for(int cur = 0; cur < last; cur++) {
            if(vids[cur+1]->largerRating(vids[cur]) ) {
                swap(vids[cur], vids[cur+1]);
            }
        }
    }
    

    Please note, that it is entirely superfluous to compare a boolean value to a boolean value, so instead of writing conditions like this:

    if(condition == true) {
    if(negated == false) {
    

    you should just write

    if(condition) {
    if(!negated) {
    

    That's what bools are for, after all.