冒泡排序数组

Bubble Sorting an Array

本文关键字:数组 冒泡排序      更新时间:2023-10-16

我正在编写一个程序,从数组中删除最小的数字。我相信我几乎到了可以解决问题的地步,然而,我被困在了最后一部分(或者我是这么认为的)。

示例输入和输出:

输入:1 2 3 4 5 7 10输出:1 2 3 4 5 7 10

删除:2 3 4 5 7 10删除:3 4 5 7 10删除4 5 7 10

等等…

#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;
}

通过按数值对数组进行排序

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";*/
}
#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;
}

输出:

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