Here is the insertion sort code from 
http://softwareandfinance.com/CPP_Insertion_Sort.html

int InsertionSort()
{
    int max;
    std::cout << "\nProgram for Ascending order of Numeric Values using 
INSERTION SORT";
    std::cout << "\n\nEnter the total number of elements: ";
    std::cin >> max;    
 
    int *numarray = new int[max];
 
    for(int i = 0; i < max; i++)
    {
        std::cout << "\nEnter [" << i + 1 << "] element: ";
        std::cin >> numarray[i];
    }
 
    std::cout << "Before Sorting   : ";
    for(int k = 0; k < max; k++)
        std::cout << numarray[k] << " ";
    std::cout << "\n";
 
    for(int i = 1; i < max; i++)
    {
        int j = i;
        while(j > 0)
        {
            if(numarray[j-1] > numarray[j])
            {
                int temp = numarray[j - 1];
                numarray[j - 1] = numarray[j];
                numarray[j] = temp;
                j--;
            }
            else
                break;
        }
        std::cout << "After iteration " << i << ": ";
        for(int k = 0; k < max; k++)
            std::cout << numarray[k] << " ";
        std::cout << "/*** " << i + 1 << " numbers from the begining of the 
array are input and they are sorted ***/\n";
    }
 
    std::cout << "\n\nThe numbers in ascending orders are given below:\n\n";
    for(int i = 0; i < max; i++)
    {
        std::cout << "Sorted [" << i + 1 << "] element: ";
        std::cout << numarray[i];
        std::cout << "\n";
    }
 
    delete [] numarray;
    return 0;
}
 

int main()
{
    InsertionSort(); 

    return 0;

}
Output 


--------------------------------------------------------------------------------
Program for Ascending order of Numeric Values using INSERTION SORT
 
Enter the total number of elements: 8
 
Enter [1] element: 80
Enter [2] element: 60
Enter [3] element: 40
Enter [4] element: 20
Enter [5] element: 10
Enter [6] element: 30
Enter [7] element: 50
Enter [8] element: 70
 
Before Sorting   : 80 60 40 20 10 30 50 70
After iteration 1: 60 80 40 20 10 30 50 70 
After iteration 2: 40 60 80 20 10 30 50 70 
After iteration 3: 20 40 60 80 10 30 50 70 
After iteration 4: 10 20 40 60 80 30 50 70 
After iteration 5: 10 20 30 40 60 80 50 70 
After iteration 6: 10 20 30 40 50 60 80 70 
After iteration 7: 10 20 30 40 50 60 70 80 
 
 
The numbers in ascending orders are given below:
 
Sorted [1] element: 10
Sorted [2] element: 20
Sorted [3] element: 30
Sorted [4] element: 40
Sorted [5] element: 50
Sorted [6] element: 60
Sorted [7] element: 70
Sorted [8] element: 80
Press any key to continue . . .

Refer to http://cpp.softwareandfinance.com for more details. 

--- In [email protected], "Rob Richardson" <rob.richard...@...> wrote:
>
> John's suggestion is also likely to be more efficient in the real world,
> as a simple bubble sort (as suggested in an earlier post on this thread)
> can take a lot of time in certain situations.  The sorting algorithm
> used in the Standard Library is much less susceptible to such problems.
> 
> I do not know the details, but I'm sure others on this list do, or they
> can be found with a bit of research.
> 
> RobR
>


Reply via email to