Sorting Arrays 3. Sorting Arrays There are several sorting algorithms that can b
ID: 3762288 • Letter: S
Question
Sorting Arrays
3. Sorting Arrays There are several sorting algorithms that can be used to sort arrays. The bubble sort is a fairly simple sorting algorithm. As elements are sorted in the bubble sort, they gradually "bubble" (or rise) to their proper location in the array, like bubbles rising in a glass of soda. The following program uses the bubble sort algorithm to sort integers in an array in ascending order. Note that this program uses the range-based for loop introduced in C++11 I sorts an array of integers using Bubble Sort #includeExplanation / Answer
program for bubble sort decending order:
# include<iostream>
using namespace std;
cont int size=10;
void bubblesort(int arr[], int length);
int main()
{
int arr[size];
for(int i=0;i<size;i++)
{
count << "enter an integer";
cin >> arr[i];
}
bubblesort(arr,size);
for (int a: arr)
{
cout<< a<< " ";
}
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++)
{
if(arr[j]<arr[j+1])
{
int temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.