|
|
|
|
|
|
Array |
|
Sort an array |
|
Make all elements in an array in ascending or
descending order |
|
Sort algorithm |
|
Selection Sort |
|
Bubble Sort |
|
|
|
|
|
|
|
|
|
|
In a selection sort, the smallest value in a
list is initially selected from the complete list of data and exchanged
with the first element in the list. |
|
After this first selection and exchange, the
next smallest element in the revised list is selected and exchanged with
the second element in the list. |
|
Since the smallest element is already in the
first position in the list, this second pass need only consider the second
through the last elements. |
|
|
|
|
|
|
We will implement three functions |
|
void swap(int& x, int& y); |
|
int smallestElement(int list[], int size, int
startIndex); |
|
int SelectionSort(int list[], int size); |
|
|
|
|
// swap the values of two integer variable |
|
void swap(int& x, int& y) { |
|
int temp; |
|
temp = x; |
|
x = y; |
|
y = temp; |
|
} |
|
|
|
|
|
|
// return the index of the smallest value |
|
// startIndex indicates the starting element |
|
int smallestElement(int list[], int size, int
startIndex) { |
|
int smallIndex = startIndex; |
|
|
|
for (int i = startIndex + 1; i < size;
i++) { |
|
if (list[smallIndex] > list[i]) |
|
smallIndex = i; |
|
} |
|
return smallIndex; |
|
} |
|
|
|
|
|
|
// This function will sort a given array in
ascending order |
|
// and it will also return how many exchanges
needed in the process |
|
int SelectionSort(int list[], int size) { |
|
int move = 0; |
|
int smallIndex; |
|
for(int i = 0; i < size; i++) { |
|
smallIndex = smallestElement(list, size,
i); |
|
if ( smallIndex != i) { |
|
move++; |
|
swap(list[i], list[smallIndex]); |
|
} |
|
} |
|
return move; |
|
} |
|
|
|
|
|
|
int list[]= {690, 307, 32, 155, 426}; |
|
cout << "The moves :" <<
SelectionSort(list,5) <<
endl; |
|
for(int i = 0; i < 5; i++) |
|
cout << list[i] << " " ; |
|
cout << endl; |
|
|
|
|
|
|
In a Bubble Sort, we need N-1 passes. |
|
On each pass we visit each element, and check
whether that element and its immediate neighbor are in order. If they are
out of order we switch them before going on. After the first pass through
the list at least the largest element will be put in its proper place.
After the second pass, at least the largest two elements will be in the
right place, and so on. We repeat the process until the whole list is
sorted. |
|
|
|
|
Exchange adjacent pairs of elements that are
out of order |
|
Largest value “sinks” to the bottom each
pass, while smaller values gradually “bubble” up to the top |
|
|
|
|
|
|
|
Initial array(list) : 690 307
32 155 426 |
|
Pass 1 |
|
690 307 | 32
155 426 |
|
307 690 32 |
155 426 |
|
307
32 690 155 | 426 |
|
307
32 155 690
426 | |
|
307
32 155 426
690 |
|
Pass 2 |
|
307 32
| 155 426 690 |
|
32 307
155 | 426 690 |
|
32 155
307 426 690 (already in order) |
|
|
|
|
// This function will sort a given array in
ascending order |
|
// and it will also return how many exchanges
made in the process |
|
int BubbleSort(int list[], int size) { |
|
int move = 0; // move will record how
many exchanges needed |
|
// totally, there will be size -1 passes |
|
for(int i = 0; i < size -1; i++) { |
|
//in each pass, there will be size-1
comparison |
|
for(int j =1; j < size; j++) { |
|
// check if exchange is needed |
|
if (list[j] < list[j-1]){ |
|
swap(list[j], list[j-1]); |
|
move++; } |
|
} |
|
} |
|
return move; |
|
} |
|
|
|
|
|
|
int list[]= {690, 307, 32, 155, 426}; |
|
cout << "The moves :" <<
BubbleSort(list,5) << endl; |
|
for(int i = 0; i < 5; i++) |
|
cout
<< list[i] << "
" ; |
|
cout << endl; |
|
|
|