245,882 views
7 votes
7 votes
One:

Starting at the second element compare it to the value to the left (the first element) if the value of the second element is smaller than the first element swap the values.
Starting at the third element compare it to the value to the left (the second element). If the value of the third element is not smaller than the value to the left of it go to the next step. Otherwise swap the values and now compare that same value to the value to the left (the first element). If the value of the element is not smaller than the value to the left of it go to the next step. Otherwise swap and then go the the next step.

Repeat this process until you reach the end of the array. The array is now sorted.
Write the pseudo code then write the program in Java using Eclipse.

User Procrade
by
2.8k points

1 Answer

20 votes
20 votes

Answer:

Introduction

Sometimes, data we store or retrieve in an application can have little or no order. We may have to rearrange the data to correctly process it or efficiently use it. Over the years, computer scientists have created many sorting algorithms to organize data.

In this article we'll have a look at popular sorting algorithms, understand how they work and code them in Python. We'll also compare how quickly they sort items in a list.

For simplicity, algorithm implementations would be sorting lists of numbers in ascending order. Of course, you're free to adapt them to your need.

Bubble Sort

This simple sorting algorithm iterates over a list, comparing elements in pairs and swapping them until the larger elements "bubble up" to the end of the list, and the smaller elements stay at the "bottom".

Explanation

We begin by comparing the first two elements of the list. If the first element is larger than the second element, we swap them. If they are already in order we leave them as is. We then move to the next pair of elements, compare their values and swap as necessary. This process continues to the last pair of items in the list.

Upon reaching the end of the list, it repeats this process for every item. Though, this is highly inefficient. What if only a single swap needs to be made in the array? Why would we still iterate though it n^2 times, even though it's already sorted?

Obviously, to optimize the algorithm, we need to stop it when it's finished sorting, otherwise it'll reevaluate an already sorted array many times.

Step-by-step explanation:

User Matt Swanson
by
2.7k points