Insertion Sort is giving bizarre output [closed]









up vote
-6
down vote

favorite












I wrote a insertion sort program but it's not sorting the inputs correctly. Please help me find the error in the program.
If my input is 1 2 3 10 -1 -2 -3 then the output is



Value of a[0] is 1
Value of a[1] is 2
Value of a[2] is 3
Value of a[9] is -1
Value of a[10] is -2
Value of a[11] is -3
Value of a[19] is 10


I don't know if my logic is correct but I don't know where I did wrong please help me find the error in the program.



 #include<stdio.h>
void looper(int *);
void sort(int *,int *);
int main()

int a[25];
for(int i=0;i<=24;i++)

printf("Enter the value of a[%d] : ",i);
scanf("%d",&a[i]);

looper(a);
for(int i=0;i<=24;i++)

printf("Value of a[%d] is %dn",i,a[i]);


void looper(int *p)

for(int i=1;i<=24;i++)

for(int j=(i-1);j>=0;j--)

sort((p+i),(p+j));



void sort(int *a,int *b)

int tmp;
if((*a)<(*b))

tmp=*a;
*a=*b;
*b=tmp;











share|improve this question















closed as off-topic by dandan78, gsamaras, EJoshuaS, Pearly Spencer, Machavity Nov 10 at 16:35


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – dandan78, gsamaras, EJoshuaS, Pearly Spencer, Machavity
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 2




    Um.. maybe its because I've been awake for too long, but that looks an aweful lot like a non-optimized bubble sort; not insertion sort. Just saying. And your post should include your 25 numbers of input data. The expected result is somewhat obvious from that, so no need to specify that, but "it's not sorting the inputs correctly." tells a small part of the story; obviously it's not sorting them correctly or you wouldn't be here. What is it doing?
    – WhozCraig
    Nov 10 at 14:00







  • 1




    It is a bubble sort. And the loop conditions in looper are suspicious.
    – Nelfeal
    Nov 10 at 14:01










  • @Nelfeal can you please help me out with the code? What changes would you do?
    – Debojeet Das
    Nov 10 at 14:04







  • 1




    Actually I misread the sort call; it's not really a bubble sort. It's not really anything I know in fact. I think you meant to write sort(p+j+1, p+j).
    – Nelfeal
    Nov 10 at 14:13











  • @Nelfeal Sure I understood that reference but for insertion sort what should I do?
    – Debojeet Das
    Nov 10 at 14:25














up vote
-6
down vote

favorite












I wrote a insertion sort program but it's not sorting the inputs correctly. Please help me find the error in the program.
If my input is 1 2 3 10 -1 -2 -3 then the output is



Value of a[0] is 1
Value of a[1] is 2
Value of a[2] is 3
Value of a[9] is -1
Value of a[10] is -2
Value of a[11] is -3
Value of a[19] is 10


I don't know if my logic is correct but I don't know where I did wrong please help me find the error in the program.



 #include<stdio.h>
void looper(int *);
void sort(int *,int *);
int main()

int a[25];
for(int i=0;i<=24;i++)

printf("Enter the value of a[%d] : ",i);
scanf("%d",&a[i]);

looper(a);
for(int i=0;i<=24;i++)

printf("Value of a[%d] is %dn",i,a[i]);


void looper(int *p)

for(int i=1;i<=24;i++)

for(int j=(i-1);j>=0;j--)

sort((p+i),(p+j));



void sort(int *a,int *b)

int tmp;
if((*a)<(*b))

tmp=*a;
*a=*b;
*b=tmp;











share|improve this question















closed as off-topic by dandan78, gsamaras, EJoshuaS, Pearly Spencer, Machavity Nov 10 at 16:35


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – dandan78, gsamaras, EJoshuaS, Pearly Spencer, Machavity
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 2




    Um.. maybe its because I've been awake for too long, but that looks an aweful lot like a non-optimized bubble sort; not insertion sort. Just saying. And your post should include your 25 numbers of input data. The expected result is somewhat obvious from that, so no need to specify that, but "it's not sorting the inputs correctly." tells a small part of the story; obviously it's not sorting them correctly or you wouldn't be here. What is it doing?
    – WhozCraig
    Nov 10 at 14:00







  • 1




    It is a bubble sort. And the loop conditions in looper are suspicious.
    – Nelfeal
    Nov 10 at 14:01










  • @Nelfeal can you please help me out with the code? What changes would you do?
    – Debojeet Das
    Nov 10 at 14:04







  • 1




    Actually I misread the sort call; it's not really a bubble sort. It's not really anything I know in fact. I think you meant to write sort(p+j+1, p+j).
    – Nelfeal
    Nov 10 at 14:13











  • @Nelfeal Sure I understood that reference but for insertion sort what should I do?
    – Debojeet Das
    Nov 10 at 14:25












up vote
-6
down vote

favorite









up vote
-6
down vote

favorite











I wrote a insertion sort program but it's not sorting the inputs correctly. Please help me find the error in the program.
If my input is 1 2 3 10 -1 -2 -3 then the output is



Value of a[0] is 1
Value of a[1] is 2
Value of a[2] is 3
Value of a[9] is -1
Value of a[10] is -2
Value of a[11] is -3
Value of a[19] is 10


I don't know if my logic is correct but I don't know where I did wrong please help me find the error in the program.



 #include<stdio.h>
void looper(int *);
void sort(int *,int *);
int main()

int a[25];
for(int i=0;i<=24;i++)

printf("Enter the value of a[%d] : ",i);
scanf("%d",&a[i]);

looper(a);
for(int i=0;i<=24;i++)

printf("Value of a[%d] is %dn",i,a[i]);


void looper(int *p)

for(int i=1;i<=24;i++)

for(int j=(i-1);j>=0;j--)

sort((p+i),(p+j));



void sort(int *a,int *b)

int tmp;
if((*a)<(*b))

tmp=*a;
*a=*b;
*b=tmp;











share|improve this question















I wrote a insertion sort program but it's not sorting the inputs correctly. Please help me find the error in the program.
If my input is 1 2 3 10 -1 -2 -3 then the output is



Value of a[0] is 1
Value of a[1] is 2
Value of a[2] is 3
Value of a[9] is -1
Value of a[10] is -2
Value of a[11] is -3
Value of a[19] is 10


I don't know if my logic is correct but I don't know where I did wrong please help me find the error in the program.



 #include<stdio.h>
void looper(int *);
void sort(int *,int *);
int main()

int a[25];
for(int i=0;i<=24;i++)

printf("Enter the value of a[%d] : ",i);
scanf("%d",&a[i]);

looper(a);
for(int i=0;i<=24;i++)

printf("Value of a[%d] is %dn",i,a[i]);


void looper(int *p)

for(int i=1;i<=24;i++)

for(int j=(i-1);j>=0;j--)

sort((p+i),(p+j));



void sort(int *a,int *b)

int tmp;
if((*a)<(*b))

tmp=*a;
*a=*b;
*b=tmp;








c arrays sorting insertion-sort






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 15:34

























asked Nov 10 at 13:57









Debojeet Das

13




13




closed as off-topic by dandan78, gsamaras, EJoshuaS, Pearly Spencer, Machavity Nov 10 at 16:35


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – dandan78, gsamaras, EJoshuaS, Pearly Spencer, Machavity
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by dandan78, gsamaras, EJoshuaS, Pearly Spencer, Machavity Nov 10 at 16:35


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – dandan78, gsamaras, EJoshuaS, Pearly Spencer, Machavity
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 2




    Um.. maybe its because I've been awake for too long, but that looks an aweful lot like a non-optimized bubble sort; not insertion sort. Just saying. And your post should include your 25 numbers of input data. The expected result is somewhat obvious from that, so no need to specify that, but "it's not sorting the inputs correctly." tells a small part of the story; obviously it's not sorting them correctly or you wouldn't be here. What is it doing?
    – WhozCraig
    Nov 10 at 14:00







  • 1




    It is a bubble sort. And the loop conditions in looper are suspicious.
    – Nelfeal
    Nov 10 at 14:01










  • @Nelfeal can you please help me out with the code? What changes would you do?
    – Debojeet Das
    Nov 10 at 14:04







  • 1




    Actually I misread the sort call; it's not really a bubble sort. It's not really anything I know in fact. I think you meant to write sort(p+j+1, p+j).
    – Nelfeal
    Nov 10 at 14:13











  • @Nelfeal Sure I understood that reference but for insertion sort what should I do?
    – Debojeet Das
    Nov 10 at 14:25












  • 2




    Um.. maybe its because I've been awake for too long, but that looks an aweful lot like a non-optimized bubble sort; not insertion sort. Just saying. And your post should include your 25 numbers of input data. The expected result is somewhat obvious from that, so no need to specify that, but "it's not sorting the inputs correctly." tells a small part of the story; obviously it's not sorting them correctly or you wouldn't be here. What is it doing?
    – WhozCraig
    Nov 10 at 14:00







  • 1




    It is a bubble sort. And the loop conditions in looper are suspicious.
    – Nelfeal
    Nov 10 at 14:01










  • @Nelfeal can you please help me out with the code? What changes would you do?
    – Debojeet Das
    Nov 10 at 14:04







  • 1




    Actually I misread the sort call; it's not really a bubble sort. It's not really anything I know in fact. I think you meant to write sort(p+j+1, p+j).
    – Nelfeal
    Nov 10 at 14:13











  • @Nelfeal Sure I understood that reference but for insertion sort what should I do?
    – Debojeet Das
    Nov 10 at 14:25







