0% found this document useful (0 votes)
43 views

8-Sorting Algorithms A) Insertion Sort

Insertion sort is an in-place comparison-based sorting algorithm that maintains a sorted sub-list by iterating through the list and inserting each element into its sorted position. It removes one element, finds where it belongs in the sorted list, and inserts it there, repeating until the list is fully sorted. The algorithm iterates through the array, compares elements, shifts greater elements to the right, and inserts the element into its sorted place.

Uploaded by

ayesha ahsan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

8-Sorting Algorithms A) Insertion Sort

Insertion sort is an in-place comparison-based sorting algorithm that maintains a sorted sub-list by iterating through the list and inserting each element into its sorted position. It removes one element, finds where it belongs in the sorted list, and inserts it there, repeating until the list is fully sorted. The algorithm iterates through the array, compares elements, shifts greater elements to the right, and inserts the element into its sorted place.

Uploaded by

ayesha ahsan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1

Data Structure & Algorithms

Insertion Sort
This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is
always sorted. For example, the lower part of an array is maintained to be sorted. An element
which is to be inserted in this sorted sub-list, has to find its appropriate place and then it has to
be inserted there. Hence the name, insertion sort. The array is searched sequentially and
unsorted items are moved and inserted into the sorted sub-list (in the same array). This algorithm
is not suitable for large data sets.

Insertion sort iterates, consuming one input element each repetition, and growing a sorted
output list. At each iteration, insertion sort removes one element from the input data, finds the
location it belongs within the sorted list, and inserts it there. It repeats until no input elements
remain.
2
Data Structure & Algorithms

Algorithm
1 for j ← 2 to length[A]

2 do key ← A[ j]

3 Insert A[ j] into the sorted sequence A[1 . . j − 1].


4i←j −1

5 while i > 0 and A[i] > key

6 do A[i + 1] ← A[i]

7i←i−1
8 A[i + 1] ← key

In Simple English we can write Insertion Sort as Follows:

Step 1 − If it is the first element, it is already sorted. return 1;

Step 2 − Pick next element


Step 3 − Compare with all elements in the sorted sub-list

Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted

Step 5 − Insert the value

Step 6 − Repeat until list is sorted

Implementation in C++
int ar[5], length, temp, i, j;

length = 5;

for(i=0; i<length; i++)


{

cout<<"Enter elements: "<<endl;

cin>>ar[i];

for(i=1; i<length; i++)

{
3
Data Structure & Algorithms

temp = ar[i];

j=i-1;
while(temp<ar[j] && j>=0)

ar[j+1] = ar[j];

j--;
}

ar[j+1]=temp;

cout<<"In ascending order:"<<endl;

for(i=0; i<length ; i++)

cout<<ar[i]<<"\t";
}

You might also like