-1

I wrote a program to run the insertion sorting algorithms. Everything seems good. But when I execute it there is a strange result. It seems that the programs uses garbage in memory. How could I fix it?

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void insertion_inc(int Array[], int size)
{

    int i, j, key; 
    for (i = 1; i < size; i++)
    {
        j = i - 1;
        key = Array[i];
        while (j >=0 && Array[j]>key )
        {
            Array[j + 1] = Array[j];
            j--;
        }
        Array[j++] = key;
    }
}


void insertion_dec(int Array[], int size)
{

    int i, j, key;
    for (i = 1; i < size; i++)
    {
        j = i - 1;
        key = Array[i];
        while (j >= 0 && Array[j] < key)
        {
            Array[j + 1] = Array[j];
            j--;
        }
        Array[j++] = key;
    }
}

void print_array(int arr[], int size)
{

    for (int i = 0; i < size; i++)
        printf(" %d ", arr[i]);
}


/////////////////////////////////////////////

int main()
{

    time_t t;
    srand((unsigned)time(&t));
    int Arr[10];
    int size = sizeof(Arr);
    printf("The unsorted Array : \n ");
    for (int i = 0; i < size;i++)
        Arr[i] = rand()/10;
    print_array(Arr, size);
    printf("\n\n\n The sorted Array : \n");
    insertion_inc(Arr, size);
    print_array(Arr, size);
    return 0;
}

**The output always looks like this : **

The unsorted Array :

12 23 54 87 2 68 29 57 97 12 -858993460 272204808 7601072 7742451 1 15039136 15044096 1 15039136 15044096 7601164 7742023 272204692 7738189 7738189 8462336 0 0 0 0 0 0 0 0 7775604 7775616 0 7601080 0 7601272

The sorted Array :

23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 57 68 7601272 7738189 7738189 7738189 7742023 7775616 8462336 8462336 8462336 15039136 15039136 15039136 15039136 15044096 272204692 272204808 272204808 272204808 272204808 272204808 272204808 272204808 272204808

Even when I ignore the use of srand() and rand(), there is still the problem.

dbush
  • 205,898
  • 23
  • 218
  • 273
Ali.Nemat
  • 39
  • 3
  • 5
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/1362568) – Mike Kinghan May 11 '20 at 18:16
  • Welcome. Which IDE do you use? If the answer is Notepad++ or similar, that's an editor, so get an IDE. now that you have that, learn to use the debugger; you can set breakpoints, run your code and it will stop when it hits a breakpoint, at a line in your code where you want it to stop. Then you can examine the call stack & the values of all of your variables, plus more. The debugger is your best friend and could have solved this, and many/most, future questions for you. Good luck – Mawg says reinstate Monica May 11 '20 at 18:19
  • `while (j >=0 && Array[j]>key )` can overflow `Array` I believe. Need an upper bounds check. – Michael Dorgan May 11 '20 at 18:19
  • I use visual Stedio 2019. yes your right about the Array overflow. so how should i alter the code ? – Ali.Nemat May 11 '20 at 20:27

2 Answers2

2

This line:

int size = sizeof(Arr);

does not do what you think it does. It is returning the size in bytes, rather than elements.

See How do I determine the size of my array in C?

Acorn
  • 24,970
  • 5
  • 40
  • 69
1

You're not calculating the array size correctly:

int size = sizeof(Arr);

The sizeof operator gives the size of its operand in bytes. So assuming an int is 4 bytes on your system, sizeof(Arr) evaluates to 40.

You instead want to divide the size of the array by the size of an element of the array to get the number of elements.

int size = sizeof(Arr) / sizeof(*Arr);
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thanks for the Answer. But what about the the result of the sorting function. it dosen`t work properly, if you look at the results (the first ten numbers you get to know the problem). what should i do about it ? – Ali.Nemat May 11 '20 at 20:24