2




2




Um.. maybe its because I've been awake for too long, but that looks an aweful lot like a non-optimized bubble sort; not insertion sort. Just saying. And your post should include your 25 numbers of input data. The expected result is somewhat obvious from that, so no need to specify that, but "it's not sorting the inputs correctly." tells a small part of the story; obviously it's not sorting them correctly or you wouldn't be here. What is it doing?
– WhozCraig
Nov 10 at 14:00





Um.. maybe its because I've been awake for too long, but that looks an aweful lot like a non-optimized bubble sort; not insertion sort. Just saying. And your post should include your 25 numbers of input data. The expected result is somewhat obvious from that, so no need to specify that, but "it's not sorting the inputs correctly." tells a small part of the story; obviously it's not sorting them correctly or you wouldn't be here. What is it doing?
– WhozCraig
Nov 10 at 14:00





1




1




It is a bubble sort. And the loop conditions in looper are suspicious.
– Nelfeal
Nov 10 at 14:01




It is a bubble sort. And the loop conditions in looper are suspicious.
– Nelfeal
Nov 10 at 14:01












@Nelfeal can you please help me out with the code? What changes would you do?
– Debojeet Das
Nov 10 at 14:04





@Nelfeal can you please help me out with the code? What changes would you do?
– Debojeet Das
Nov 10 at 14:04





1




1




Actually I misread the sort call; it's not really a bubble sort. It's not really anything I know in fact. I think you meant to write sort(p+j+1, p+j).
– Nelfeal
Nov 10 at 14:13





Actually I misread the sort call; it's not really a bubble sort. It's not really anything I know in fact. I think you meant to write sort(p+j+1, p+j).
– Nelfeal
Nov 10 at 14:13













@Nelfeal Sure I understood that reference but for insertion sort what should I do?
– Debojeet Das
Nov 10 at 14:25




@Nelfeal Sure I understood that reference but for insertion sort what should I do?
– Debojeet Das
Nov 10 at 14:25












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Your current method is ... not quite right, for any kind of sort. Insertion sort would take an element and insert it at the correct index, then shift the already inserted items down appropriately. Something like



void looper(int *p)

for (int i = 1;i < 25;i++)

int key = p[i];
int j = i - 1;
while (j >= 0 && p[j] > key)

p[j + 1] = p[j];
j = j - 1;

p[j + 1] = key;







share|improve this answer





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    Your current method is ... not quite right, for any kind of sort. Insertion sort would take an element and insert it at the correct index, then shift the already inserted items down appropriately. Something like



    void looper(int *p)

    for (int i = 1;i < 25;i++)

    int key = p[i];
    int j = i - 1;
    while (j >= 0 && p[j] > key)

    p[j + 1] = p[j];
    j = j - 1;

    p[j + 1] = key;







    share|improve this answer


























      up vote
      0
      down vote



      accepted










      Your current method is ... not quite right, for any kind of sort. Insertion sort would take an element and insert it at the correct index, then shift the already inserted items down appropriately. Something like



      void looper(int *p)

      for (int i = 1;i < 25;i++)

      int key = p[i];
      int j = i - 1;
      while (j >= 0 && p[j] > key)

      p[j + 1] = p[j];
      j = j - 1;

      p[j + 1] = key;







      share|improve this answer
























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Your current method is ... not quite right, for any kind of sort. Insertion sort would take an element and insert it at the correct index, then shift the already inserted items down appropriately. Something like



        void looper(int *p)

        for (int i = 1;i < 25;i++)

        int key = p[i];
        int j = i - 1;
        while (j >= 0 && p[j] > key)

        p[j + 1] = p[j];
        j = j - 1;

        p[j + 1] = key;







        share|improve this answer














        Your current method is ... not quite right, for any kind of sort. Insertion sort would take an element and insert it at the correct index, then shift the already inserted items down appropriately. Something like



        void looper(int *p)

        for (int i = 1;i < 25;i++)

        int key = p[i];
        int j = i - 1;
        while (j >= 0 && p[j] > key)

        p[j + 1] = p[j];
        j = j - 1;

        p[j + 1] = key;








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 4:04

























        answered Nov 10 at 14:29









        BurnsBA

        1,6421220




        1,6421220













            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus