Computer Science
What will be the status of the following list after fourth pass of insertion sort and fourth pass of bubble sort used for arranging elements in ascending order?
12, 14, -54, 64, 90, 24
Python List Manipulation
3 Likes
Answer
Insertion Sort
list = [12, 14, -54, 64, 90, 24]
First pass
[12, 14, -54, 64, 90, 24] → [12, 14, -54, 64, 90, 24] — it will not swap since 12 < 14
Second pass
[12, 14, -54, 64, 90, 24] → [12, -54, 14, 64, 90, 24] — it will swap since -54 < 14
[12, -54, 14, 64, 90, 24] → [-54, 12, 14, 64, 90, 24] — it will swap since -54 < 12
Third pass
[-54, 12, 14, 64, 90, 24] → [-54, 12, 14, 64, 90, 24] — it will not swap since 64 > 14
Fourth pass
[-54, 12, 14, 64, 90, 24] → [-54, 12, 14, 64, 90, 24] — it will not swap since 90 > 64
After the fourth pass of insertion sort, the list is: [-54, 12, 14, 64, 90, 24]
Bubble Sort
list = [12, 14, -54, 64, 90, 24]
First pass — [12, -54, 14, 64, 24, 90]
Second pass — [-54, 12, 14, 24, 64, 90]
First pass
[12, 14, -54, 64, 90, 24] → [12, 14, -54, 64, 90, 24] — it will not swap since 12 < 14
[12, 14, -54, 64, 90, 24] → [12, -54, 14, 64, 90, 24] — it will swap since -54 < 14
[12, -54, 14, 64, 90, 24] → [12, -54, 14, 64, 90, 24] — it will not swap since 14 < 64
[12, -54, 14, 64, 90, 24] → [12, -54, 14, 64, 90, 24] — it will not swap since 64 < 90
[12, -54, 14, 64, 90, 24] → [12, -54, 14, 64, 24, 90] — it will swap since 24 < 90
Second pass
[12, -54, 14, 64, 24, 90] → [-54, 12, 14, 64, 24, 90] — it will swap since -54 < 12
[-54, 12, 14, 64, 24, 90] → [-54, 12, 14, 64, 24, 90] — it will not swap since 12 < 14
[-54, 12, 14, 64, 24, 90] → [-54, 12, 14, 64, 24, 90] — it will not swap since 14 < 64
[-54, 12, 14, 64, 24, 90] → [-54, 12, 14, 24, 64, 90] — it will swap since 24 < 64
[-54, 12, 14, 24, 64, 90] → [-54, 12, 14, 24, 64, 90] — it will not swap since 64 < 90
Since the list is sorted after the second pass: [−54, 12, 14, 24, 64, 90], there is no need to proceed to the third and fourth pass.
Answered By
2 Likes
Related Questions
WAP to shift elements of a list so that first element moves to the second index and second index moves to the third index, etc., and last element shifts to the first position.
Suppose list is [10, 20, 30, 40]
After shifting it should look like: [40, 10, 20, 30]
An array Num contains the following elements:
3, 25, 13, 6, 35, 8, 14, 45
Write a function to swap the content with next value divisible by 5 so that the resultant array looks like:
25, 3, 13, 35, 6, 8, 45, 14
The record of a student (Name, Roll No, Marks in five subjects and percentage of marks) is stored in the following list:
stRecord = ['Raman', 'A-36', [56, 98, 99, 72, 69], 78.8]Write Python statements to retrieve the following information from the list studRecord.
(i) Percentage of the student
(ii) Marks in the fifth subject
(iii) Maximum marks of the student
(iv) Roll No. of the student
(v) Change the name of the student from 'Raman' to 'Raghav'.
Write the steps to arrange the following elements using insertion sort:
12, 5, 14, 8, 3, 54, 25, 10, 27, 30