Insertion Sort Using User Input and Without Function - All About Technology! "

All About Technology!

All About Technology! A complete guide to latest technology and science.

Insertion Sort Using User Input and Without Function


Insertion sort 

  • Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands.
  • Insertion sort is efficient for sorting small number of elements only.
  • The elements we wish to sort are also known as key.
  • We chcek every elemnts left to the inserted element and sort the elements.
/*A program for insertion sort of an array using user input without function
Author: Abdullah Bin Kasem Bhuiyan
Email: jehadfeni@gmail.com
 */
#include 

int main()
{
    //Declaration of array and relevant variables
    int A[3],i;

    for(i=0; i<3 aking="" array="" d="" element:="" from="" i="" input="" int="" j="" ninput="" orting="" printf="" scanf="" the="" user="" while="">0 && A[j-1]>A[j])
        {
            int temp = A[j-1];
            A[j-1]=A[j];
            A[j]=temp;
            j--;
        }
    }
    //printing the sorted array
    printf("\nPrinted Array: \n");
    for(i=0; i<3; i++)
    {
        printf("%d\n",A[i]);
    }
    return 0;
}

Time Complexity: O(n*2)

Auxiliary Space: O(1)

Boundary Cases: Insertion sort takes maximum time to sort if elements are sorted in reverse order. And it takes minimum time (Order of n) when elements are already sorted.


No comments:

Post Top Ad