Question

In: Computer Science

Bubble sort for 12, 2, 3, 21, 11, 10,8

Bubble sort for 12, 2, 3, 21, 11, 10,8

Solutions

Expert Solution

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

First Pass:

(12, 2, 3, 21, 11, 10,8) -> ( 2, 12, 3, 21, 11, 10, 8)  ,algorithm compares the first two elements, and swaps since 12>2.

( 2, 12, 3, 21, 11, 10, 8) -> ( 2, 3, 12, 21, 11, 10, 8)  Swap since 12 > 3.

( 2, 3, 12, 21, 11, 10, 8) ->  ( 2, 3, 12, 21, 11, 10, 8)   Since these elements are already in order (21> 12), algorithm does not swap them.

( 2, 3, 12, 21, 11, 10, 8) -> ( 2, 3, 12, 11, 21, 10, 8) Swap since 21> 11

. ( 2, 3, 12, 11, 21, 10, 8) -> ( 2, 3, 12, 11, 10, 21, 8)  Swap since 21> 10

( 2, 3, 12, 11, 10, 21, 8)-> ( 2, 3, 12, 11, 10, 8, 21) Swap since 21> 8

Second Pass:

( 2, 3, 12, 11, 10, 8, 21) -> ( 2, 3, 12, 11, 10, 8, 21) Don't swap since 3>2

( 2, 3, 12, 11, 10, 8, 21) -> ( 2, 3, 12, 11, 10, 8, 21)   Don't swap since 12>3

( 2, 3, 12, 11, 10, 8, 21) ->  ( 2, 3, 11, 12, 10, 8, 21) Swap since 12>11

( 2, 3, 11, 12, 10, 8, 21) -> ( 2, 3, 11, 10, 12, 8, 21) Swap since 12>10

( 2, 3, 11, 10, 12, 8, 21) -> ( 2, 3, 11, 10, 8, 12, 21) Swap since 12>8

Third Pass:

( 2, 3, 11, 10, 8, 12, 21) ->( 2, 3, 11, 10, 8, 12, 21) Don't swap

(2, 3, 11, 10, 8, 12, 21) -> (2, 3, 11, 10, 8, 12, 21) Don't swap

(2, 3, 11, 10, 8, 12, 21) -> (2, 3, 10, 11, 8, 12, 21) Swap since 11>10

(2, 3, 10, 11, 8, 12, 21) -> (2, 3, 10, 8, 11, 12, 21) Swap since 11>8

Fourth Pass:

(2, 3, 10, 8, 11, 12, 21) -> (2, 3, 10, 8, 11, 12, 21)   Don't swap

(2, 3, 10, 8, 11, 12, 21) -> (2, 3, 10, 8, 11, 12, 21) Don't swap

(2, 3, 10, 8, 11, 12, 21) -> (2, 3, 8, 10, 11, 12, 21) Don't swap

(2, 3, 8, 10, 11, 12, 21) -> (2, 3, 8, 10, 11, 12, 21)   Don't swap

(2, 3, 8, 10, 11, 12, 21) -> (2, 3, 8, 10, 11, 12, 21)   Don't swap

Sorted Array will be: (2, 3, 8, 10, 11, 12, 21)


Related Solutions

Bubble sort for 12, 2, 3, 21, 11, 10,8
Bubble sort for 12, 2, 3, 21, 11, 10,8
1. Insertion sort for 12, 2, 3, 21, 11, 10,8 2. Bubble sort for 12, 2,...
1. Insertion sort for 12, 2, 3, 21, 11, 10,8 2. Bubble sort for 12, 2, 3, 21, 11, 10,8 3. selection sort for 12, 2, 3, 21, 11, 10,8 analysis of algorithm
selection sort for 12, 2, 3, 21, 11, 10,8
selection sort for 12, 2, 3, 21, 11, 10,8
selection sort for 12, 2, 3, 21, 11, 10,8
selection sort for 12, 2, 3, 21, 11, 10,8
Analysis of Algorithims Bubble sort for 12, 2, 3, 21, 11, 10,8 Binary search for K=12...
Analysis of Algorithims Bubble sort for 12, 2, 3, 21, 11, 10,8 Binary search for K=12 in the array A={2, 3, 5, 7,11,15, 16,18,19} selection sort for 12, 2, 3, 21, 11, 10,8
Insertion sort for 12, 2, 3, 21, 11, 10,8 java lanuage
Insertion sort for 12, 2, 3, 21, 11, 10,8 java lanuage
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Bubble sort, show work. Write...
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Bubble sort, show work. Write the algorithm.
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...
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge...
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT