CouldnoT Posted August 22, 2020 Share Posted August 22, 2020 Bubble sort The Bubble Sort algorithm is simple, inefficient sorting algorithm. It is not recommended for use, since its performance at sorting a list of items is terribly slow. It is best at sorting a small list of items, but not for large ones. In bubble sort algorithm, a data array is read, and the first value is compared with the second, if it is greater, it is swapped with the second value. Since the swap cannot be done without losing data in one of the variable spaces, a temporary variable is created to temporarily hold the content of one of the values while the swap is carried out. Lets look at an example. Suppose you have a row of blocks with letters on them in random order and you wish to arrange them in alphabetical order from left to right. G A Z C S Begin with the first block (array item position 1). in this case, letter G. If the block to the right of it (i.e. array item position 2) should come before it, swap them so that they are in order. i.e. A G Z C S If you were doing this by hand, you might just pick the blocks to be moved with one in each hand and cross your arms to swap them. Or you might move the first one in position a1 out of it’s position temporarily (and store it in a temporary position, say temp) and then move the second one in position a2 to the location of a1 and then finally move the value in temp (which of course was originally the value in a1) to a2. Now repeat the same steps, this time between the second (a2) and the third (a3) item in the array. A C Z G S Then again A C G Z S And again A C G S Z And that’s it. Now the code for sorting an array of 10 numeric characters is shown below : Procedure BubbleSort(numbers : Array of Integer; size : Integer); Var i, j, temp : Integer; Begin For i := size-1 DownTo 1 do For j := 2 to i do If (numbers[j-1] > numbers[j]) Then Begin temp := numbers[j-1]; numbers[j-1] := numbers[j]; numbers[j] := temp; End; End. 1 Link to comment Share on other sites More sharing options...
TLG George Posted August 25, 2020 Share Posted August 25, 2020 (edited) Topic inactiv. Closed! Felicitari pentru munca depusa! Edited August 25, 2020 by TLG Grizzly Link to comment Share on other sites More sharing options...
Recommended Posts