Basic ideas
First the first element and the second element are relatively large, assuming the first one. The two are exchanged, otherwise they are not exchanged; then the second element is compared to the third one. Assuming the second large, then the two exchange, otherwise do not exchange ... has been going on in this way. Finally the biggest element was exchanged to the last. A trip to bubble sort is complete.
Code
publicvoidBubbleSort(intint n) { int temp; for (int1; i < n; i++) { for (int1; j >= i; j--) { if1] > a[j]) { 1]; 1] = a[j]; a[j] = temp; } } }}
Performance analysis
The number of elements in the main bubble sort is independent of the initial arrangement of the elements to be sorted in the input sequence. However, the number of times the data is exchanged is related to the initial arrangement of the elements to be ordered, preferably without a single exchange. In the worst case, each time the comparison needs to be exchanged.
Bubble sort of sorting algorithm