Skip to main content
deleted 1 character in body
Source Link

I recently came across sorting techniques and of particular, 'insertion sorting'.

Although the logic and method is fairly understandable, the actual function seemed a bit complex (given below).

void InSort(int AR[], int size)
{
    int tmp,j; 
    AR[0]=INT_MIN;  //defined in limits.h , basically the smallest possible value 

     for(int i=1;i<size;i++)
     {  
       tmp=AR[i];
       j=i-1;

        while(tmp<AR[j])
         {
           AR[j+1]=AR[j];
           j--;
          }
        
       AR[j+1]=temp;AR[j+1]=tmp;
     }

}

Please note that the elements for the above function are entered from AR[1].

I then tried my own attempt to perform the sort in a more simple way as illustrated below. (ascending)

void iSort(int Arr[] , int size)
{
  Int temp;
  for(int i=1 ; i<size ; i++)
  {

    for(int j=0 ; j<i ; j++)
    {
       if(Arr[i]<Arr[j])
       {
        temp=Arr[i];
        Arr[i]=Arr[j];
        Arr[j]=temp;
        }
    }
  }
}

To my dismay, someone told me that although this method will perform the sort, it does not qualify as an 'Insert Sort', but I still believe that it does follow the same principle.

Is this so? Then, what is particularly wrong with my attempt? More Importantly, does the second function qualify as an insertion sort?

--

I would like to mention that I'm relatively very new to programming in general, so do forgive my seemingly primitive attempt, all I want to do is learn! Also, this isn't the recent C++ version, its actually stone age old, so apologies in advance!

I'd be happy for any inputs received , thanks in advance!

I recently came across sorting techniques and of particular, 'insertion sorting'.

Although the logic and method is fairly understandable, the actual function seemed a bit complex (given below).

void InSort(int AR[], int size)
{
    int tmp,j; 
    AR[0]=INT_MIN;  //defined in limits.h , basically the smallest possible value 

     for(int i=1;i<size;i++)
     {  
       tmp=AR[i];
       j=i-1;

        while(tmp<AR[j])
         {
           AR[j+1]=AR[j];
           j--;
          }
        
       AR[j+1]=temp;
     }

}

Please note that the elements for the above function are entered from AR[1].

I then tried my own attempt to perform the sort in a more simple way as illustrated below. (ascending)

void iSort(int Arr[] , int size)
{
  Int temp;
  for(int i=1 ; i<size ; i++)
  {

    for(int j=0 ; j<i ; j++)
    {
       if(Arr[i]<Arr[j])
       {
        temp=Arr[i];
        Arr[i]=Arr[j];
        Arr[j]=temp;
        }
    }
  }
}

To my dismay, someone told me that although this method will perform the sort, it does not qualify as an 'Insert Sort', but I still believe that it does follow the same principle.

Is this so? Then, what is particularly wrong with my attempt? More Importantly, does the second function qualify as an insertion sort?

--

I would like to mention that I'm relatively very new to programming in general, so do forgive my seemingly primitive attempt, all I want to do is learn! Also, this isn't the recent C++ version, its actually stone age old, so apologies in advance!

I'd be happy for any inputs received , thanks in advance!

I recently came across sorting techniques and of particular, 'insertion sorting'.

Although the logic and method is fairly understandable, the actual function seemed a bit complex (given below).

void InSort(int AR[], int size)
{
    int tmp,j; 
    AR[0]=INT_MIN;  //defined in limits.h , basically the smallest possible value 

     for(int i=1;i<size;i++)
     {  
       tmp=AR[i];
       j=i-1;

        while(tmp<AR[j])
         {
           AR[j+1]=AR[j];
           j--;
          }
        
       AR[j+1]=tmp;
     }

}

Please note that the elements for the above function are entered from AR[1].

I then tried my own attempt to perform the sort in a more simple way as illustrated below. (ascending)

void iSort(int Arr[] , int size)
{
  Int temp;
  for(int i=1 ; i<size ; i++)
  {

    for(int j=0 ; j<i ; j++)
    {
       if(Arr[i]<Arr[j])
       {
        temp=Arr[i];
        Arr[i]=Arr[j];
        Arr[j]=temp;
        }
    }
  }
}

To my dismay, someone told me that although this method will perform the sort, it does not qualify as an 'Insert Sort', but I still believe that it does follow the same principle.

Is this so? Then, what is particularly wrong with my attempt? More Importantly, does the second function qualify as an insertion sort?

--

I would like to mention that I'm relatively very new to programming in general, so do forgive my seemingly primitive attempt, all I want to do is learn! Also, this isn't the recent C++ version, its actually stone age old, so apologies in advance!

