2

I'm trying to create a sorting algorithm, it contains a nested loop which compares each element of the array to all other elements in the array, and if an element is greater in value than any of its succeeding elements, they switch places with each other. But for some reason my program won't output anything and exits with code 0, i.e. success.

Following is my code:

#include <iostream>
using namespace std;

void sortAlgo(int *a, int n){
    int tmp;
    for (int i=0; i<=n-1; i++){
        for(int j=i+1; j<=n; j++){ //O(n^2)
            if(a[i]>a[j]){
                //LHS variable assumes RHS quantity
                tmp=a[i]; //a[i] value stored in temp variable
                a[i]=a[j]; //shifts a[j] value to a[i]
                a[j]=tmp; //a[j] takes value of a[i]
            }
        }
    }
    for(int x=0; x<=n; x++){
                cout<<a[x]<<" ";
            }
}

int main(){
    int arr[10]={1,2,3,5,23,12,4};
    sortAlgo(arr, 7);
}

I am using VS Code.

8
  • Is there a reason you're writing your own sort? Commented Oct 28, 2022 at 21:22
  • This is the second recent question about bubble sort that got the algorithm wrong. It's the simplest sort in existence, and it's obvious even from its Wikipedia page that this is using the wrong loop condition. No insult to OP, but if I ever meet the person writing these tutorials I'm going to have some fairly direct feedback for them. Commented Oct 28, 2022 at 21:25
  • Any time you're using <= in a loop condition you are probably going out of bounds. Like for(int x; x<=n; x++) and for(int j=i+1; j<=n; j++). for (int i=0; i<=n-1; i++) could be rewritten as for (int i=0; i < n; i++). Commented Oct 28, 2022 at 21:26
  • Anyway, to the point: run it in a debugger. Step through your code and see what is happening. Add print statements if you must. And enable all your compiler warnings. Commented Oct 28, 2022 at 21:26
  • 3
    In for(int x; x<=n; x++), what's the initial value of x? It's undefined, so it's random garbage at runtime (likely not zero), so the code doesn't output anything. Commented Oct 28, 2022 at 21:26

1 Answer 1

2

It is not the bubble sort algorithm You are trying to implement the selection sort algorithm with redundant swaps.

These for loops

for(int j=i+1; j<=n; j++){ //O(n^2)

and

for(int x; x<=n; x++){

have invalid conditions that in general can result in undefined behavior if the passed array will have exactly n elements because the expression a[n] will access memory beyond the array..

Moreover in the second for loop the variable x was not initialized that again invokes undefined behavior.

Pay attention to that instead of to swap "manually" two elements

tmp=a[i]; //a[i] value stored in temp variable
a[i]=a[j]; //shifts a[j] value to a[i]
a[j]=tmp; //a[j] takes value of a[i]

you could use standard C++ function std::swap declared in the header <utility>.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I'm sorry for the fundamental errors I've made in both asking and implementation. This place schools you quite occasionally and I guess that's how one learns.

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.