Knowee
Questions
Features
Study Tools

Bubble Sort is a popular sorting algorithm. It works by repeatedly swapping adjacent elements thatare out of order.a) Write the pseudo code of the bubblesort algorithmb) Implement the bubblesort algorithm.c) Read 8 numbers from the keyboard and store them in an array. Sort the numbers usingthe bubble sort algorithm.

Question

Bubble Sort is a popular sorting algorithm. It works by repeatedly swapping adjacent elements thatare out of order.a) Write the pseudo code of the bubblesort algorithmb) Implement the bubblesort algorithm.c) Read 8 numbers from the keyboard and store them in an array. Sort the numbers usingthe bubble sort algorithm.

🧐 Not the exact question you are looking for?Go ask a question

Solution

a) Pseudo code for Bubble Sort Algorithm:

procedure bubbleSort(list : array of items)
   n = length(list)
   repeat 
      swapped = false
      for i = 1 to n-1 inclusive do
         /* if this pair is out of order */
         if list[i-1] > list[i] then
            /* swap them and remember something changed */
            swap( list[i-1], list[i] )
            swapped = true
         end if
      end for
   until not swapped
end procedure

b) Implementation of Bubble Sort Algorithm in Python:

def bubbleSort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1] :
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr

c) Reading 8 numbers from the keyboard, storing them in an array and sorting them using the bubble sort algorithm:

arr = []
for i in range(8):
    num = int(input("Enter number: "))
    arr.append(num)

sorted_arr = bubbleSort(arr)
print("Sorted array is:", sorted_arr)

In the above code, we first read 8 numbers from the user and store them in an array. Then we call the bubbleSort function to sort the array. Finally, we print the sorted array.

This problem has been solved

Similar Questions

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. The problem with bubble sort is its worst case scenario. When the smallest element is in the last position, then it takes more time to sort in ascending order, but takes less time to sort in descending order.An array is called beautiful if all the elements of the array are in either ascending or descending order. Given an array of numbers, find the minimum swap operations required to make the array beautiful.

How does Bubble Sort work?By selecting the smallest element and swapping it to the front.By repeatedly swapping adjacent elements if they are in the wrong order.By dividing the array and merging it back together in sorted order.By finding the median and partitioning the array around it.

Rearrange the below algorithm for Bubble Sort.Input: A is the list of elements and n is the size of the listOutput: A1, A2,...,An, arranged in increasing orderif A[j] >A[ j+1 ]for j = 0 to n-i-1end bubbleSortprocedure bubbleSort(A,n)swap a[j] <-> A[j+1]for i = 0 to n-1

Write a description of bubble sort in programming, give the steps to follow, keep it short

Design a Java program that allows users to input a series of integers into an array and then sorts the array in ascending order. Implement the sorting algorithm using the bubble sort technique.Write a Java code that accomplishes the following tasks:Prompt the user to enter the number of elements in the array.Prompt the user to input each element of the array.Sort the array in ascending order using the bubble sort algorithm.Display the sorted array.Demonstrate the functionality of your program with a sample input/output scenario.Sample Input12 // No of elements 23 // Elements to store in an array674512900260725434129865Sample Output12 12 23 34 45 54 65 67 72 98 260 900

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.