【问题标题】:Trying to Understand whats wrong with my bubble Sort Method试图了解我的冒泡排序方法出了什么问题
【发布时间】:2013-04-13 16:59:29
【问题描述】:

我不确定为什么我的代码不起作用。它们都打印相同的值而不是排序它们。 输出没有改变,数组似乎仍然没有排序。有什么想法可以解决这个问题吗?我想知道为什么。

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

【问题讨论】:

  • 显示代码如何调用BubbleSort函数?
  • 即使假设你让它工作,你的冒泡排序仍然有一个主要问题:它是一个冒泡排序。
  • 这是一个家庭作业。我会发布主文件。

标签: c++ sorting bubble-sort


【解决方案1】:

试试这个..基本上每次迭代后你需要运行内部循环比外部变量少一个...主要是因为它已经排序了

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


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2017-03-02
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多