Consider an array of length 5, arr[5] = {9,7,4,2,1}. What are the steps of…

2024

Consider an array of length 5, arr[5] = {9,7,4,2,1}. What are the steps of insertions done while running insertion sort on the array?

  1. A.

    7 9 4 2 1 4 7 9 2 1 24 7 9 1 1 2 4 7 9

  2. B.

    9 7 4 1 2 9 7 1 2 9 1 2 4 7 1 2 4 7 9

  3. C.

    7 4 2 1 9 4 2 1 9 7 2 1 9 7 4 1 9 7 4 2

  4. D.

    7 9 4 2 1 2 4 7 9 1 4 7 9 2 1 1 2 4 7 9

Attempted by 207 students.

Show answer & explanation

Correct answer: A

Answer: a

Explanation: Insertion sort works by building a sorted portion of the array one element at a time. At each step, an element (called the 'key') is taken from the unsorted part and inserted into its correct position in the sorted part.

Step 1: Start with the second element (index 1) as the key. Compare 7 with 9. Since 7 < 9, swap them. Array becomes: 7 9 4 2 1.

Step 2: Move to the third element (index 2) as the key. Compare 4 with 9 and 7. Since 4 < 7 and 4 < 9, shift both 7 and 9 to the right and place 4 at the front. Array becomes: 4 7 9 2 1.

Step 3: Move to the fourth element (index 3) as the key. Compare 2 with 9, 7, and 4. Since 2 < 4, 2 < 7, and 2 < 9, shift all three to the right and place 2 at the front. Array becomes: 2 4 7 9 1.

Step 4: Move to the fifth element (index 4) as the key. Compare 1 with 9, 7, 4, and 2. Since 1 < 2, 1 < 4, 1 < 7, and 1 < 9, shift all four to the right and place 1 at the front. Array becomes: 1 2 4 7 9.

In each step, the key is the element being compared with the elements to its left. The process continues until the entire array is sorted.

हिन्दी उत्तर: a

व्याख्या: इन्सर्शन सॉर्ट एक ऐसा एल्गोरिथ्म है जो एक तत्व को एक बार में लेकर उसे उसकी सही स्थिति में रखता है। शुरुआत में, दूसरे तत्व को की (key) माना जाता है और उसे उसके बाईं ओर के तत्वों के साथ तुलना की जाती है। यदि यह छोटा है, तो वे एक स्थान आगे बढ़ते हैं और की को उसकी सही स्थिति में रखा जाता है।

चरण 1: दूसरे तत्व (7) को की मानें। 9 के साथ तुलना करें। 7 < 9 होने पर, उन्हें आपस में बदल दें। अब अर्रे हो जाता है: 7 9 4 2 1।

चरण 2: तीसरे तत्व (4) को की मानें। 9 और 7 के साथ तुलना करें। 4 < 7 और 4 < 9 होने पर, दोनों को दाईं ओर बढ़ाएं और 4 को शुरू में रखें। अब अर्रे हो जाता है: 4 7 9 2 1।

चरण 3: चौथे तत्व (2) को की मानें। 9, 7 और 4 के साथ तुलना करें। 2 < 4, 2 < 7 और 2 < 9 होने पर, तीनों को दाईं ओर बढ़ाएं और 2 को शुरू में रखें। अब अर्रे हो जाता है: 2 4 7 9 1।

चरण 4: पांचवें तत्व (1) को की मानें। 9, 7, 4 और 2 के साथ तुलना करें। 1 < 2, 1 < 4, 1 < 7 और 1 < 9 होने पर, चारों को दाईं ओर बढ़ाएं और 1 को शुरू में रखें। अब अर्रे हो जाता है: 1 2 4 7 9।

प्रत्येक चरण में, की वह तत्व होता है जिसकी तुलना उसके बाईं ओर के तत्वों के साथ की जाती है। यह प्रक्रिया तब तक जारी रहती है जब तक पूरा अर्रे सॉर्ट नहीं हो जाता।

Explore the full course: Amcat Preparation