Bubble sort: Difference between revisions
From Lazarus wiki
Jump to navigationJump to search
m (→Features) |
(rename section →Features: to →Characteristics: as “slow”/etc. is usually not a selling point) |
||
(6 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{Bubble sort}} | |||
Bubble sort is a simple sorting algorithm. | Bubble sort is a simple [[sorting algorithm]]. | ||
== | == Characteristics == | ||
* Very slow | * Very slow | ||
Line 8: | Line 9: | ||
* Fast only when the array is nearly sorted | * Fast only when the array is nearly sorted | ||
== | == Unit UBubbleSort == | ||
<syntaxhighlight lang="pascal"> | |||
unit UBubbleSort; | unit UBubbleSort; | ||
interface | interface | ||
Line 54: | Line 53: | ||
until n = 0; | until n = 0; | ||
end; | end; | ||
end. | end. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Example of | == Example of usage == | ||
<syntaxhighlight lang="pascal"> | |||
uses | uses | ||
UBubbleSort | UBubbleSort | ||
... | ... | ||
var | var | ||
a: array[0..100] of integer; | a: array[0..100] of integer; | ||
begin | begin | ||
... | ... | ||
BubbleSort(a); | |||
BubbleSort( a ); | ... | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== See also == | == See also == | ||
* [https://www.youtube.com/watch?v=DXO-nqATduU| COMP1 Revise & Learn : Bubble Sort] | * [https://www.youtube.com/watch?v=DXO-nqATduU| COMP1 Revise & Learn : Bubble Sort] | ||
Latest revision as of 21:47, 27 January 2021
│
English (en) │
suomi (fi) │
français (fr) │
Bubble sort is a simple sorting algorithm.
Characteristics
- Very slow
- Suitable only for small quantities sorting
- Fast only when the array is nearly sorted
Unit UBubbleSort
unit UBubbleSort;
interface
type
// data type
TItemBubbleSort=integer;
procedure BubbleSort( var a: array of TItemBubbleSort );
implementation
procedure swap( var a, b:TItemBubbleSort );
var
temp : TItemBubbleSort;
begin
temp := a;
a := b;
b := temp;
end;
procedure BubbleSort( var a: array of TItemBubbleSort );
var
n, newn, i:integer;
begin
n := high( a );
repeat
newn := 0;
for i := 1 to n do
begin
if a[ i - 1 ] > a[ i ] then
begin
swap( a[ i - 1 ], a[ i ]);
newn := i ;
end;
end ;
n := newn;
until n = 0;
end;
end.
Example of usage
uses
UBubbleSort
...
var
a: array[0..100] of integer;
begin
...
BubbleSort(a);
...