What is Insertion Sort. Its basic information

muqadasqasim10 24 views 11 slides May 14, 2024
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

it consists of the introduction , algorithms and code output. https://educationgateway.org/
This pptx also consists of advantages and disadvantages of insertion sort.


Slide Content

educationgateway.org Insertion-Sort

INTRODUCTION The idea of insertion sort is similar to the idea of sorting playing cards

INSERTION-SORT Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part

INSERTION-SORT CONT........ 5 1 6 3 4 3 Lets take this Array. As we can see here, in insertion sort, we pick up a key, and compares it with elemnts ahead of it, and puts the key in the right place 5 has nothing before it. 1 is compared to 5 and is inserted before 5. 6 is greater than 5 and 1. 2 is smaller than 6 and 5, but greater than 1, so its is inserted after 1. And this goe s on. ..

ALGORITHM INSERTION-SORT (A) 1. for j=2 to A.length 2. key= A[j] 3. // Insert A [j] into the sorted sequence A[i.. j - 1 ] 4. i = j - 1 5. while i > 0 and A[i] > ky 6. A[i + 1] = A[i] 7. i = i - 1 8. A[i + 1] = key

CODE

CODE OUTPUT

ADVANTAGES Simple Efficient for small data sets One of the faster O(n^2) performance algorithms Does not require extra memory Low overhead Best case is O(n) Nearly sorted input

DISADVANTAGES Poor performance with large lists Expensive with many elements Not as quick as merge sort or quicksort Worst case is O(n^2) - Input Array/List in reverse order

TIME COMPLEXITY Best case: O(n) Average case: O(n^2) • W orst case: O(n^2)

Thanks