Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sorting Arrays 3. Sorting Arrays There are several sorting algorithms that can b

ID: 3762289 • 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 #include

Explanation / Answer

for descending order the code remains the same we will use arr[j]<arr[j+1]] in the IF conditions rest remains the same.

void bubblesort(int arr[],intlength)

{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]; // if the element on the right is greater to the left swap it and repeat it till the end of the array

arr[[j+1]=arr[j];

arr[j]=temp;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote