Question

Problem 9. (10 pts.) Sort the sequence [8.7.4,9,2,5) using the Bubble sort algorithm below. Show the sequence in each step. 2

Please also give the explanation thanks

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

Completing the table,we have:

i=1

i=2 i=3 i=4 i=5
j=1 7,8,4,9,2,5 4,7,8,2,5,9 4,7,2,5,8,9 2,4,5,7,8,9 2,4,5,7,8,9
j=2 7,4,8,9,2,5 4,7,8,2,9,5 4,2,7,5,8,9 2,4,5,7,8,9 xxx
j=3 7,4,8,9,2,5 4,7,2,8,5,9 4,2,5,7,8,9 xxx xxx
j=4 7,4,8,2,9,5 4,7,2,5,8,9 xxx xxx xxx
j=5 7,4,8,2,5,9 xxx xxx xxx xxx

here xxx= Loop Conditions Terminated

Given Array ={8,7,4,9,2,5}

PAAS 1:

8,7,4,9,2,5 -->7,8,4,9,2,5 [Algorithm compares the first two elements and swaps since 8>7]

7,8,4,9,2,5 --> 7,4,8,9,2,5 [since 8>4 ,swap]

7,4,8,9,2,5--> 7,4,8,9,2,5 [since 9>8,no swap]

7,4,8,9,2,5-->7,4,8,2,9,5 [since 9>2, swap]

7,4,8,2,9,5-->7,4,8,2,9,5 [since 9>5,swap]

PAAS 2:

7,4,8,2,9,5 -->4,7,8,2,5,9   [since 7>4 swap]

4,7,8,2,5,9-->4,7,8,2,9,5 [since 8>7 no swap]

4,7,8,2,9,5-->4,7,2,8,5,9   [ since 8>2 swap]

4,7,2,8,5,9-->4,7,2,5,8,9 [ since 8>5 swap]

PAAS 3:

4,7,2,5,8,9 -->4,7,2,5,8,9 [since 7>4 no swap]

4,7,2,5,8,9-->4,2,7,5,8,9 [since 7>2,swap]

4,2,7,5,8,9-->4,2,5,7,8,9 [ since 7>5 swap]

PAAS 4:

4,2,5,7,8,9 -->2,4,5,7,8,9 [since 4>2 swap]

2,4,5,7,8,9-->2,4,5,7,8,9 [since 4<5 no swap]

Now, the array is sorted but the algorithm does not know about it, hence it will cover another pass.

PAAS 5:

2,4,5,7,8,9 -->2,4,5,7,8,9

Hence the above array is sorted.

ThankYou

Add a comment
Know the answer?
Add Answer to:
Please also give the explanation thanks Problem 9. (10 pts.) Sort the sequence [8.7.4,9,2,5) using the...
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
  • Consider the following python code that will sort the list of numbers using the Bubble Sort...

    Consider the following python code that will sort the list of numbers using the Bubble Sort algorithm def bubbleSort(alist): print(alist) for j in range (len(alist) - 1, 8, -1): for i in range(): if alist[i] > alist[i + 1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp print(alist) alist = [52, 25, 94, 17, 25, 52] bubbleSort (alist) print(alist) Sort the following series of values into ascending order using the Bubble Sort algorithm. Write out the complete row of...

  • Solve ques no. 2 a, b, c, d . Algorithm 1 Sort a list al,..., an...

    Solve ques no. 2 a, b, c, d . Algorithm 1 Sort a list al,..., an for i=1 to n-1 do for j=1 to n-i do if aj > aj+1 then interchange a; and a;+1 end if end for end for (b) Algorithm 1 describes a sorting algorithm called bubble sort for a list al,...,an of at least two numbers. Prove that the algorithm is complete, correct and terminates. (2) Complexity of Algorithms (Learning Target C2) (a) What is the...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • Search for 4 in the sequence {3,5,7,8,9,12,21,25}, by working through each step of the algorithm given...

    Search for 4 in the sequence {3,5,7,8,9,12,21,25}, by working through each step of the algorithm given below. Specify the values of i, j, m and am in each step. procedure binary search (x: integer, a1,a2,...,an: increasing integers) i := 1 {i is left endpoint of search interval } j := n {j is right endpoint of search interval } while i < j m := b(i + j)/2c if x > am then i := m + 1 else j...

  • Please help me to solve the problem with java language! An implementation of the Merge Sort...

    Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...

  • please I need it urgent thanks algorithms 2.1 Searching and Sorting- 5 points each 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Ex...

    please I need it urgent thanks algorithms 2.1 Searching and Sorting- 5 points each 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 4. Give pseudocode for an algorithm that will solve the following problem. Given an array AlL..n) that contains every number between 1 and n +1 in...

  • [5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9,...

    [5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9, 4, 3). Assuming we call the following function using statement selection sort (int array, 7); Please fill the table to show the changes of the array int_array after each iteration. The first column is filled. void selection_sort (int list[], int list_size) for (int i = 0; i < list size - 1; 1++) int current min = list[1]; int current_min_index-i for (int j -...

  • Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below...

    Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...

  • 7. An alternate version of bubblesort sorts the list in ascending order by moving the smallest va...

    7. An alternate version of bubblesort sorts the list in ascending order by moving the smallest value to the first position on the first pass and so on. The pseudocode for this version is shown below. The procedure swap is used to interchange the values in its two arguments. In the box, rewrite the contents of the list 5, 2, 3, 4, 1 every time it changes, in this5, 2, 3, 4, 1 new version of bubblesort. Walk through each...

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