Question

In: Computer Science

C++ Write a program for sorting a list of integers in ascending order using the bubble...

C++

Write a program for sorting a list of integers in ascending order using the bubble sort algorithm

Requirements
Implement the following functions:

  1. Implement a function called readData
    int readData( int **arr)
    arr is a pointer for storing the integers. The function returns the number of integers.
    The function readData reads the list of integers from a file call data.txt into the array arr. The first integer number in the file is the number of intergers. After the first number, the file lists the integers line by line.
  2. void bsort(int *arr, int last)
    arr is a pointer to an array of integers to be sorted. last is the number of elements in the array. The function bsort sorts the list of integers in ascending order.
    Here is the Link to the Bubble Sort.
  3. writeToConsole(int * arr, int last)
    arr is a pointer to an array of integers. last is the number of elements in the array. The function writeToConsole displays the sorted list.
  4. Do not use the array notation in your solution.

Here is the content of the file data.txt.
9
8
4
7
2
9
5
6
1
3

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include<iostream>

#include<fstream>

#include<cstdlib>

using namespace std;

int readData(int *(&arr)){

    int n;

    ifstream in;

    in.open("data.txt");

    if(in.fail()){

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

        return 0;

    }

    in>>n;

    arr = new int[n];

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

        in>>*(arr+i);

    }

    in.close();

    return n;

}

void bsort(int *arr, int last){

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

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

            if(*(arr+j)>*(arr+j+1)){

                int temp = *(arr+j);

                *(arr+j) = *(arr+j+1);

                *(arr+j+1) = temp;

            }

        }

    }

}

void writeToConsole(int *arr, int last){

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

        cout<<*(arr+i)<<" ";

    }

    cout<<endl;

}

int main(){

    int *arr;

    int n;

    n = readData(arr);

    cout<<"Before sorting: "<<endl;

    writeToConsole(arr, n);

    bsort(arr, n);

    cout<<"After sorting: "<<endl;

    writeToConsole(arr, n);

    delete[] arr;

    return 0;

}


Related Solutions

***C++ Coding*** Write a program for sorting a list of integers in ascending order using the...
***C++ Coding*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Please include comments to understand code. Requirements Implement the following functions: int readData( int **arr) arr is a pointer to pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr. The first integer number in the file is the number of intergers....
// This program uses a bubble sort to arrange an array of integers in // ascending...
// This program uses a bubble sort to arrange an array of integers in // ascending order (smallest to largest). It then display the array // before the sorting and after the sorting. Modify the program so it orders // integers in descending order (largest to smallest). Then add some code // to display the array at each step of the algorithm. You don't have to // modify anything in the main() function. All modification are inside // the bubbleSortArray()...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers....
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using an ascending order selection sort, modified to print out the...
Write C program that reorders elements of an array of integers such that the new order...
Write C program that reorders elements of an array of integers such that the new order is in descending order (first number being the largest). Must have a main function and a swap function. - int main() will declare an array with the values { 32, 110, 79, 18, 22, 2}. This array will be passed to the swap function. - the void swap function will perform the necessary operations to reorder the elements of the array. - After swap()...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program should: 1. Accurately implement the skip list ADT using a random number generator and nodes that contain an integer as well as the addresses of adjacent nodes to the left, right, up, and down. 2. Correctly implement the Insert, Search, and Delete operations. 3. Read a series of unique, newline-delineated integers from a file and insert them (one at a time in the order...
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
Write a C++ program to read in a list of 10 integers from the keyboard. Place...
Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT