冒泡排序数组
Bubble Sorting an Array
我正在编写一个程序,从数组中删除最小的数字。我相信我几乎到了可以解决问题的地步,然而,我被困在了最后一部分(或者我是这么认为的)。
示例输入和输出:
输入: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
相关文章:
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 为什么指针在对二维数组进行排序时无法正常工作?
- 使用排序函数 c++ 对字符数组进行排序
- 使用 std::sort 对二维 c 数组进行排序
- 使用STL对用户输入数组进行排序的错误有什么解决方案吗?
- 对 0 、1 和 2 的数组进行排序
- C++按 2 列对多维数组进行排序
- 如何将数组传递到函数中,以便用户可以根据需要对"phrases"/数组进行排序
- 选择随机字符串数组的排序
- 如何在c++中对同时包含负数和正数的字符串数组进行排序
- 数组将排序排序为降序而不是升序
- 如何按降序对标准数组进行排序 - C++ 11.
- 在 C 中对 2 Dim 数组进行排序
- 通过 std::sort 对 C 2D 数组进行排序
- 如何使用CMP函数对矢量字符串数组进行排序
- 用于对可变数量的数组进行排序的可变参数模板
- 按字母顺序对结构数组进行排序
- 冒泡排序无法对C++中的对象的动态数组进行排序
- 使用冒泡排序对随机数量的动态数组进行排序