Question

C++ Program (Linux): Create a bubble sort that can read any list of number from a...

C++ Program (Linux):

Create a bubble sort that can read any list of number from a text file and print them out. Also print the number of comparison (how many time is had to compare number in order to sort).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <fstream>

#include <sstream>

using namespace std;

int main()

{

string line;

ifstream ifile("file.txt"); //input file

int d,n,i = 0, j = 0, swap = 0;

int arr[100]; //size of the array

if(ifile.is_open())

{

while(getline (ifile, line)) // reading first line from input file

{

std::istringstream is(line);

while( is >> n ) { //getting number from beginning of string

d = n;

}

arr[i++] = d;

}

int ARR_SIZE = i;

for(i=0;i<ARR_SIZE;i++)

{

for(j=0;j<ARR_SIZE;j++)

{

if(arr[i] < arr[j]) //swapping elements if larger is found

{

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

swap++;

}

}

}

for(i=0; i<ARR_SIZE; i++)

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

cout << "\nTotal number of swaps are " << swap << endl;

ifile.close();

}

else

cout<<"Unable to open file"<<endl;

return 0;

}

Terminal [+) (83%) w) 6:27 AM Save bubblesort.cpp file.txt Name:-/Desktop/pro/C++/file.txt MIME Type: plain text document (teNOTE : Number of comparisons is nothing but ARR_SIZE * ARR_SIZE. However, I have specified number of swaps.

Add a comment
Know the answer?
Add Answer to:
C++ Program (Linux): Create a bubble sort that can read any list of number from a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will create a dimple Bubble Sort program to sort a string of random integers or...

    You will create a dimple Bubble Sort program to sort a string of random integers or text. Please read instructions and examples Please show screenshot of proof that the code works in the C program ECE 216 Programming Project 2 The Bubble Sort You will create a simple Bubble Sort program to sort a string of random integers or text or whatever you can punch in from the keyboard. For the input data, you will input a string of single...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • C Programming Language on Linux - Word Frequency Program Please write a Program in C that...

    C Programming Language on Linux - Word Frequency Program Please write a Program in C that will accept a text file name as a command-line argument via a main program that will do the following: First, read the file (first pass) and create a linked list of words (in their order of occurrence), with the frequency of each word set to 0. Then, read the file (second pass) and for each word identified, search the linked list, and when found,...

  • Write a modularized, menu-driven program to read a file with unknown number of records.

    ==============C++ or java================Write a modularized, menu-driven program to read a file with unknown number of records.Create a class Records to store the following data: first and last name, GPA , an Id number, and an emailInput file has unknown number of records; one record per line in the following order: first and last names, GPA , an Id number, and emailAll fields in the input file are separated by a tab (‘\t’) or a blank space (up to you)No error...

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • OPERATING SYSTEM... C , LINUX Modify the mycat.c program to write to stderr the number of...

    OPERATING SYSTEM... C , LINUX Modify the mycat.c program to write to stderr the number of bytes that were read from stdin each time. You may use cLion inside VM, or any other text editor of your choice, such as vi or emacs. There is also a neat text editor named Kate available in Ubuntu (you can find it in the menu). In cLion you can open a single.cfile without having to create a whole new project and modify the...

  • C++ SORTING – BUBBLE SORT METHOD Use the below Text File and write code that sorts...

    C++ SORTING – BUBBLE SORT METHOD Use the below Text File and write code that sorts it based on the users sort method selection. Please provide a separate .cpp file wit hthe code containing the sort method. The sorting method uses the text file below and sorts it accordingly. Seperate the sorting method into an additional C++ file. ********************************* Text File Below is a text file called classes.txt. This text file lists, by course number and section number a series...

  • please use C++ write a program to read a textfile containing a list of books. each...

    please use C++ write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. each line in the file has tile, ..... write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. Each line in...

  • For C++ Write a main program that inputs a list of 20 numbers from a text file and then calls 4 ...

    For C++ Write a main program that inputs a list of 20 numbers from a text file and then calls 4 sort functions. Create a Sort Lab design document that include flowcharts of the main program and four sort functions Sort Functions:   Insertion, Selection, Bubble and Merge In each Sort function: count the number of compares and swaps needed to sort the numbers and display that information in the sort function. Run the program for four sets of data --...

  • Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection...

    Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection Sort with arrays of varying lengths. You will time each algorithm. The algorithms' time complexities should allow us to predict how these algorithms will perform. Program needs Four separate arrays, each with the same length and filled with the same sequence of randomly chosen numbers. Four separate functions, one for each of the four algorithms. All four functions will need to be timed. Be...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT