Answered step by step
Verified Expert Solution
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 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
Here, algorithm compares the first two elements, and swaps since
Swap since
Swap since
Now, since these elements are already in order algorithm does not swap them.
Second Pass
Swap since
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
Implement in c the function for sorting the array using BubbleSort algorithm in a file bubblesort.c with same interface of selsort.c and put all contracts related to preconditions, postconditions and loop invariants.
Implement a testing program in c by using sorttest.c 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started