Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
2.4k views
in Computer by (69.8k points)

Define a heap. Describe the algorithm to insert an element into the heap. 

1 Answer

+1 vote
by (69.3k points)
selected by
 
Best answer

Insertion Sort Algorithm: 

Consider an array 'A' with 'N' 

elements

1. set A[0]=-infinity [initialize sentinel element]

2. Repeat steps 3 to 5 for K=2,3..,.......N

3. set TEMP=A[K] and PTR=K-1 4 Repeat while TEMP <A[PTR]

(a) set A[PTR+1]=A[PTR] [moves element forward] 

(b) set PTR=PTR-1 [End of loop]

5. set A[PTR+1]=TEMP [inserts elements in proper place

[End of step2 loop]

6. Return

Complexity of insertion sort

Worst case: The worst case occurs when the -array 'A' is in reverse order and the inner loop must use the max number K-1 of comparisons. Hence

F(n)= 1 + 2+........+(n-1)=n(n-1)/2=0(n2 )

Average case: In Average case there will be approximately (K-1)/2 comparisons in the inner loop. Accordingly, for the average case,

F(n)=1/2+2/2+........ ...+(n-1)/2=n(n-1)/4=0(n2 )

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...