试图了解我的泡沫排序方法有什么问题
Trying to Understand whats wrong with my bubble Sort Method
我不知道为什么我的代码不能工作。它们都输出相同的值,而不是对它们进行排序。输出没有改变,数组看起来仍然没有排序。有什么办法可以解决这个问题吗?我想知道为什么。
void BSort::BubbleSort()
{ int temp =0;
for(int index = 0; index < sizes; index++){
for(int index2 = 0; index2 < sizes-1; index2++ ){
if(Array1[index2] > Array1[index2+1]){
temp = Array1[index2];
Array1[index2] = Array1[index2+1];
Array1[index2+1] = temp; }
}
}
}
/*************************************************************************/
//--------------------------- BubbleSort2( ) --------------------------//
/*************************************************************************/
void BSort::BubbleSort2()
{
int temp =0;
for(int index = 0; index < sizes-1; index++){
for(int index2 = sizes-1; index2 > index; index2--){
if( Array2[index2] < Array2[index2-1]){
temp = Array2[index2];
Array2[index2] = Array2[index2-1];
Array2[index2-1] = temp;
}
}
}
}
/*************************************************************************/
//----------------------------- CombSort( ) ---------------------------//
/*************************************************************************/
void BSort::CombSort() {
int temp = 0;
int tempsize =sizes;
int index2;
while((tempsize = int(tempsize/1.3)) >1){
for(int index = sizes-1; index >= tempsize; index--){
index2 = index-tempsize;
if(Array3[index] < Array3[index2]){
temp = Array3[index];
Array3[index] = Array3[index2];
Array3[index2] = temp;
}
}
bool testcase = true;
for(int in = 0; in < sizes; in++){
for(int in2 =sizes-1,testcase = false; in2 > in; in2--){
if(Array3[in2] < Array3[in2-1])
{ temp = Array3[in2];
Array3[in2] = Array3[in2-1];
Array3[in2-1] = temp;
testcase = true;
}
}
}
}
}
主文件#include<iostream>
#include<cstdlib>
#include "BSort.h"
using namespace std;
int main(void)
{
int a[] = {-2, 88, 6, -1, 10, 15, 3, 12, -11, 9, 33, 21, 4, 7, 45, 55, 62, 18, 0, 20};
BSort S(a,20);
cout << endl << " Display Array elements before sorting";
S.DisplayListElements();
// Calling bubble sort
S.BubbleSort();
S.BubbleSort2();
S.CombSort();
cout << endl << endl << " Display Array elements Increasing order After BubbleSort";
S.DisplayListElements();
system("pause");
return 0;
}
试试这个…基本上在每次迭代之后,你需要运行的内循环比外部变量少一个…主要是因为它已经排序了
void BSort::BubbleSort()
{ int temp =0;
for(int index = 0; index < sizes; index++){
for(int index2 = 0; index2 < sizes-index; index2++ ){
if(Array1[index2] > Array1[index2+1]){
temp = Array1[index2];
Array1[index2] = Array1[index2+1];
Array1[index2+1] = temp; }
}
}
}
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在 c++ 中拥有一组结构的正确方法是什么?
- 通过JNI传递数据数组的最快方法是什么
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 在另一个类视图中添加最多2个图表的正确方法是什么
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 在C++中包含原型文件的正确方法是什么?
- 在 OpenCV C++ 中估计基本矩阵之前对相应点进行归一化的正确方法是什么?
- 在PostgreSQL中根据它们的ID选择大量行的最快方法是什么?
- 在OSX上使用CMake将Adobe的XMP工具包构建为共享库的最简单方法是什么?