简体   繁体   English

C++中的冒泡排序算法

[英]Bubble Sorting Algorithm in C++

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.我正在尝试将冒泡排序算法实现为整数数组,对数组进行排序的 function 将数组作为参数并假设返回排序后的数组。

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}我得到的 output:{8,4,9,7,6,5,13,11,10}

You must pass in the size of the array because an array it decays to the pointer to its first element (element 0). 您必须传递数组的大小,因为它decays到指向其第一个元素(元素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;
          }
     }    
}

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,然后在你的主 function 中使用它,如下所示,


...

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详细看一下冒泡排序算法-> 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这是一个 GitHub 存储库,您将在其中找到一堆算法,其中包含完整的详细信息和用c++ https://github.com/teamroyalcoder/algorithms编写的源代码

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

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