Anda di halaman 1dari 3

Selection Sorting..

Selection sort carries out a sequence of passes over the table. At the first pass an entry
is selected on some criteria and placed in the correct position in the table. The possible
criteria for selecting an element are to pick the smallest or pick the largest. If the
smallest is chosen then, for sorting in ascending order, the correct position to put it is
at the beginning of the table. Now that the correct entry is in the first place in the table
the process is repeated on the remaining entries. Once this has been repeated n-1 times
the n-1 smallest entries are in the first n-1 places which leaves the largest element in
the last place. Thus only n-1 passes are required. The algorithm can be described as
follows:
for i from 1 to n-1
{
Find smallest element in ith to nth entries.
Exchange this element with ith entry.
}

For example consider the following example of selection sort being carried out on a
sample set of data:
9 2 5 7 4 8 on pass 1 look for smallest in 1st to 6th
swap 2nd with first giving
2 9 5 7 4 8 on pass 2 look for smallest in 2nd to 6th
swap 5th with second giving
2 4 5 7 9 8 on pass 3 look for smallest in 3rd to 6th
swap 3rd with third giving
2 4 5 7 9 8 on pass 4 look for smallest in 4th to 6th
swap 4th with fourth giving
2 4 5 7 9 8 on pass 5 look for smallest in 5th to 6th
swap 5th with 6th giving
2 4 5 7 8 9 sorted.

The algorithm is now written in C++. Of course the limits 1 to n-1 in the above now
become 0 to n-2 because array subscripts start at 0.
for (i = 0; i < n-1; i++)
{
// find smallest entry in ith to n-1 th place
// p is subscript of smallest entry yet found
p = i;
for (j = i+1; j < n; j++)
if (a[j]<a[p])
p = j;
// exchange pth element with ith element
t = a[p];
a[p] = a[i];
a[i] = t;
}

Bubble sort
The simplest sorting algorithm is bubble sort. The bubble sort works by iterating down an array to be
sorted from the first element to the last, comparing each pair of elements and switching their positions
if necessary. This process is repeated as many times as necessary, until the array is sorted. Since the
worst case scenario is that the array is in reverse order, and that the first element in sorted array is
the last element in the starting array, the most exchanges that will be necessary is equal to the length
of the array. Here is a simple example:

Given an array 23154 a bubble sort would lead to the following sequence of partially sorted arrays:
21354, 21345, 12345. First the 1 and 3 would be compared and switched, then the 4 and 5. On the
next pass, the 1 and 2 would switch, and the array would be in order.

The basic code for bubble sort looks like this, for sorting an integer array:

for(int x=0; x<n; x++)

for(int y=0; y<n-1; y++)

if(array[y]>array[y+1])

int temp = array[y+1];

array[y+1] = array[y];

array[y] = temp;

}
Insertion sorting

The insertion sort maintains the two sub-arrays within the same array. At the
beginning of the sort, the first element in the first sub-array is considered the "sorted
array". With each pass through the loop, the next element in the unsorted second sub-
array is placed into its proper position in the first sorted sub-array.

The insertion sort can be very fast and efficient when used with smaller arrays.
Unfortunately, it loses this efficiency when dealing with large amounts of data.

// Insertion Sort Function for Descending Order


void InsertionSort( apvector <int> &num)
{
int i, j, key, numLength = num.length( );
for(j = 1; j < numLength; j++) // Start with 1 (not 0)
{
key = num[j];
for(i = j - 1; (i >= 0) && (num[i] < key); i--) // Smaller values move up
{
num[i+1] = num[i];
}
num[i+1] = key; //Put key into its proper location
}
return;
}