Question

Data Structure and Algorithm

(a) Given the following integer list: 10 23 12 34 a[0] a[1] a[2] a[3] a[4] Show a trace (step by step) for each execution of

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

The list is:

10, 23, 2, 12, 24

In the first iteration of the outer loop, pass =1

=> Inner loop starts executing from i = 0

==> a[0] and a[1] are compared and as a[i] is not more than a[i+1]. So nothing happens

=> Inner loop executes for i = 1

==> a[1] and a[2] are compared and as a[i] is more than a[i+1]. So both are swapped and list becomes: 10, 2, 23, 12, 24

=> Inner loop executes for i = 2

==> a[2] and a[3] are compared from the updated list and as a[i] is more than a[i+1]. So both are swapped and list becomes: 10, 2, 12, 23, 24

=> Inner loop executes for i = 3

==> a[3] and a[4] are compared and as a[i] is not more than a[i+1]. So nothing happens.

So, after first iteration of outer loop, list is:

10, 2, 12, 23, 24

In the second iteration of the outer loop, pass =2

=> Inner loop starts executing from i = 0

==> a[0] and a[1] are compared and as a[i] is more than a[i+1]. So both are swapped and list becomes: 2, 10, 12, 23, 24

=> Inner loop executes for i = 1

==> a[1] and a[2] are compared from the updated list and as a[i] is not more than a[i+1]. So nothing happens

=> Inner loop executes for i = 2

==> a[2] and a[3] are compared from the updated list and as a[i] is not more than a[i+1]. So nothing happens

=> Inner loop executes for i = 3

==> a[3] and a[4] are compared and as a[i] is not more than a[i+1]. So nothing happens.

So, after second iteration of outer loop, list is:

2, 10, 12, 23, 24

In the third iteration of the outer loop, pass =3

=> Inner loop starts executing from i = 0

==> a[0] and a[1] are compared and as a[i] is not more than a[i+1]. So nothing happens

=> Inner loop executes for i = 1

==> a[1] and a[2] are compared from the updated list and as a[i] is not more than a[i+1]. So nothing happens

=> Inner loop executes for i = 2

==> a[2] and a[3] are compared from the updated list and as a[i] is not more than a[i+1]. So nothing happens

=> Inner loop executes for i = 3

==> a[3] and a[4] are compared and as a[i] is not more than a[i+1]. So nothing happens.

So, after third iteration of outer loop, list is:

2, 10, 12, 23, 24

In the fourth iteration of the outer loop, pass =4

=> Inner loop starts executing from i = 0

==> a[0] and a[1] are compared and as a[i] is not more than a[i+1]. So nothing happens

=> Inner loop executes for i = 1

==> a[1] and a[2] are compared from the updated list and as a[i] is not more than a[i+1]. So nothing happens

=> Inner loop executes for i = 2

==> a[2] and a[3] are compared from the updated list and as a[i] is not more than a[i+1]. So nothing happens

=> Inner loop executes for i = 3

==> a[3] and a[4] are compared and as a[i] is not more than a[i+1]. So nothing happens.

So, after fourth iteration of outer loop, list is:

2, 10, 12, 23, 24

In the fifth iteration of the outer loop, pass =3

=> Inner loop starts executing from i = 0

==> a[0] and a[1] are compared and as a[i] is not more than a[i+1]. So nothing happens

=> Inner loop executes for i = 1

==> a[1] and a[2] are compared from the updated list and as a[i] is not more than a[i+1]. So nothing happens

=> Inner loop executes for i = 2

==> a[2] and a[3] are compared from the updated list and as a[i] is not more than a[i+1]. So nothing happens

=> Inner loop executes for i = 3

==> a[3] and a[4] are compared and as a[i] is not more than a[i+1]. So nothing happens.

So, after fifth iteration of outer loop, list is:

2, 10, 12, 23, 24

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!

Add a comment
Know the answer?
Add Answer to:
Data Structure and Algorithm (a) Given the following integer list: 10 23 12 34 a[0] a[1]...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Sort the list of numbers: 35, 12, 6, 23, 18 using Selection sort. Work through each...

    Sort the list of numbers: 35, 12, 6, 23, 18 using Selection sort. Work through each step of the algorithm. The algorithm for selection sort is as follows: i. Find the smallest item in a list. ii. Swap this value with the value currently at the front of the list. iii. Repeat Steps 1 and 2 with the current size of the list minus one (list size = list size-1)

  • 1.             (5 marks) Perform a selection sort on the list 23, 59, 34, 13, 31, 10. Show...

    1.             (5 marks) Perform a selection sort on the list 23, 59, 34, 13, 31, 10. Show the list after each exchange that has an effect on the list ordering. 2.             (5 marks) Explain why the bubble sort algorithm does Ө(n2) comparisons on an n-element list. The bubble-sort algorithm is shown just after question 10 on p. 141. 3.              (5 marks) Write the resulting data list, give the ending value of legit, and find the exact number of copies done by the converging...

  • Introduction BUBBLE SORT: Step-by-step example Let us take the array of numbers "5 1 4 2...

    Introduction BUBBLE SORT: Step-by-step example Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required First Pass: ( 5 1 4 2 8 ) → ( 1 5 4 2 8 ). Here, algorithm compares the first two elements, swaps since 5 (15428)→(14528). Swap since 5 > 4 (14528)→(14258). Swap...

  • 11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I...

    11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I Fig. 6.15: fig06 15.c 2 Sorting an array's values into ascending order. Finclude <stdio.h> 4 #define SIZE 10 6 function main begins program execution 7 int main(void) 9 initialize a lo int a CSIZEJ 12, 6, 4, 8, 10, 12, 89, 68, 45, 37) 12 putsc Data items in original order output original array IS for (size t i 0; i SIZE ++i) a[i])...

  • I need the answer selection 3. Given the array of 10 integers shown below, apply bubble...

    I need the answer selection 3. Given the array of 10 integers shown below, apply bubble sort to sort the array of values into descending order (i.e., from large to small). Show the content of the array after the 1" and 2 passes of bubble sort performed on the array. The bubble sort algorithm is given on the last page of the exam if you need a reference. selech 45 34 126 24 18 9021 19 55 Show the content...

  • in C please 20. Change the bubble sort algorithm (Program 12-5) as follows: Use two directional...

    in C please 20. Change the bubble sort algorithm (Program 12-5) as follows: Use two directional bubbling in each pass. In the first bubbling, the smallest ele. ment is bubbled up; in the second bubbling, the largest element is bubbled I down. This sort is known as the shaker sort. 12-5 Bubble Sort (continued) // Statements // Each iteration is one sort pass for (int current = 0, sorted = 0; current <= last && !sorted; current++) for (int walker...

  • Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is...

    Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is said to be ordered if its values order. In an ascending ordered array, the value of each element is less than or equal to the value of the next element. That is, [each element] <= [next element]. A sort is an algorithm for ordering an array. Of the many different techniques for sorting an array we discuss the bubble sort It requires the swapping...

  • Show the execution of the selection sort algorithm on the following array. Hint: The yellow or...

    Show the execution of the selection sort algorithm on the following array. Hint: The yellow or shaded squares should be the remaining unsorted values. Pass # 0 1 2 3 4 5 6 7 0 16 11 21 32 41 20 3 9 1 2 3 4 5 6 7 Show the execution of the insertion sort algorithm on the following array. Hint: The yellow or shaded squares should be the remaining unsorted values. Pass # 0 1 2 3...

  • I. Write a computer program to do the following tasks: a) read a ist of integer...

    I. Write a computer program to do the following tasks: a) read a ist of integer data items into to an aray: b) date items that need to be sorted by using the bubble sort algorithm; c) using a binary scarch algorithm to determine the position of a specific item in the list. In the bubble sort procedure, you need to print the left and right endpoints for each searching step. Your program should print the location of the data...

  • In the Super Simple CPU command 0100000000000101, what does the operand represent? the data to load...

    In the Super Simple CPU command 0100000000000101, what does the operand represent? the data to load the address of the data to load the operation to perform the instruction number ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ What is the Pep/8 assembly command to read a decimal number into memory? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Write an algorithm to find the max in a list. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For this question, use the following array of values: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 23 41 66 20...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT