Question

In: Computer Science

C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that...

C++

Bubble Sort

Write a program that ask user to enter 7 numbers and store that in array. Display that all numbers before and after performing Bubble sort. You must have to create new function with required parameter to perform Bubble sort.

Sample Run :-

Enter 1 number :- 1

Enter 2 number :- 5

Enter 3 number :- 7

Enter 4 number :- 45

Enter 5 number :- 90

Enter 6 number :- 6

Enter 7 number :- 55

Numbers Before Bubble Sort :-

1 5 7 45 90 6 55

Numbers after Bubble sort :- 1 5 6 7 45 55 90

Solutions

Expert Solution

#include <iostream>

using namespace std;

void bubbleSort(int arr[], int size) {

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

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

// To sort in descending order, change > to < in this line.

if (arr[j] > arr[j + 1]) {

// swap if greater is at the rear position

int temp = arr[j];

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

arr[j + 1] = temp;

}

}

}

}

int main() {

int arr[7];

for(int i=0;i<7;i++)

{

cout<<"Enter "<<i+1<<" number:- ";

cin>>arr[i];

}

cout<<"Numbers Before Bubble Sort:- ";

for(int i=0;i<7;i++)

{

cout<<arr[i]<<" ";

}

  

bubbleSort(arr,7);

  

cout<<"\nNumbers After Bubble Sort:- ";

for(int i=0;i<7;i++)

{

cout<<arr[i]<<" ";

}

}


Related Solutions

Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
Write a program in MIPS Assembly. This program will ask the user to enter 20 numbers....
Write a program in MIPS Assembly. This program will ask the user to enter 20 numbers. The program will store these numbers in an array in memory (sequential memory locations). It will then print out the list of numbers in three different formats: The numbers will be printed each on a separate line. The numbers will be printed on a single line, with spaces between the numbers. The program will ask the user to enter a number n. The program...
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and compute xy
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
C++ Programa that: // 1) Ask the user to enter two numbers greater than zero. Store...
C++ Programa that: // 1) Ask the user to enter two numbers greater than zero. Store the //    values into two variables named 'n' and 'm'. //    Additionally, implement a while-loop to keep asking the user to enter //    the numbers again if any of the two numbers are equal or smaller than zero. // 2) If the two numbers (n, m) are equal, display the message "The //    numbers are the same". If the two numbers are different, display...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT