Question

Need a simple C++ code that read a text file of integers, puts the integers in...

Need a simple C++ code that read a text file of integers, puts the integers in a array and using quick sort, sorts the numbers.

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

#include <iostream>

#include <fstream>

using namespace std;

// A utility function to swap two elements

void swap(int* a, int* b)

{

int t = *a;

*a = *b;

*b = t;

}

/* This function takes last element as pivot, places

the pivot element at its correct position in sorted

array, and places all smaller (smaller than pivot)

to left of pivot and all greater elements to right

of pivot */

int partition (int arr[], int low, int high)

{

int pivot = arr[high]; // pivot

int i = (low - 1); // Index of smaller element

for (int j = low; j <= high- 1; j++)

{

// If current element is smaller than or

// equal to pivot

if (arr[j] <= pivot)

{

i++; // increment index of smaller element

swap(&arr[i], &arr[j]);

}

}

swap(&arr[i + 1], &arr[high]);

return (i + 1);

}

/* The main function that implements QuickSort

arr[] --> Array to be sorted,

low --> Starting index,

high --> Ending index */

void quickSort(int arr[], int low, int high)

{

if (low < high)

{

/* pi is partitioning index, arr[p] is now

at right place */

int pi = partition(arr, low, high);

// Separately sort elements before

// partition and after partition

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

}

}

/* UTILITY FUNCTIONS */

/* Function to print an array */

void printArray(int A[], int size)

{

int i;

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

cout<<A[i]<<" ";

cout<<endl;

}

int main(int argc, char * argv[])

{

string inFile;

cout<<"Enter input file name: ";

cin>>inFile;

std::fstream myfile(inFile, std::ios_base::in);

int a;

int arr[1000];

int i = 0;

while (myfile >> a)

{

arr[i] = a;

i++;

}

myfile.close();

printArray(arr, i);

quickSort(arr, 0, i-1);

printArray(arr, i);

return 0;

}

######### input.txt #########
4
3
12
6
7
1
0
56
21

fourthweek fourthweek fourthweek g++ quickSort.cpp fourthweek./a.out Enter input file name: input.txt 4 3 12 6710 56 21 013 4

Add a comment
Know the answer?
Add Answer to:
Need a simple C++ code that read a text file of integers, puts the integers in...
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
  • Please read data into a program from ascending_mostly_sorted.txt (a text file containing integers that are mostly...

    Please read data into a program from ascending_mostly_sorted.txt (a text file containing integers that are mostly in ascending order) and store into an array. Implement Insertion Sort to sort the values in ascending order. Least → Greatest Measure the time it takes to execute the sort in milliseconds or even nanoseconds. IN c++ Please run the sort 3 times.

  • 3. Write a program to read a (binary) file of integers, sort the integers, and write...

    3. Write a program to read a (binary) file of integers, sort the integers, and write them back to the same file. Assume that all the numbers can be stored in an array. (Exercise 3) 4. Repeat exercise 3, but assume that only 20 numbers can be stored in memory (in an array) at any one time. Hint: you will need to use at least two additional files for temporary output. Please finish the question in C language programming, thank...

  • 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...

  • C++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

  • In C Programming Language, write a simple program to read one text file and print to...

    In C Programming Language, write a simple program to read one text file and print to screen all its text. Make use of the stderr and exit program statements should there not be only one text file identified on the command line. If successful then before fclose of the text file, use the fputs program statement to write a line "72 degrees Sunny light wind. Nice!\n". print the return code of fputs (should be zero). close and exit. Test program...

  • write a c++ code to read multiple integers from input.dat until the end of file. Edit...

    write a c++ code to read multiple integers from input.dat until the end of file. Edit input.dat and put several integers in it. Compile and execute the code then check output.dat to verify all the integers are in there. Update the code to check to see if input.dat exists before reading from it. If it doesn’t exist print an error message (and don’t read!). Compile and execute your code. Delete input.dat and output.dat and execute – did you see your...

  • does anyone know how to do this? C++ Your task for this assignment is to use...

    does anyone know how to do this? C++ Your task for this assignment is to use C++ language to implement an insertion sort algorithm. 1. Implement an insertion sort with an array to sort 10 arbitrary numbers using the C++ programming language. The program initializes an array with 10 random 3-digit positive integers. The program then sorts the numbers in the array using the insertion sort algorithm. 2. The program displays the original values in the array before the sort...

  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • 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...

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