11A Simple C Programming Sortiong Array Simple C Programming
11A. Simple C++ Programming Sortiong Array
Simple C++ Programming Sortiong Array//sorts an array of integers using Bubble Sort #include using namespace std; const int SIZE = 10; void bubblesort(int arr [], int length); int main() {int arr[SIZE]; for (int i = 0; i 0; i--) {for (int j = 0; j arr[j + l]) {//swap the numbers int temp = arr [j + l]; arr[j + 1] = arr [j]; arr[j] = temp;}}}} Compile using the -std = c++0x flag and run the program to verify that it sorts 10 integers entered by the user in ascending order. With a little modification, we can arrange the integers in this program in descending order. Modify the program so that it sorts the integers entered by the user in descending order instead of ascending order. You may assume that the user enters integers when prompted.Solution
#include <iostream>
using namespace std;
const int SIZE=10;
void bubblesort(int arr[],int length);
int main()
{
int arr[SIZE];
for(int i=0;i<SIZE; i++){
cout<<\"Enter an integer: \";
cin>>arr[i];
}
bubblesort(arr,SIZE);
for(int i=0;i<SIZE; i++){
cout<<arr[i]<<\" \";
}
cout << endl;
return 0;
}
void bubblesort(int arr[],int length){
for(int i=length-1;i>0; i--){
for(int j=0;j<i;j++)
{
//JUST CHANGED AS LESSTHAN
if(arr[j]<arr[j+1]){
int temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
}
OUTPUT:
Enter an integer: 5
Enter an integer: 4
Enter an integer: 3
Enter an integer: 2
Enter an integer: 1
Enter an integer: 6
Enter an integer: 7
Enter an integer: 8
Enter an integer: 9
Enter an integer: 10
10 9 8 7 6 5 4 3 2 1

