Bubble sort is the simplest sorting algorithm. It can be viewed as the lighter values coming up step by step.
Bubble sort works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
Bubble sort works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
The code below implements the bubble sort.
typedef vector <int> IntVect;
void SwapInts(int & A, int & B) { int temp = A; A = B; B = temp; } void BubbleSort(IntVect & A) { for (int i = A.size() - 1; i >= 0; i--) { for (int j = 0; j < i; j++) { if (A[j] > A[j + 1]) { SwapInts(A[j], A[j + 1]); } } } } |
No comments:
Post a Comment