Bubble Sort Using Recursion 1 Write a program that will read

Bubble Sort Using Recursion

1. Write a program that will read each line from A2.txt and store the numbers into an array.
2. Sort the numbers of that array using Bubble sort algorithm (using recursion).
3. Show the output in file B2.txt.
*******Note: In total there will be 10 lines in B2.txt, where each line will contain a sorted array. First two lines are given as sample.

********HINTS: Convert the outer loop as a recursion call. Inner loop will stay the same.

A2.txt:

Sample input:
41 67 34 0 69 24 78 58 62 64 5 45 81 27 61

91 95 42 27 36 91 4 2 53 92 82 21 16 18 95

Sample output:
0 24 27 34 41 45 5 58 61 62 64 67 69 78 81

16 18 2 21 27 36 4 42 53 82 91 91 92 95 95

Solution

#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
/* recursive bubble sort to sort in ascending order */
void bubbleSort(int *data, int n) {
        int i, temp;

        if (n > 0) {
                for (i = 1; i < n; i++) {
                        if (data[i - 1] > data[i]) {
                                temp = data[i];
                                data[i] = data[i - 1];
                                data[i - 1] = temp;
                        }
                }

                bubbleSort(data, n - 1);
        }

        return;
}

int main() {
        int i, n=30, *data;
        /* dynamically allocate memory to store i/p values */
        ofstream outfile(\"B2.txt\");
        ifstream infile(\"A2.txt\");
        while(!infile.eof())
        {
        data = new int(n);
        for(int k=0;k<15;k++)
            infile>>data[k];
        /* sorts the given numbers */
            bubbleSort(data, 15);
            for (i = 0; i < n; i++) {
               outfile<<data[i]<<\" \";
            }
            outfile<<endl;
  }
        return 0;
}

Bubble Sort Using Recursion 1. Write a program that will read each line from A2.txt and store the numbers into an array. 2. Sort the numbers of that array using
Bubble Sort Using Recursion 1. Write a program that will read each line from A2.txt and store the numbers into an array. 2. Sort the numbers of that array using

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site