Question

In: Computer Science

1. For each of the following lists, perform a bubble sort, and show the list after...

1. For each of the following lists, perform a bubble sort, and show the list after each exchange:

D,B,G,F,A,C,E,H

2. Explain why the bubble sort algorithm does O (n^2) comparisons on an n-element list.

Solutions

Expert Solution

Thanks for the question. Here are the answers with explanations.

=========================================================================================

Question 1

Here are the list after each exchange

D B G F A C E H   
B D G F A C E H   
A D G F B C E H    
A B F G D C E H
A B D G F C E H
A B C G F D E H
A B C F G D E H
A B C D G F E H
A B C D F G E H
A B C D E G F H
A B C D E F G H

Question 2

In bubble sort we are having two loops, the second loop is nested inside another for loop, the outer loop runs for each element in the list so if there are n elements we are iterating over n times

the inner loop runs 1 less than the current loop

so for the 1st pass the inner loop runs n-1 times

for the 2nd pass the inner loop runs for n-2 times

for the n-1th pass the inner loop runs for 1 time

for the nth pass the inner loops runs for 0 times

hence if we add all the inner loop runs we get the series like = 1+2+3+... +(n-2)+(n-1) which is n*(n-1)/2

which when simplified we get of order n^2


Related Solutions

Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
For the given array, simulate the working operation of Bubble Sort. Show your work at each...
For the given array, simulate the working operation of Bubble Sort. Show your work at each step. Make sure to show the status of the array after every swap. [ 28, 13, 22, 7, 34, 2 ]
The bubble sort described in AS 7e is inefficient when perform on a larger array. To...
The bubble sort described in AS 7e is inefficient when perform on a larger array. To improve the efficiency, we have to observe the basic mechanism of Bubble Sort. Each inner loop of the Bubble sort always move the largest item on the unsorted left side to the sorted right side. When a list is sorted, we say the list is in ascending order by convention. We can enhance the efficiency of traditional Bubble sort by making only one swapping...
Given the following array [17, 15,21,208,16,122,212,53,119,43] Apply bubble sort algorithm and show the status of the...
Given the following array [17, 15,21,208,16,122,212,53,119,43] Apply bubble sort algorithm and show the status of the array after each pass. Also calculate how many comparisons you will be required to pass
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from the current directory a list of intergers (10 numbers to be exact), and the sort them, and print them to the screen. You can use redirection to read data from a given file through standard input, as opposed to reading the data from the file with the read API (similar to Lab #1). You can assume the input data will only have 10 numbers...
I need to write a method that sorts a provided Linked list with bubble sort and...
I need to write a method that sorts a provided Linked list with bubble sort and using ONLY Java List Iterator Methods (Link Below) https://docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html     public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception {     // first line to start you off     ListIterator<E> iit = c.listIterator(), jit;     /**** Test code: do not modify *****/     List cc = new LinkedList(c);     Collections.sort(c);     ListIterator it1 = c.listIterator(), it2 = cc.listIterator(); while (it1.hasNext()) { if (!it1.next().equals(it2.next()))         throw new Exception("List not sorted");...
Question III: Programming\flowcharts\algorithms Sort the following list from the smallest to the largest using the bubble...
Question III: Programming\flowcharts\algorithms Sort the following list from the smallest to the largest using the bubble sort algorithm (show all passes) [4 points] 20 10 30 5 0 Write a python program that asks the user to enter the number of seconds. The program will print the whole hours, whole minutes and remaining seconds. For example: if the user entered the number of seconds as 3663 then the output will be: 1 hour(s), 1 minute(s), 3 second(s). [5 points] Note:...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
use quick sort to sort the following array. show each pass and what the pivot is...
use quick sort to sort the following array. show each pass and what the pivot is during the pass. please explain why you are swapping positions. do not use a compiler. 30 5 40 11 20 9 15 2 60 25 80 3 73 35 4 75 20 6
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT