Sign in Join
Wiki copytemporary

Bubble sort: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Bubble sort

OldNewLine
1+Bubble sort is a simple [[sorting algorithm]].
2+
3+## Characteristics
4+
5+- Very slow
6+- Suitable only for small quantities sorting
7+- Fast only when the array is nearly sorted
8+
9+## Unit UBubbleSort
10+
11+``` pascal
12+unit UBubbleSort;
13+
14+interface
15+
16+type
17+ // data type
18+ TItemBubbleSort=integer;
19+
20+procedure BubbleSort( var a: array of TItemBubbleSort );
21+
22+implementation
23+
24+procedure swap( var a, b:TItemBubbleSort );
25+var
26+ temp : TItemBubbleSort;
27+begin
28+ temp := a;
29+ a := b;
30+ b := temp;
31+end;
32+
33+procedure BubbleSort( var a: array of TItemBubbleSort );
34+var
35+ n, newn, i:integer;
36+begin
37+ n := high( a );
38+ repeat
39+ newn := 0;
40+ for i := 1 to n do
41+ begin
42+ if a[ i - 1 ] > a[ i ] then
43+ begin
44+ swap( a[ i - 1 ], a[ i ]);
45+ newn := i ;
46+ end;
47+ end ;
48+ n := newn;
49+ until n = 0;
50+end;
51+
52+end.
53+```
54+
55+## Example of usage
56+
57+``` pascal
58+uses
59+ UBubbleSort
60+ ...
61+
62+var
63+ a: array[0..100] of integer;
64+begin
65+ ...
66+ BubbleSort(a);
67+ ...
68+```
69+
70+## See also
71+
72+- \[<https://www.youtube.com/watch?v=DXO-nqATduU>\| COMP1 Revise & Learn : Bubble Sort\]
73+
74+---
75+*From the Free Pascal wiki, page "Bubble sort", as it stood on 2021-01-27 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history