Search code examples
c++bubble-sort

Bubble Sorting an Array


I'm writing a program to delete the lowest number from an array. I believe I'm almost to the point where I can solve the problem, however, I'm stuck on the last part (or so I think).

Example input and output:

Input: 1 2 3 4 5 7 10 Output: 1 2 3 4 5 7 10

Delete: 2 3 4 5 7 10 Delete: 3 4 5 7 10 Delete 4 5 7 10

And so on...

#include <iostream>
#include "queue.h"
#define SIZE 5
using namespace std;

class PriorityQueue
{
  int top, bottom;
  int numbers[SIZE];
public:
  PriorityQueue ()
  {
    top = bottom = -1;
  }

  void insert (int);
  void remove ();
  void display ();

  ~PriorityQueue ()
  {
  }
};

void PriorityQueue :: insert (int num)
{
  if (bottom == (SIZE -1) )
    cout << "Overflow queue is full" << endl;
  else if (bottom == -1)
    top = bottom = 0;
  else if (bottom < (SIZE -1) )
    bottom ++;
  numbers [bottom] = num;
}

void PriorityQueue :: remove ()
{
  for (int i=1; i <SIZE; i++)
  {
    for (int j=0; j < SIZE - i; j++)
    {
      if(top[j] > top[j +1])
      {
        SIZE temp = top[j];
        top[j] = top[j + 1];
        top[j + 1] = temp;
      }
    }
  }
  /*if ((top >= 0) && (top < SIZE))
    {
      cout << "\n Element removed = " << numbers[top];
      top++;
    }
  else
    cout << "Queue is empty";*/
}

void PriorityQueue :: display ()
{
  for (int i = top; i <= bottom; i++)
    cout << numbers [i] << ' ';
}

/*void bubbleSort(int numbers[], int n)
{
    for (int i=1; i <SIZE; i++)
    {
        for (int j=0; j < SIZE - i; j++)
        {
            if(list[j] > list[j +1])
            {
                elemType temp = list[j];
                list[j] = list[j + 1];
                list[j + 1] = temp;
            }
        }
    }
}*/

int main ()
{
  PriorityQueue pq;
  int n, choose;
  while (1)
    {
      cout << " 1. Insert" <<endl;
      cout << " 2. Remove" <<endl;
      cout << " 3. Display" <<endl;
      cout << " 4. Exit " <<endl;
      cin >> choose;

      switch (choose)
    {
    case 1 : cout << "\n Enter element to push ";
      cin >> n;
      cout << endl;
      pq.insert (n);
      break;
    case 2 :
      pq.remove ();
      break;
    case 3 :
      pq.display ();
      cout <<endl;
      break;
    case 4 :
      return (0);
    }
    }
  return 0;
}

By sorting the array by the number's value

void PriorityQueue :: remove ()
{
  for (int i=1; i <SIZE; i++)
  {
    for (int j=0; j < SIZE - i; j++)
    {
      if(top[j] > top[j +1])
      {
        SIZE temp = top[j];
        top[j] = top[j + 1];
        top[j + 1] = temp;
      }
    }
  }
  /*if ((top >= 0) && (top < SIZE))
    {
      cout << "\n Element removed = " << numbers[top];
      top++;
    }
  else
    cout << "Queue is empty";*/
}

Solution

  • #include <iostream>
    using namespace std;
    #define SIZE 5
    class PriorityQueue
    {
    int numbers[SIZE];
    int bottom;
    public:
    PriorityQueue():bottom(SIZE-1){};
    void bubbleSort();
    void insert(int num);
    void remove();
    void display();
    };
    
    void PriorityQueue :: bubbleSort () {
        //Sort
        for (int i = SIZE-1; i > bottom; --i) {
            for (int j = bottom ; j < SIZE - 2; j++) {
              if(numbers[j] > numbers[j + 1])
              {
                int temp = numbers[j];
                numbers[j] = numbers[j + 1];
                numbers[j + 1] = temp;
              }
            }
        }
    }
    
    void PriorityQueue :: remove() {
        bubbleSort();
        if(bottom < SIZE-1) ++bottom;
    }
    
    void PriorityQueue :: insert (int num) {
        if (bottom < 0 ) {
            cout << "Queue is full" << endl;
            }
        else {
            --bottom;
            numbers [bottom] = num;
            cout<<"Insert. new value of bottom  index : " << bottom << "\n";
            }
    }
    void PriorityQueue::display(){
        if(bottom == SIZE-1){ cout<<"Nothing to display.\n";}
        else{
            for(int i = SIZE-2; i >= bottom; --i) {
                cout << numbers[i] << ((i!=bottom)?"::":"");
                }
        cout<<"\n";
    }
    }
    
    int main(){
        PriorityQueue pq;
        pq.insert(2);
        pq.insert(5);
        pq.insert(1);
        pq.insert(3);
        pq.insert(9);
        pq.display();
        pq.bubbleSort();
        pq.display();
        pq.remove();
        pq.display();
        pq.remove();
        pq.remove();    
        pq.remove();    
        pq.remove();
        pq.display();
        pq.remove();
        pq.display();
        pq.insert(2);
        pq.display();
        return 0;
    }
    

    output:

    Insert. new value of bottom  index : 3
    Insert. new value of bottom  index : 2
    Insert. new value of bottom  index : 1
    Insert. new value of bottom  index : 0
    Insert. new value of bottom  index : -1
    2::5::1::3::9
    9::5::3::2::1
    9::5::3::2
    Nothing to display.
    Nothing to display.
    Insert. new value of bottom  index : 3
    2