vortiassociates.blogg.se

Bubble sort
Bubble sort





bubble sort

To sort this array in ascending order three(n-1) iterations will be required these are: Suppose the name of the array is data and it has four elements with the following values:

bubble sort

To understand the bubble sort, a numerical example is given below. At the end of the n-1 iterations, the data is arranged in ascending order. This process is repeated n-1 times in an array of n elements. In this iteration, the second-largest value moves to the second last position in the array. The values are compared up to the n-l element of the array. In the second bubble sort iteration, the above process is repeated. Thus at the end of the first bubble sort iteration, the largest value moves to the last position in the array. This process is repeated until the last element of the array. Similarly, the value in the second element of the array is compared with the third element and these values are interchanged, if necessary. The values of the elements are interchanged if the value in the second element is greater than the value in the first element. In the first iteration, the value of the first element of the array is compared with the second element. The following explains the sorting of data in an array in ascending order using this method. To sort data in an array of n elements, n-1 iterations are required. It is used for sorting only a small amount of data. The bubble sort method is a slow process. Through the exchange of elements, the larger value slowly floats or bubbles up to the top. If one element is larger than the other, the two are exchanged. To arrange an array in ascending order, two neighboring elements are compared. The bubble sort method is used to arrange values of an array in ascending or in descending order.

  • Example write a program which sorts the data in ascending order using bubble sort algorithm in c++:īubble Sort- this is a very detail tutorial about the Bubble Sort algorithm in c / c++ and you will also learn how this algorithm works.






  • Bubble sort