Write c++ program. Use Pointers to write a function to bubble sort an integer array. Input format. Specifies the length of the sequence and enters the sequence. Output format. Output in order from smallest to largest
时间: 2024-02-06 11:02:45 浏览: 103
Sure, here's the C++ program to bubble sort an integer array using pointers:
```c++
#include <iostream>
using namespace std;
void bubbleSort(int *arr, int n) {
for(int i=0; i<n-1; i++) {
for(int j=0; j<n-i-1; j++) {
if(*(arr+j) > *(arr+j+1)) {
int temp = *(arr+j);
*(arr+j) = *(arr+j+1);
*(arr+j+1) = temp;
}
}
}
}
int main() {
int n;
cout << "Enter the length of the sequence: ";
cin >> n;
int arr[n];
cout << "Enter the sequence: ";
for(int i=0; i<n; i++) {
cin >> *(arr+i);
}
bubbleSort(arr, n);
cout << "The sorted sequence is: ";
for(int i=0; i<n; i++) {
cout << *(arr+i) << " ";
}
return 0;
}
```
In this program, we first take input the length of the integer array and then the sequence itself. We pass this array and its length to the bubbleSort() function that implements the bubble sorting algorithm using pointers. Finally, we output the sorted sequence.
阅读全文