简体   繁体   English

c ++冒泡排序与基数排序

[英]c++ Bubble Sort vs Radix Sort

I have been working on this program for a project. 我一直在为一个项目开发此程序。 Everything runs fine but there is something wrong with my bubble sort. 一切运行正常,但我的气泡排序有问题。 I ran and displayed the result of the function but every so often it shows negative numbers, which it should NOT. 我运行并显示了函数的结果,但是每隔很多次它就会显示负数,但不应这样做。 And also every so often it does not sort properly, meaning it the my bubble sort function is not sorting them in order some what. 而且,它经常也没有正确排序,这意味着我的气泡排序功能没有对它们进行排序。

#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;

int const temp = 10000;

void bubbleSort ( int array [ temp ] );  
void radixSort ( int * array, int arraySize );
void display ( int btime);

int main ( )
{
    int A1;
    int array [ temp ];
    char a = '\0';

    cout << "\nWould You Like To Perform Bubble and Radix Test? (y/n): ";
    cin >> a;

    while ( a == 'y' || a == 'Y' )
    {
        srand ( ( unsigned ) time ( 0 ) );

        for ( size_t i = 0; i < temp; i++ )
        {
            A1 = ( rand ( ) % temp ) + 1 ;           
            array [ i ] = A1;            
            bubbleSort ( array );
        }
    }

    while ( a == 'n' || a == 'N' )
    {
        break;
    }

    return 0;
}

void bubbleSort ( int array [ temp ] )
{
   for ( int i = 0; i < temp; i++ )
    {
        for ( int j = 0; j < temp - 1; j++ )
        {
            if ( array [ j ] > array [ j + 1 ] )
            {
                int temp = array [ j ];
                array [ j ] = array [ j + 1 ];
                array [ j + 1 ] = temp;

                //Test: to see if its working properly.
                cout << array [ j + 1 ] << endl; //idk if this should be placed here
           }
        }
    }  
}

Problems in your code: 您的代码中的问题:

  1. Your while loop. 您的while循环。 You don't make any breaking condition in the loop. 您不会在循环中产生任何破坏条件。

Solution: Use you own preferred breaking condition. 解决方案:使用您自己喜欢的断裂条件。 Something like: 就像是:

while ( true )
    {
        cin >> a;
        if(a == 'y' || a == 'Y') {
            //your array init and bubblesrt method calling here
        } else {
           break;
        }
    }
  1. Calling bubbleSort with an uninitialized array in a for loop (!). for循环(!)中使用未初始化的数组调用bubbleSort

Solution: 解:

Put the bubbleSort method calling outside of for loop, bubbleSort方法调用到for循环之外,

for ( size_t i = 0; i < temp; i++ )
   {
      A1 = ( rand ( ) % temp ) + 1 ;           
      array [ i ] = A1;            
   }
bubbleSort ( array );

Following is the 'proof' of bubbleSort() method implementation which actually works perfectly: 以下是bubbleSort()方法实现的“证明”,它实际上运行良好:

#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;

int const TEMP = 4;

void bubbleSort ( int array [ TEMP ] );


int main ( )
{
    int array [ TEMP ] = {3,2,-1,0}; // a simple demonstration
    bubbleSort(array);

    for (int i = 0; i < TEMP; i++) {
        cout<< array[i];
    }
    return 0;
}

void bubbleSort(int array[TEMP]) {
int temp;
for (int i = 0; i < TEMP -1; i++) {
    for (int j = 0; j < TEMP -i - 1; j++) {
        if (array[j] > array[j + 1]) {
            temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
        }
    }
}

And the output is: 输出为:

-1023 -1023

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM