|
Bubble Sort
|
|
Bubble Sort is an
elementary sorting algorithm. It works by repeatedly
exchanging adjacent elements, if necessary. When no
exchanges are required, the data is sorted.
|
|
ALGORITHM : BUBBLE
SORT
|
|
Procedure
BubbleSort(A, MAX) : Here A is an array
consisting of MAX elements. This procedure sorts the
elements in Ascending Order by repeatedly exchanging
adjacent elements, if necessary. Here 'temp' is a
temporary variable used to exchange the adjacent
elements in this procedure.
ASCENDING ORDER
for i ← 1 to MAX
do for j ← MAX downto i+1
do If
A[j] <
A[j-1]
then
Exchange A[j] ↔
A[j-1]
DESCENDING ORDER
for i ← 1 to MAX
do for j ← MAX downto i+1
do If
A[j] > A[j-1]
then
Exchange A[j] ↔
A[j-1]
|
|
Source Code
|
|
|
|
Complete Source code of Stack
written in C.
|
|
Insertion Sort
|
|
If the first few objects are
already sorted, an unsorted object can be inserted in
the sorted set in proper place. This is called insertion
sort. An algorithm consider the elements one at a time,
inserting each in its suitable place among those already
considered (keeping them sorted).
|
Procedure InsertionSort(A, MAX) : Here A is an array
consisting of MAX elements. This procedure sorts the
elements in Ascending Order.
|
|
ASCENDING ORDER
|
For j = 2 to length [A]
do key = A[j] Put
A[j] into the
sorted sequence A[1 . . j-1]
i = j-1 while i > 0 and
A[i] > key
do
A[i+1] = A[i]
i = i-1 A[i+1] =
key
|
|
Insertion Sort - Ascending
Order
|
|
To learn more about Shell
Sort
|
|
Back
|
|
Next
|
|