1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
barxatty [35]
3 years ago
7

Write a program that sorts an array of 10 integers using bubble sort. In the bubble sort algorithm, smaller values gradually “bu

bble” their way upward to the top of the array like air bubbles rising in water, while the larger values sink to the bottom. The bubble sort makes several passes through the array (of 10 items). On each pass, successive pairs of elements are compared. If a pair is in increasing order (or the values are identical), we leave the values as they are. If a pair is in decreasing order, their values are swapped in the array. The comparisons on each pass proceed as follows—the 0th element value is compared to the 1st, the 1st is compared to the 2nd, the 2nd is compared to the third, ..., the second-to-last element is compared to the last element.
Computers and Technology
1 answer:
blsea [12.9K]3 years ago
8 0

Answer:

#include<iostream>

using namespace std;

int main(){

   //initialization

  int arr1[10] = {2,4,6,1,7,9,0,3,5,8};

  int temp;

   int size_arr;

   //nested for loop

  for(int i=0;i<size_arr-1;i++){

   for(int j=0;j<size_arr-i-1;j++){

       if(arr1[j]>arr1[j+1]){ //compare

               //swapping

           temp = arr1[j];

           arr1[j]=arr1[j+1];

           arr1[j+1]=temp;

       }

   }

  }

  //display the each element

  for(int i=0;i<10;i++){

   cout<<arr1[i]<<" ";

  }

    return 0;

}

Explanation:

Create the main function and declare the variable and defining the array with 10 values.

take the nested for loop, nested loop means loop inside another loop.

the outer loop traverse in the array from 0 to size-1.

and inside loop traverse from 0 to size -i-1, because for every cycle of the outer loop the one element is sorted at the end. so, we do not consider the element from the last for every cycle of the outer loop. That's why (size-i-1)

In the bubble sort, the first element compares with the second element and if the first id greater than the second then swap the value. so, for the above algorithm, we take the if statement and it checks the condition if the condition becomes true then the swap algorithm executes.

we take a third variable for swapping. after that, if the outer loop condition false it means all elements will traverse and then the loop will terminate.

and finally, take another for loop and display the output.

You might be interested in
Which of the following transferable skills are generally the most looked for in the IT field?
san4es73 [151]
The correct option is D.
Transferable skills refers to those set of skills that do not belong to any specific field or industry, they are general skills and can be transferred from one job to another. Option D is the correct choice because all jobs require the employees to have problem solving skill, team building skills and communication skills; all these skills contribute to a successful career. 
3 0
3 years ago
Read 2 more answers
Which of the following tasks requires you to use the Tabs option?
Monica [59]
Indenting a paragraph :)
7 0
4 years ago
Do rats smell good? what do you think
irina1246 [14]

Answer:

Rats are not smelly animals. They are actually extremely clean animals and should not smell bad at all. ... A sick rat odor will smell like infection. Most likely if you are smelling your rats it is due to the cage and bedding not being cleaned often enough or from using the wrong type of litter and bedding.

Explanation:

7 0
3 years ago
Read 2 more answers
How many words fit on a double-spaced page?
sveticcg [70]
It depends on the length of the words.
5 0
4 years ago
What is the advantage of taking notes in a digital format? The technology protects digital notes from being copied. Digital note
VMariaS [17]

Answer:

Technology allows digital notes to be shared on the Internet.

Explanation:

6 0
3 years ago
Read 2 more answers
Other questions:
  • Olivia needs to get permission to use a graph of data gathered by a trade association she plans to include the graph in a report
    10·2 answers
  • Can you plug a usb 2.0 into a 3.0 port on your computer
    12·1 answer
  • Select the recommended design practice that applies to a web site using images for main site navigation.
    7·1 answer
  • Assume that the following variables have been defined in a program: int x = 10; int y = 20; int z = 30; Write a cout statement t
    11·1 answer
  • Modify the NumberedList class we implementd during the lecture by adding a member function: void NumberedList::insertPosition(in
    13·1 answer
  • Give me username ideas for rblx.
    6·2 answers
  • Do like TikTok??????
    14·2 answers
  • Describe two types of storage devices?​
    15·1 answer
  • Que es un vlog? Cual es la diferencia entre vloc y papgina web? ​
    7·1 answer
  • PLS HELP ILL GIVE BRAINLY- (enter the answer) Microsoft _________ is an example of a desktop publishing software
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!