Programming : Sorting (Bubble Sort)

8/3/13

Ordering is the process of arranging a set of objects in a particular order or arrangement. The object can be an ascending sequence (Ascending) or decreased (Descending). The sorted data  has many advantages, among others:
1. Speed ​​up the search process
2. Can be easily determined maximum and minimum ordering process
There is a need to bring a variety of sorting methods which aim to obtain optimal sorting method.

Sorting algorithms which are widely recognized among others:
1. Bubble Sort (Sorting Bubble)
2. Insertion Sort (Sorting Insert)
3. Maximum / Minimum Sort
4. Heap Sort
5. Shell Sort
6. Quick Sort
7. Merge Sort
8. Radix Sort
9. Tree Sort


For all the sorting algorithms that will be discussed later using the type Array (Array),
which is defined as follows:
{Declaration}
Const NMaks = 100 //Maximum Number of elements {Array}
Type LarikInt = array [1 ... NMaks] of integer


In this post, we will discuss only three sorting methods, namely:
1. Ordering Bubbles (Bubble Sort) 
Ordering bubble inspired by soap bubbles are above the water surface. Because soaps are lighter specific gravity than water density, it will always be soap bubbles floating on the surface. The principle of flotation is used in bubble sort. Array elements are the most precious little "float", meaning lift up (or to the left end of the array) through a process of exchange, if it will do sorting enlarged, if ordering smaller otherwise.



The flotation process is done as much as N times steps. At the end of each step to-i, L Array [1 .. N] will consist of two parts, the parts that have been ordered, ie L [1 .. i] and the part that has not been ordered, ie L [i +1 .. N]. After the last step, obtained Array L [1 .. N] are ordered ascending

To get the sorted array ascending, the process is carried out at each step is as follows:

Step 1: 
Start of the elements k = N, N-1, ... 2,
compare L [k] with L [k-1]. If L [k] <L [k-1],
interchange L [k] with L [k-1].
At the end of step 1, the elements of L [1] contains the first minimum price.

Step 2: 
Start of the elements k = N, N-1, ..., 3,
compare L [k] with L [k-1]. If L [k] <L [k-1],
interchange L [k] with L [k-1].
At the end of step 2, the element L [2] contains a minimum price to the two and the array L [1 .. 2] sequences

Step 3:
Start of the elements k = N, N-1, ..., 4,
compare L [k] with L [k-1]. If L [k] <L [k-1],
interchange L [k] with L [k-1].
At the end of step 3, the element L [3] contains a minimum price to the two and the array L [1 .. 3] ordered

Step N-1:
Beginning of element k = N,
compare L [k] with L [k-1]. If L [k] <L [k-1],
interchange L [k] with L [k-1].
At the end of step N-1, the element L [N-1] contains a minimum price to the N-1 and the array L [1 .. N-1] ordered ascending (remaining element is L [N], does not need to be sorted out because it is only one only)

To understand more look this example
Example: Given an array with N = 6 pieces Ascending elements not, this array will be sorted ascending. Element of the array is 25, 27, 10, 8, 76, 21

The algorithm is as follows:
Procedure BubleSort (Input / output L: LarikInt, Input N: Integer)
{
K. Early  : Element Array L has been defined
K. End    : Element Array ascending sequences L such that L [1] ≤ L [2] ≤ ... ≤ L [N]

Process   : Sorting array elements with L so that the sequential ascending Bubble Sort method declaration}

i              : integer {counter for the number of steps}
k             : integer {enumerator for each step flotation pd}
Temp      : integer {variables help to exchange}

For i ← 1 to N-1 do      for k ← N downto i +1 do         if L [k] <L [k-1] then            {Swap L [k] with L [k-1]}            Temp ← L [k ]            L [k] ← L [k-1]            L [k-1] ← Temp         endif      endfor endfor
To obtain an ordered array of steps declined done otherwise. If the note sorting with Buble Sort method is inefficient for sorting arrays have large size, because the number of exchange operations performed at each step. But this method is simple and easy to understand.

No comments:

 

Tags