Compare the first and second items if they are out of order


SORTING:

An array is said to be ordered if its values are either ascending or descending order. In an ascending ordered array, the value of each element is less than or equal to the value of the next element. That is, [each element] <= [next element] .

A sort is an algorithm for ordering an array. Of the many different techniques for sorting an array we disbuss the bubble sort. .

It recwires the swapping of values sorted in a pair of variables. If varl, var2, and temp are all variables of the same type, then the statements
temp =varl
varl=var2
var2=temp

Assign varl's value to var2 and var2's value to varl.

Bubble Sort: The bubble sort is an algorithm that compares adjacent items and swaps those that are out of order. If this process repeated enough times , the list will be ordered.

Let's carry out this process on the list.

The steps for each pass through the list are as follows:

1. Compare the first and second items. If they are out of order, swap them.

2. Compare the second and third item. If they are out of order, swap them.

3.Repeat this pattern for all remaining pairs.

The first time through the list, this process is repeated to the enoreffirelist. This is called first pass. After the first pass the last item will be in its proper position. Therefore, the second pass does not have to consider it and so requires one less comparison. At the end of the second pass, the last two items will be in the proper position. Each successive pass requires one less comparison. After four passes, the last four items will be in their proper positions, and hence, the first will be also.

As a specific example of this process, consider the list of numbers: 21 13 17 5 3

This rocess corn letes the ust a ass through the data and results in the largest number moving to the bottom of the list. As the lamest value sinks to its resting place at the bottom o the list the smaller elements slowl rise or "bubble" to the to' o the list. This bubbling effect of the smaller elements is what Rave rise to the name "bubble" sort for this sorting algorithm.'

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Compare the first and second items if they are out of order
Reference No:- TGS02386469

Now Priced at $10 (50% Discount)

Recommended (97%)

Rated (4.9/5)