0

I'm trying to implement the bubble sorting algorithm to an array of integers, the function which sorts the array takes an array as a parameter and suppose to return the sorted array.

Here is the code:

#include <iostream>

using namespace std;
int* BubbleSort(int data[]){

for(int i=0; i<sizeof(data)/sizeof(data[0])-1; i++){
    for(int j=0; j<sizeof(data)/sizeof(data[0])-1-i; j++){
         if(data[j+1]>data[j]){
            int temp = data[j+1];
            data[j+1]=data[j];
            data[j]=temp;
        }
    }
}
return data;
}

int main()
{
    int data[]={8,4,9,7,6,5,13,11,10};
    int *a=BubbleSort(data);
    cout<<"{";
    for(int i=0; i<sizeof(data)/sizeof(data[0]); i++){
        cout<<a[i];
        if(i==sizeof(data)/sizeof(data[0])-1){
        cout<<"}"<<endl;
        }else{
        cout<<",";
        }
    }

   return 0;
}

The output I'm getting: {8,4,9,7,6,5,13,11,10}

5
  • 2
    Turn your warnings on. I got warning: sizeof on array function parameter will return size of 'int *' instead of 'int []' [-Wsizeof-array-argument]. Commented Mar 3, 2018 at 18:57
  • Ok how to fix this? Commented Mar 3, 2018 at 18:58
  • 3
    You need to pass in the length of the array as a parameter to the function. Or do the "modern" thing and use a container like std::array or std::vector Commented Mar 3, 2018 at 18:59
  • Handy extra reading on why this happened: What is array decaying? Commented Mar 3, 2018 at 19:21
  • sizeof() returns the size of something in bytes, so it should never be used to get the size of an array. Also, when an array is passed to a function, it's not copied. So you don't need to return the sorted array. How did you learn to do stuff like return the pointer? Commented Mar 3, 2018 at 19:34

2 Answers 2

5

You must pass in the size of the array because an array it decays to the pointer to its first element (element 0).

void BubbleSort(int data[], int size){

    for(int i(0); i != size; ++i){
         for(int j(i + 1); j != size; ++j){
             if(data[i] > data[j]){
                  int temp = data[i];
                  data[i] = data[j];
                  data[j] = temp;
          }
     }    
}
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe too late, but maybe in the future it will be useful,

Try to use this code


...

/**
* Sort array of integers with Bubble Sort Algorithm
*
* @param arr   Array, which we should sort using this function
* @param arrSZ The size of the array
* @param order In which order array should be sort
*
* @return Sorted array of integers
*/
void bubbleSortInt(double arr[], int arrSz, string order = "ascending")
{
    for (int i = 0; i < arrSz; ++i)
    {
        for (int j = 0; j < (arrSz - i - 1); ++j)
        {
            // Swapping process
            if ((order == "descending") ? arr[j] < arr[j + 1] : arr[j] > arr[j + 1])
            {
                swap(arr[j], arr[j + 1]);
            }
        }
    }
    return; // Optional because it's a void function
}// end bubbleSortInt

...

Then use it in your main function like below,


...

double integers[array_size];
const int array_size = 10;
string order = "ascending";
bubbleSortInt(integers, array_size, order)
for (int i = 0; i < array_size; ++i)
{
    cout << integers[i] << "\t";
}

...

Have a detailed look at the bubble sort algorithm -> https://github.com/teamroyalcoder/algorithms#bubble-sort-algorithm


This is a GitHub repo where you will find a bunch of algorithms with full details and source code written in c++ https://github.com/teamroyalcoder/algorithms

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.