冒泡排序、选择排序和插入排序
Bubble sort, Selection sort, and insertion sort
https://i.stack.imgur.com/QvWSG.jpg
这是显示结果的链接。
这个结果就是我的C++结果必须像一样
如该结果所示,有气泡排序、选择排序和插入排序、
并且每种处理都显示在黑屏上。
例如,(Bubble Sort)
20 10 40 30
20 10 30 40
10 20 30 40。
我必须使用void displayPtrArray
来显示这样的内容。
#include <iostream>
#include <string>
#include <array>
#include <iomanip>
using namespace std;
void reset(int array[], const int size);
void displayIntArray();
void displayPtrArray(const int array[], int size);
void BubbleSort(int array[], int size);
void SelectionSort(int array[], int size);
void InsertionSort(int a[], int size);
const int RR = 4;
int main()
{
int arr[RR];
reset(arr, RR);
}
void reset(int array[], const int size)
{
cout << "The originial array has been reset to:" << endl;
int array[RR] = { 20, 40, 10, 30 };
for (int n = 0; n < size; n++)
{
cout << setw(5) << array[n];
}
cout << endl << endl;
}
void displayPtrArray(const int array[], int size)
{
}
void displayIntArray()
{
}
void BubbleSort(int array[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void selectionSort(int array[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
void InsertionSort(int a[], int size)
{
for (int i = 1; i<size; i++)
{
int j;
int current = a[i];
for (j = i - 1; j >= 0 && a[j]> current; j--)
{
a[j + 1] = a[j];
}
a[j + 1] = current;
}
}
我对每种分拣都使用该功能,但如果这是错误的,请告诉我。
教我如何用显示每种类型的过程
void displayPtrArray
我真的不知道。。。。。T_T。。。。。。。。。。。
请帮忙!!
您已经拥有了displayPtrArray所需的确切代码!重置中定义的for
循环应该执行此操作。
从预期的输出来看,每次数组发生变化时都会显示它。对于冒泡排序,数组在进行交换时(在if语句的末尾)会发生变化,因此您需要在swap = true
之后的行中添加对displayPtrArray的调用。
对于选择排序,数组在外部For循环的末尾发生变化,因此应该在array[startScan] = minValue;
之后的行中添加对displayPtrArray的调用。
对于插入排序,数组在内部For循环的每次迭代中都会发生变化,在外部For循环的最后一行也会发生变化。因此,您可能必须在这两个位置调用displayPtrArray。
注意:如果您不熟悉语法,可以这样调用函数:displayPtrArray(array, size);
,其中array
是数组变量的名称(在冒泡排序和选择排序中,名称为array
;在插入排序中,为a
),size
是数组的大小(在所有三个排序函数中都一致命名)。
实际上,查看排序过程相当简单。您只需要在排序过程中输出即可。您不需要为此调用另一个函数。试试这个
void BubbleSort(int array[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
cout << "nThe array is ";
for ( int i = 0; i < ( size - 1 ); i++ )
cout << array[i] << "t"; // Look here
cout << "nThe value of temp is " << temp << endl; // and here
}
} while (swap);
}
- 这是插入排序的正确实现吗?
- 使用列表 STL 递归进行插入排序
- 列表 iter 不取消引用 使用列表进行插入排序
- 如何使插入排序更快?
- 插入排序的最小交换
- 如何通过保持第一个插入的位置来对列表中插入的其余元素进行排序?
- std::为插入和排序设置不同的比较器
- 数组将排序排序为降序而不是升序
- 使用按插入方式排序的参数创建哈希映射
- 如何在 C++ 中插入和排序具有唯一条目的对象向量
- 是否有插入然后排序的替代方法
- 排序 std::set 使用 operator() 对插入进行排序
- 用于插入大部分排序数据的数据结构,这些数据将保持排序顺序
- 与排序相关的算法(将每个项目替换为排序排序中的索引)
- "std::"具有排序/排序、存在测试和头/尾访问的数据结构?
- 插入阵列排序方法
- 哪一种方法是更快的向量(插入后排序)或集合
- c++标准库是否有按插入顺序排序的集合
- 使用快速排序排序不会给出排序的数组
- 将元素插入已排序的循环双链表中