0
#include <stdio.h>
int main(){
    int array[] ={4, 5, 3, 1,6,2,6,10,22,10,10};
    for(int i = 0; i <= 10; i++){
        for (int j = 0; j <= 9; j++){
            printf("Array a[i] is %d\n", array[i]);
            if(array[i] < array[j]){
                // Swap
                int temp;
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
            printf("Array a[i,j] is %d %d\n", array[i],array[j]);
        }
    }
    for (int i = 0; i <= 10; i++){
        printf("%d", array[i]);
    }
}

Could anyone check whether this sorting algorithm correct or not, because it seems to me that it does work. If it is correct, could you tell the name of this sorting algorithm please? Thank you!

I have tried to compile this code with different given output and it seems to me that it work just fine!

3
  • 1
    Looks like bubble sort. Commented Dec 1, 2022 at 22:42
  • @PM77-1 I don't think so, bubble sort usually only swaps adjacent elements. This is more like the slowest version of insertion sort possible. Commented Dec 1, 2022 at 22:50
  • @AceGambit - You are right. Commented Dec 1, 2022 at 22:52

1 Answer 1

2

This looks to be a correct, effective, but inefficient version of insertion sort. Typically with insertion sort, the inner loop won't start at 0 every time, it'll start at i + 1 since you know everything before index i should already be sorted.

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

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.