0% found this document useful (0 votes)
13 views8 pages

Insertion Sort

The document provides an overview of sorting algorithms, specifically focusing on insertion sort. It explains the concept of sorting, different types of sorting (internal, external, in-place), and illustrates the insertion sort process with examples and a sample code implementation. The algorithm rearranges elements by repeatedly placing unsorted elements into their correct position in a sorted list.

Uploaded by

srp.ch12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Insertion Sort

The document provides an overview of sorting algorithms, specifically focusing on insertion sort. It explains the concept of sorting, different types of sorting (internal, external, in-place), and illustrates the insertion sort process with examples and a sample code implementation. The algorithm rearranges elements by repeatedly placing unsorted elements into their correct position in a sorted list.

Uploaded by

srp.ch12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SORTING

A Sorting Algorithm is used to rearrange a given array or list


elements
according to a comparison operator on the elements. The comparison
operator is used to decide the new order of element in the respective
data structure.
Various algorithms are better suited to some of these situations
Internal Sort
– The data to be sorted is all stored in the computer’s main
memory.
External Sort
– Some of the data to be sorted might be stored in some
external, slower, device.
In Place Sort
– The amount of extra space required to sort the data is
constant with the input size.
INSERTION SORT
Insertion sort is a sorting algorithm that places an
unsorted element at its suitable place in each iteration.
• Idea: like sorting a hand of playing cards
– Start with an empty left hand and the cards facing
down on the table.
– Remove one card at a time from the table, and insert
it into the correct position in the left hand
• compare it with each of the cards already in the hand,
from right to left
– The cards held in the left hand are sorted
• these cards were originally the top cards of the pile on
the table
INSERTION SORT

To insert 12, we need to


make room for it by moving
first 36 and then 24.

6 10 24 36

12
INSERTION SORT

6 10 24 36

12
INSERTION SORT

6 10 24 3
6

12
INSERTION SORT
Before going through the program, lets see the steps of
insertion sort with the help of an example.
Input elements: 89 17 8 12 0
s

Step 1: 89 17 8 12 0 (the bold elements are sorted list


and non-bold unsorted list)
a

Step 2: 17 89 8 12 0 (each element will be removed


from unsorted list and placed at the right position in the
sorted list)

Step 3: 8 17 89 12 0
Step 4: 8 12 17 89 0
Step 5: 0 8 12 17 89
INSERTION SORT
Algorithm:--
for(i=1;i<n;i++)
{
k=a[i];
j=i-1;
while(j>=0 && a[i]>key)
{
a[j+1]=a[j];
j=j+1;
}
a[j+1]=key;
}
INSERTION SORT
#include<stdio.h>
int main()
{
int i, j, count, temp, number[25];
printf("How many numbers u are going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
// This loop would store the input numbers in array
for(i=0;i<conunt;i++)
scanf("%d",&number[i]);
// Implementation of insertion sort algorithm
for(i=1;i<count;i++)
{
temp=number[i];
j=i-1;
while((temp<number[j])&&(j>=0))
{
number[j+1]=number[j];
j=j-1;
}
number[j+1]=temp;
}
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}

You might also like