-3

I am attempting to bubble sort the values in the array "A" using c++, but get error saying stack around variable A is corrupted?

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int A[] = {5,7,8,2,4,3};

    for (int i = 1; i <= 7 - 1; i++)
    {
        for (int j = 7; j >= i + 1; j--)
        {
            if (A[j] < A[j - 1])
            {
                swap(A[j], A[j - 1]);
            }
        }
    }
}
Rishav
  • 3,818
  • 1
  • 31
  • 49
  • Why are you using `<=7`? The number of elements in the array is 6. –  Mar 03 '18 at 17:09
  • 4
    You do know that array indexes are *zero* based? – Some programmer dude Mar 03 '18 at 17:09
  • 4
    Aside from that, please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Mar 03 '18 at 17:10
  • An array with six elements is indexed from 0 to 5, not 1 to 7. – molbdnilo Mar 03 '18 at 17:22
  • friends don't let friends use bubble sorts. – ddyer Mar 03 '18 at 23:32
  • @ddyer: I'd +1 you but I'm afraid you're talking over OP's head. – einpoklum Mar 03 '18 at 23:33

2 Answers2

2

I am attempting to ... sort the values in the array "A" ... using C++

Here, I'll sort them for you:

2, 3, 4, 5, 7, 8

(sigh) phew, that was some hard work! But at least now you don't need to bother with using C++.



What, not good enough? Oh, you really want to do it with C++? Fine... here you go:

#include <array>
#include <algorithm>

int main()
{
    auto A = make_array(5,7,8,2,4,3);
    std::sort(std::begin(A), std::end(A));
}

The make_array function is taken from here; you also have std::experimental::make_array(), but that's not standardized yet.

Note this won't use bubble-sorting; but then - why would you want to bubble-sort? It's quite inefficient as the array size increase... you might want to check out this comparison of sort algorithms (with neat animations too).

einpoklum
  • 118,144
  • 57
  • 340
  • 684
2

You don't need #include string since you are not using any string. Your initialization & conditions are wrong. Here is code that works:

My code:

    #include <iostream>

    using namespace std;

    int main()
    {
       int A[] = {5, 7, 8, 2, 4, 3};
       int j = 1;
       int tmp;

       for(int i = 0; i < 6; i++){
          for(j = 0; j < 6-i-1; j++){
              if(A[j] > A[j + 1]){
                  tmp = A[j];
                  A[j] = A[j + 1];
                  A[j + 1] = tmp;
              }

          }
       }

       cout << "The sorted data in order: " << endl;
       for(int i = 0; i < 6; i++){
          cout << A[i] << endl;
       }


      return 0;
    }
HTownGirl17
  • 365
  • 1
  • 4
  • 9