There are several other sorting algorithms besides selection sort that makesense at your level of programming knowledge. Unfortunately, those algorithms donot offer any advantages over selection sort in terms of their algorithmperformance. Even so, coding these algorithms gives you more practice usingarrays.For example, you can sort an integer array from lowest to highest by applyingthe following procedure. Start by going through the array, looking at adjacent pairsof values. If the values forming the pair are correctly ordered, do nothing; if thevalues are out of order, swap them. In either case, move on the next pair ofvalues. The pairs overlap as you move through the list so that the second elementin one pair becomes the first element of the next pair. Repeat this operation untilyou make a complete pass in which you do not need to exchange any integers.This algorithm is called bubble sort, because the values seem to “bubble up” totheir eventual positions.Reimplement the SortIntegerArray function using the bubble sort algorithm.
时间: 2024-02-14 20:09:24 浏览: 315
Sure, I can help you with that. Here's the implementation of the SortIntegerArray function using the bubble sort algorithm:
```
void SortIntegerArray(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
```
This function takes an integer array and its size as input, and then sorts the array using the bubble sort algorithm. The outer loop runs from 0 to size-1, and the inner loop runs from 0 to size-i-1. In each iteration of the inner loop, we compare adjacent pairs of values and swap them if they are out of order. This process is repeated until we make a complete pass in which we do not need to exchange any integers.
I hope this helps! Let me know if you have any further questions.
阅读全文