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
777dan777 [17]
3 years ago
11

Write a program in C++ to implement bubblesort using the swap function ?

Computers and Technology
1 answer:
marishachu [46]3 years ago
3 0

C++ program for implementation of Bubble sort  

#include <bits/stdc++.h>  

using namespace std;  

 void swap(int *x, int *y)  /*Defining Swap function of void return type*/

{  

  int temp = *x;  //Swaping the values  

   *x = *y;  

   *y = temp;  

}  

void bubbleSort(int array[], int n) /*Defining function to implement bubbleSort  */

{  

  int i, j;  

  for (i = 0; i < n-1; i++)      

      for (j = 0; j < n-i-1; j++)  /*The last i components are already in location  */

      if (array[j] > array[j+1])  

          swap(&array[j], &array[j+1]);  //Calling swap function

}  

int main()  //driver function

{  

   int array[] = {3, 16, 7, 2, 56, 67, 8}; //Input array  

   int n = sizeof(array)/sizeof(array[0]);  //Finding size of array

   bubbleSort(array, n); //Function calling  

   cout<<"Sorted array: \n";  

   for (int i = 0; i < n; i++)  //printing the sorted array

       cout << array[i] << " ";  

   cout << endl;  

 return 0;  

}  

<u>Output</u>

Sorted array:  

2 3 7 8 16 56 67

You might be interested in
Which access database object asks a question about the data stored in a database and displays specific fields and records that a
elena-14-01-66 [18.8K]
The Query object is used to asks a question about the data stored in a database and displays the specific fields and records that answer the question.

In code form, the select command is used for queries. 
6 0
3 years ago
Given positive integer numinsects, write a while loop that prints that number doubled without reaching 100. follow each number w
ELEN [110]
Please provide the language you're using when you ask for programming help, otherwise you aren't going to get the answer that you are looking for.

Here it is in Java, and I'm assuming the number is given via user input? Otherwise, just remove the user input function and replace the integer with a value of your choice. Note, that this isn't the full code; only what is relevant to the question.


public static void main(String[] args) {
    int num = numInput(10);
    printDoubles(num, 100);  // You can create a user input function for
                                             // maxValue if you wanted to.
}

/**
 * Receives user input between 0 and the absolute value of maxInput.
 * @param maxInput The largest absolute value that can be input.
 */
private static int numInput(int maxInput) {
    Scanner sc = new Scanner(System.in);

    maxInput = Math.abs(maxInput);

    int num = 0;
    while (!(num > 0 && num <= maxInput)) {
        num = sc.nextInt();

        if (!(num > 0 && num <= maxInput)) {
            System.out.println("Input too small or too large");
        }
    }

    return num;
}

/**
 * Continues to print out num doubled until maxValue is reached.
 * @param num The number to be printed.
 * @param maxValue The maximum value (not including in which num can                                       be doubled to.
 */
private static void printDoubles(int num, int maxValue) {
    if (num >= maxValue) {
        System.out.println("No output.");
    }

    while (true) {
        if (num >= maxValue) {
            break;
        }

        if (num < maxValue) {
            System.out.print(num + " ");
        }

            num *= 2;
    }

    System.out.println();
}

8 0
3 years ago
Read 2 more answers
You are installing a webcam in the screen bezel of your laptop. Prior to disassembling the laptop, what other devices in the scr
guapka [62]

Answer:

a. Microphone

d. Wi-Fi antenna

Explanation:

The wifi antenna , is present in the screen bezel , and the two cables are connected to the motherboard and the wifi adapter .

Hence , we need to be aware of the wifi antenna , before installing the webcam .

Microphone , as in most of the laptop , the microphone is present just near the screen bezel , and hence , need to be aware of before the installation process of the webcam .

7 0
3 years ago
Read 2 more answers
Which of the following is an example of cyberharassment?a. Someone sends you a credible threat that they are "out to get you."b.
Marta_Voda [28]

Answer:

D

Explanation:

In order for it to be considered harassment, it has to happen on more than one occasion.

7 0
3 years ago
Describe some of the moral, ethical, and legal issues surrounding responsible use of technology (and the information obtained th
rosijanka [135]

Answer:

Technology must be used in the right manner, and for the right purposes. When the latest in image processing is used to find a new planet through machine learning, we are right. However, when we kill somebody with a gun, we are not right. The bullet can be directed to legs rather than heart, and with continuous love and affection, any heart can be won. Sometimes this is allowed legally, Also it can be ethically correct as some institutions might allow it ethically, and it can be for some morally right as well. However, still, this is not right for some, and that is the almighty. And it's hard to match him morally or ethically. And for him nothing wrong is legal. Coming back to the use of technology in the workplace, you will find that you must follow the ethics of that institution, and your moral value must always be subsidiary to their ethics. And in the 21st century, and particularly in developed domains you will never be asked to sacrifice your moral value, and by the end of next quarter, this might be true for the whole world. Hence, ethics and moral values by then can in all cases be used interchangeably, However, when it comes to being legal, your institution will let you know all the laws, and you need to follow them, and these are applicable to all the institutions, and hence you need not require to sacrifice anything for being legal. Always remember that in the 21st century, you might see some very lethal technologies, and your job in the workplace is to study those technologies with your fullest potential. Issues will arise if you do not work with your fullest potential. And if you will, your moral value will be respected, and you will be easily able to obey their ethics, as no company ever think about even a little bad for their employees. And legally even the director is required to be correct just like you. And what is legal is being decided by the court and not any company. Hence, always try to work with your fullest potential, and never think to harm anybody, or your company. If you do this, you will be the hero of your company very soon.

Explanation:

And remember, the information that you get to know in your company is confidential, and you should not leak it to anybody, and even your relatives. Always keep them hidden inside you, and never ever try to leak them.

3 0
3 years ago
Other questions:
  • If an improvement creates no significant change in a product’s performance, then it is a(n) ? a0 design improvement.
    6·1 answer
  • What is the name of the organization responsible for assigning public ip​ addresses?
    13·1 answer
  • For this problem, you will write a function standard_deviation that takes a list whose elements are numbers (they may be floats
    10·1 answer
  • What is a popular use for SLR film cameras? (choose one)
    14·1 answer
  • A computer record is used to store all the information about one transaction, but several such records must be used to store the
    12·1 answer
  • What are the primary benefits of relying on systems development standards?
    7·1 answer
  • When a chart is selected, numerous customization options can be found on which Chart Tools tabs?
    11·2 answers
  • Write a public static method named evens that takes in 1 argument int a, and returns a String containing all positive even numbe
    8·1 answer
  • Design the program in the following way:
    8·1 answer
  • Determine which program you would recommend each of the customers should use. There are multiple correct answers so please inclu
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!