I'd be happy for any inputs received , thanks in advance!

added 42 characters in body
Source Link

I recently came across sorting techniques and of particular, 'insertion sorting'.

Although the logic and method is fairly understandable, the actual function seemed a bit complex (given below).

void InSort(int AR[], int size)
{
    int tmp,j; 
    AR[0]=INT_MIN;  //defined in limits.h , basically the smallest possible value 

     for(int i=1;i<size;i++)
     {  
       tmp=AR[i];
       j=i-1;

        while(tmp<AR[j])
         {
           AR[j+1]=AR[j];
           j--;
          }
        
       AR[j+1]=temp;
     }

}

Please note that the elements for the above function are entered from AR[1].

I then tried my own attempt to perform the sort in a more simple way as illustrated below. (ascending)

void iSort(int Arr[] , int size)
{
  Int temp;
  for(int i=1 ; i<size ; i++)
  {

    for(int j=0 ; j<i ; j++)
    {
       if(Arr[i]<Arr[j])
       {
        temp=Arr[i];
        Arr[i]=Arr[j];
        Arr[j]=temp;
        }
    }
  }
}

To my dismay, someone told me that although this method will perform the sort, it does not qualify as an 'Insert Sort', but I still believe that it does follow the same principle.

Is this so? Then, what is particularly wrong with my attempt? More Importantly, does the second function qualify as an insertion sort?

Also, for the first function,

--

I would like to mention that I'm relatively very new to programming in general, so do forgive my seemingly primitive attempt, all I want to do is learn! Also, this isn't the recent C++ version, its actually stone age old, so apologies in advance!

I'd be happy for any inputs received , thanks in advance!

I recently came across sorting techniques and of particular, 'insertion sorting'.

Although the logic and method is fairly understandable, the actual function seemed a bit complex (given below).

void InSort(int AR[], int size)
{
    int tmp,j; 
    AR[0]=INT_MIN;  //defined in limits.h , basically the smallest possible value 

     for(int i=1;i<size;i++)
     {  
       tmp=AR[i];
       j=i-1;

        while(tmp<AR[j])
         {
           AR[j+1]=AR[j];
           j--;
          }
        
       AR[j+1]=temp;
     }

}

Please note that the elements for the above function are entered from AR[1].

I then tried my own attempt to perform the sort in a more simple way as illustrated below. (ascending)

void iSort(int Arr[] , int size)
{
  Int temp;
  for(int i=1 ; i<size ; i++)
  {

    for(int j=0 ; j<i ; j++)
    {
       if(Arr[i]<Arr[j])
       {
        temp=Arr[i];
        Arr[i]=Arr[j];
        Arr[j]=temp;
        }
    }
  }
}

To my dismay, someone told me that although this method will perform the sort, it does not qualify as an 'Insert Sort', but I still believe that it does follow the same principle.

Is this so? Then, what is particularly wrong with my attempt?

Also, for the first function,

I would like to mention that I'm relatively very new to programming in general, so do forgive my seemingly primitive attempt, all I want to do is learn! Also, this isn't the recent C++ version, its actually stone age old, so apologies in advance!

I'd be happy for any inputs received , thanks in advance!

I recently came across sorting techniques and of particular, 'insertion sorting'.

Although the logic and method is fairly understandable, the actual function seemed a bit complex (given below).

void InSort(int AR[], int size)
{
    int tmp,j; 
    AR[0]=INT_MIN;  //defined in limits.h , basically the smallest possible value 

     for(int i=1;i<size;i++)
     {  
       tmp=AR[i];
       j=i-1;

        while(tmp<AR[j])
         {
           AR[j+1]=AR[j];
           j--;
          }
        
       AR[j+1]=temp;
     }

}

Please note that the elements for the above function are entered from AR[1].

I then tried my own attempt to perform the sort in a more simple way as illustrated below. (ascending)

void iSort(int Arr[] , int size)
{
  Int temp;
  for(int i=1 ; i<size ; i++)
  {

    for(int j=0 ; j<i ; j++)
    {
       if(Arr[i]<Arr[j])
       {
        temp=Arr[i];
        Arr[i]=Arr[j];
        Arr[j]=temp;
        }
    }
  }
}

To my dismay, someone told me that although this method will perform the sort, it does not qualify as an 'Insert Sort', but I still believe that it does follow the same principle.

Is this so? Then, what is particularly wrong with my attempt? More Importantly, does the second function qualify as an insertion sort?

--

I would like to mention that I'm relatively very new to programming in general, so do forgive my seemingly primitive attempt, all I want to do is learn! Also, this isn't the recent C++ version, its actually stone age old, so apologies in advance!

I'd be happy for any inputs received , thanks in advance!

added 132 characters in body
Source Link

I recently came across sorting techniques and of particular, 'insertion sorting'.

Although the logic and method is fairly understandable, the actual function seemed a bit complex (given below).

void InSort(int AR[], int size)
{
    int tmp,j; 
    AR[0]=INT_MIN;  //defined in limits.h , basically the smallest possible value 

     for(int i=1;i<size;i++)
     {  
       tmp=AR[i];
       j=i-1;

        while(tmp<AR[j])
         {
           AR[j+1]=AR[j];
           j--;
          }
        
       AR[j+1]=temp;
     }

}

Please note that the elements for the above function are entered from AR[1].

I then tried my own attempt to perform the sort in a more simple way as illustrated below. (ascending)

void iSort(int Arr[] , int size)
{
  Int temp;
  for(int i=1 ; i<size ; i++)
  {

    for(int j=0 ; j<i ; j++)
    {
       if(Arr[i]<Arr[j])
       {
        temp=Arr[i];
        Arr[i]=Arr[j];
        Arr[j]=temp;
        }
    }
  }
}

To my dismay, someone told me that although this method will perform the sort, it does not qualify as an 'Insert Sort', but I still believe that it does follow the same principle.

Is this so? Then, what is particularly wrong with my attempt?

--

Also, for the first function,

I would like to mention that I'm relatively very new to programming in general, so do forgive my seemingly primitive attempt, all I want to do is learn! Also, this isn't the recent C++ version, its actually stone age old, so apologies in advance!

I'd be happy for any inputs received , thanks in advance!

I recently came across sorting techniques and of particular, 'insertion sorting'.

Although the logic and method is fairly understandable, the actual function seemed a bit complex (given below).

void InSort(int AR[], int size)
{
    int tmp,j; 
    AR[0]=INT_MIN;  //defined in limits.h , basically the smallest possible value

     for(int i=1;i<size;i++)
     {  
       tmp=AR[i];
       j=i-1;

        while(tmp<AR[j])
         {
           AR[j+1]=AR[j];
           j--;
          }
        
       AR[j+1]=temp;
     }

}

I then tried my own attempt to perform the sort in a more simple way as illustrated below. (ascending)

void iSort(int Arr[] , int size)
{
  for(int i=1 ; i<size ; i++)
  {

    for(int j=0 ; j<i ; j++)
    {
       if(Arr[i]<Arr[j])
       {
        temp=Arr[i];
        Arr[i]=Arr[j];
        Arr[j]=temp;
        }
    }
  }
}

To my dismay, someone told me that although this method will perform the sort, it does not qualify as an 'Insert Sort', but I still believe that it does follow the same principle.

Is this so? Then, what is particularly wrong with my attempt?

--

I would like to mention that I'm relatively very new to programming in general, so do forgive my seemingly primitive attempt, all I want to do is learn! Also, this isn't the recent C++ version, its actually stone age old, so apologies in advance!

I'd be happy for any inputs received , thanks in advance!

I recently came across sorting techniques and of particular, 'insertion sorting'.

Although the logic and method is fairly understandable, the actual function seemed a bit complex (given below).

void InSort(int AR[], int size)
{
    int tmp,j; 
    AR[0]=INT_MIN;  //defined in limits.h , basically the smallest possible value 

     for(int i=1;i<size;i++)
     {  
       tmp=AR[i];
       j=i-1;

        while(tmp<AR[j])
         {
           AR[j+1]=AR[j];
           j--;
          }
        
       AR[j+1]=temp;
     }

}

Please note that the elements for the above function are entered from AR[1].

I then tried my own attempt to perform the sort in a more simple way as illustrated below. (ascending)

void iSort(int Arr[] , int size)
{
  Int temp;
  for(int i=1 ; i<size ; i++)
  {

    for(int j=0 ; j<i ; j++)
    {
       if(Arr[i]<Arr[j])
       {
        temp=Arr[i];
        Arr[i]=Arr[j];
        Arr[j]=temp;
        }
    }
  }
}

To my dismay, someone told me that although this method will perform the sort, it does not qualify as an 'Insert Sort', but I still believe that it does follow the same principle.

Is this so? Then, what is particularly wrong with my attempt?

Also, for the first function,

I would like to mention that I'm relatively very new to programming in general, so do forgive my seemingly primitive attempt, all I want to do is learn! Also, this isn't the recent C++ version, its actually stone age old, so apologies in advance!

I'd be happy for any inputs received , thanks in advance!

fix spelling
Source Link
Kilian Foth
  • 110.3k
  • 45
  • 300
  • 321
Loading
Source Link
Loading