Bubble Sort

Bubble Sort is probably one of the oldest, easiest, straight-forward, and inefficient sorting algorithms. It is the algorithm introduced as a sorting routine in most introductory courses on Algorithms. Bubble Sort works by comparing each element of the list with the element next to it and swapping them if required. With each pass, the largest of the list is "bubbled" to the end of the list whereas the smaller values sink to the bottom. It is similar to selection sort although not as straight forward. Instead of "selecting" maximum values, they are bubbled to a part of the list. Code:

#define max n /*where n is the array size.*/

void BubbleSort(int a[])

{

int i, j, temp;

for (i = 0; i < (max - 1); ++i)

{

for (j = 0; j < max - 1 - i; ++j )

{

if (a[j] > a[j+1])

{

temp = a[j+1];

a[j+1] = a[j];

a[j] = temp;

}

}

}

}

Key Terms:

Bubble Step - is the step in which the largest element is bubbled to its correct position. Bubble step is handled by the inner for loop.

for (j = 0; j < max - 1 - i; ++j )

{

if (a[j] > a[j+1])

{

temp = a[j+1];

a[j+1] = a[j];

a[j] = temp;

}

}

Explanation:

The first for loop:

for (i = 0; i < (max - 1); ++i)

----------------------------------------------------

‘i’ is used as a counter for the number times we are going to swap, and that since we have (max-1) elements, we can only swap less than (max-1) times. The ‘Bubble Step’:

for (j = 0; j < max - 1 - i; j++)

{

if (a[j] > a[j+1])

{

temp = a[j+1];

a[j+1] = a[j];

a[j] = temp;

}

}

---------------------------------------------------------

‘j’ is used as an index traversal of the array.

‘i’ here is used to represent the number of elements that are in the correct placement.

The for-loop condition, j<max – 1 – i, states that ‘j’ will increment until the last unsorted element which is the total number of elements (max-1) minus the number of elements that are in the correct placement (i). The if code states that if the current element, being denoted by ‘j’, is greater than the next element, you swap them. Visualization:

Examine the following table. (Note that each pass represents the status of the array after the completion of the inner for loop, except for pass 0, which represents the array as it was passed to the function for sorting) { 8 6 10 3 1 2 5 4 } pass 0

{ 6 8 3 1 2 5 4 10 } pass 1

{ 6 3 1 2 5 4 8 10 } pass 2

{ 3 1 2 5 4 6 8 10 } pass 3

{ 1 2 3 4 5 6 8 10 } pass 4

{ 1 2 3 4 5 6 8 10 } pass 5

{ 1 2 3 4 5 6 8 10 } pass 6

{ 1 2 3 4 5 6 8 10 } pass 7

Notice that even if the array is sorted at pass 4, we still reach pass 7. This is due to Bubble Sort having no code to know if the array is sorted already, (meaning ‘i’ just increment by 1 after every pass). Notice also that from pass 1, the elements are sorted from right to left. Selection Sort

The idea of Selection Sort is rather simple. It basically ‘selects’ the minimum (or maximum) of the list and swaps it with the element at the index where it’s supposed to be. CODE:

#define max n //where n is the array_size

void SelectionSort(int a[])

{

int i;

int j, min, temp;

for (i = 0; i < max - 1; ++i)

{

min=i;

for (j = i+1; j < max; ++j)

{

if (a[j] < a[min])

min = j;

}

temp = a[i];

a[i] = a[min];

a[min] = temp;

}

}

Explanation:

For the whole process, ‘min’ is used to hold the index of the smallest element. The outer loop:

for (i = 0; i < max - 1; ++i)

{

min = i;

-----------------------------------...