Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Bubble sort is a simple sorting algorithm that processes the elements of an array on multiple runs. At each run, the algorithm compares adjacent elements

Bubble sort is a simple sorting algorithm that processes the elements of an array on multiple
runs. At each run, the algorithm compares adjacent elements and swaps them if they are
not in order. Its name suggests that the larger elements bubble to the end of the array.
Take an array of numbers "51428", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required;
First Pass
(51428)->(15428), Here, algorithm compares the first two elements, and swaps since 5>1.
(15428)->(14528), Swap since 5>4
(14528)->(14258), Swap since 5>2
(14258)->(14258), Now, since these elements are already in order (8>5), algorithm does not swap them.
Second Pass
(14258)->(14258)
(14258)->(12458), Swap since 4>2
(12458)->(12458)
(12458)->(12458)
Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one additional whole pass without any swap to know it is sorted.
Third Pass
(12458)->(12458)
(12458)->(12458)
(12458)->(12458)
(12458)->(12458)
Implement in c0 the function for sorting the array using BubbleSort algorithm in a file bubblesort.c0 with same interface of selsort.c0, and put all contracts related to preconditions, postconditions and loop invariants.
Implement a testing program in c0 by using sort-test.c0, and try to use the algorithm on arrays of various size to understand the complexity.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Apache Cassandra 3 X An Expert Guide To Improving Database Scalability And Availability Without Compromising Performance

Authors: Aaron Ploetz ,Tejaswi Malepati ,Nishant Neeraj

3rd Edition

1789131499, 978-1789131499

More Books

Students also viewed these Databases questions