Question
data structure
Basic Sorts Each of the following sorting algorithms is applied to the following array of numbers: 5 6 1 8 2 4 37 The array a
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Bubble (not optimized):

original array-> 5 6 1 8 2 4 3 7

1st iteration-> 5 1 6 2 4 3 7 8
2nd iteration-> 1 5 2 4 3 6 7 8
3rd iteration-> 1 2 4 3 5 6 7 8
4th iteration-> 1 2 4 3 5 6 7 8
5th iteration-> 1 2 3 4 5 6 7 8
6th iteration-> 1 2 3 4 5 6 7 8
7th iteration-> 1 2 3 4 5 6 7 8

There is error in 4th iteration-> 1 2 4 3 5 6 7 8

Step by step execution of 4th iteration:

Array after 3rd iteration is 1 2 4 3 5 6 7 8

highlighted elements are compared and if the first element is greater than second element then they are swapped with each other.

1 2 4 3 5 6 7 8

1 2 4 3 5 6 7 8

1 2 4 3 5 6 7 8 <------------- 4,3 will get swapped because 3<4

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7 8

The correct order of elements will be-> 1 2 3 4 5 6 7 8

Selection:

original array-> 5 6 1 8 2 4 3 7

1st iteration-> 1 6 5 8 2 4 3 7
2nd iteration-> 1 2 5 8 6 4 3 7
3rd iteration-> 1 2 3 8 6 4 5 7
4th iteration-> 1 2 3 4 6 8 5 7
5th iteration-> 1 2 3 4 5 8 6 7
6th iteration-> 1 2 3 4 5 6 7 8
7th iteration->  1 2 3 4 5 6 7 8

There is error in 6th iteration-> 1 2 3 4 5 6 7 8

Array after 5th iteration is 1 2 3 4 5 8 6 7

1 2 3 4 5 8 6 7 <--------------- highlighted part is unsorted part

minimum value element in the unsorted part is 6, therefore 8,6 will get swapped then the new sequence will become

1 2 3 4 5 6 8 7

The correct order of elements will be-> 1 2 3 4 5 6 8 7

Insertion:

original array-> 5 6 1 8 2 4 3 7

1st iteration-> 5 6 1 8 2 4 3 7
2nd iteration-> 1 5 6 8 2 4 3 7
3rd iteration-> 1 5 6 8 2 4 3 7
4th iteration-> 1 2 5 6 8 4 3 7
5th iteration-> 1 2 3 5 6 8 4 7
6th iteration-> 1 2 3 4 5 6 8 7
7th iteration->  1 2 3 4 5 6 7 8

There is error in 5th iteration-> 1 2 3 5 6 8 4 7

Array after 4th iteration is 1 2 5 6 8 4 3 7 <--- highlighted part is the sorted part

In the 5th iteration 4 should be insert in the sorted part then the new sequence will become 1 2 4 5 6 8 3 7

The correct order of elements will be-> 1 2 4 5 6 8 3 7

Add a comment
Know the answer?
Add Answer to:
data structure Basic Sorts Each of the following sorting algorithms is applied to the following array...
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
  • 8. (5 points) Trace merge sort algorithm as it sorts the following sequence of integer array into...

    8. (5 points) Trace merge sort algorithm as it sorts the following sequence of integer array into a descending order. 42 45 10 64 55 37 96 7 9. (5 points) Trace shell sort algorithm that halves the gap size at each iteration as it sorts the following sequence of integer array into an ascending order. 42 18 10 64 85 37 96 71 8. (5 points) Trace merge sort algorithm as it sorts the following sequence of integer array...

  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

  • 2) Sorting (a) (5 pts) In a Merge Sort of 8 elements, the Merge function gets...

    2) Sorting (a) (5 pts) In a Merge Sort of 8 elements, the Merge function gets called 7 times. Consider a Merge Sort being executed on the array shown below. What does the array look like right AFTER the sixth call to the Merge function completes? نرا index value 0 40 2 12 4 11 5 99 6 31 7 16 27 18 0 1 2 زيا 4 5 6 7 Index Value (b) (5 pts) Consider sorting the array...

  • PROBLEM #11 What are the two algorithms used to sort an array? How do they work?...

    PROBLEM #11 What are the two algorithms used to sort an array? How do they work? -------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------------------------- PROBLEM #12 Given the following array 'jumbled', what will the array be after the first swap of a selection sort algorithm? (Assume the algorithm sorts in ascending order). --------------------------------------------------------------------------------------------------------------- */ int jumbled[] = {12, 5, 3, 1, 9, 100, 45}; /* --------------------------------------------------------------------------------------------------------------- PROBLEM #13 Given the following array 'jumbled2', what will the array be after the third swap of a...

  • Java We did in lecture, and explain your answer briefly. Problem 4: Sorting practice 14 points;...

    Java We did in lecture, and explain your answer briefly. Problem 4: Sorting practice 14 points; 2 points for each part individual-only Important: When answering these questions, make sure to apply the versions of these algorithms that were discussed in lecture. The Java code for each algorithm can be found in our Sort class. Given the following array: {14, 7, 27, 13, 24, 20, 10, 33 1. If the array were sorted using selection sort, what would the array look...

  • C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential...

    C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential search of a list assumes that the list elements are sorted in ascending order. b. A binary search of a list assumes that the list is sorted. 2. Consider the following list: 63 45 32 98 46 57 28 100 Using a sequential search, how many comparisons are required to determine whether the following items are in the list or not? (Recall that comparisons...

  • ?PLEASE READ CAREFULLY QUESTION #1 I’m doing a project which requires you to implement 4 sorting...

    ?PLEASE READ CAREFULLY QUESTION #1 I’m doing a project which requires you to implement 4 sorting algorithms. Bubble sort pair-wise, Bubble sort list-wise a.k.a selection sort, merge sort, and quick sort. These 4 sorting methods takes in an array of strings and sorts them alphabetically from a-z. I have all 4 sorting algorithms working fine, but I still need to fill out the table. There’s only one section I need help filling out. I basically need help filling out the...

  • 8 Sorting Algorithms: Bubble, selection, insertion, quick, merge, bucket, radix, counting. 1. A..Which of the above...

    8 Sorting Algorithms: Bubble, selection, insertion, quick, merge, bucket, radix, counting. 1. A..Which of the above sorting algorithms does TimSort use? 2. Which of the above algorithms sort a REVERSE ORDER list in O(n2 ) (worst case)? 3. Which of the above algorithms sort a REVERSE ORDER list in O(nlogn) (worst case)? 4. Which of the above algorithms sort an ordered list , a reverse ordered list, and a random list (all three) in 0(nlogn) (worst case)? 5. Which of...

  • Step 1: Select any four sorting algorithm and two searching algorithms Step 2: Understand the logic...

    Step 1: Select any four sorting algorithm and two searching algorithms Step 2: Understand the logic of all the algorithms Step 3: Create java program and use your sorting/searching source codes and integrate it into your main java project. Step 4: Create a separate java class for each algorithm Step 5: Create a random function that generates at least 100000 random integer numbers from 1 to 1 million(No need to print out or store the numbers) Step 6: Insert start...

  • An array contains the following characters in the sequence below: V Z D H A Write...

    An array contains the following characters in the sequence below: V Z D H A Write down the characters for each round according to the specified sorting algorithms which sorts the character array into its ascending order. First round always begins at the first location of the array. Bubble Sort Round 1: Round 2: Round 3: Round 4: Round 5: Selection Sort Round 1: Round 2: Round 3: Round 4: Round 5: Insertion Sort Round 1: Round 2: Round 3:...

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