This C++ program takes a file containing mixed numbers as input, sorts the numbers using both quicksort and bubble sort algorithms, and outputs the results to a new file. It defines a LinkedList struct to create linked lists for holding the sorted numbers. The main function reads the numbers from the input file into an array, calls the quicksort and bubble sort functions to sort the array into linked lists, then writes the sorted numbers from each linked list to the output file.
Independent Escorts Goregaon WhatsApp +91-9930687706, Best Service
Hw3
1. #include <iostream>
#include <string>
#include <fstream>
#include <cassert>
using namespace std;
// Ali Ülkü 13945 - This program takes input file with mixed numbers from user
// and sort them using bubble sort algorithm
struct LinkedList //LinkedList is a user given name
{
int num;
LinkedList *next; // pointer for the
next node
LinkedList () //default constructor
:num(0), next(NULL)
{}
LinkedList (int i,LinkedList *n) //constructor
:num(i), next(n)
{}
};
LinkedList *quickSort(int *array, int startPosition, int endPosition)
{
int n = endPosition - 1;
int middle = (startPosition+endPosition)/2;
int pivot = array[middle];
int* temp = new int[n]; // Temp is an array which contains mixed numbers
int variable = 0;
int i = startPosition;
int j = endPosition;
while (startPosition < j || i< endPosition )
{
while (array[i] < pivot)
i++;
while (array[j] > pivot)
j--;
if (i <= j) {
variable = array[i];
array[i] = array[j];
array[j] = variable;
i++;
j--;
}
/* recursion */
if (startPosition < j)
{
quickSort(array, startPosition, j);
}
if (i < endPosition)
{
quickSort(array, i, endPosition);
}
2. LinkedList * quick = new LinkedList;
quick->num = array[0];
LinkedList * head = quick;
int counter = 1; // Counter starts from 1 since i starts
from 1
for(int i = 1; i < n+2; i++)
{
quick->next = new LinkedList; // new LinkedList is
allocated from the memory whose adress is pointed by "sorting"
quick= quick->next; // After each cycle,
sorting shows a new sorting
quick->num =array[i]; // Num will takes the
value of sorted array
counter++; // to provide
true condition for assert() till last "sorting"
}
quick->next= NULL; // Last "sorting"
should point NULL since there is no need for another "sorting"
return head;
}
}
LinkedList *bubbleSort(int *array, int size)
{
int* temp = new int[size]; // Temp is an array which contains mixed
numbers
for(int i = 0; i <size; i++)
{
temp[i] = array[i]; // Temp is pushed by the elements of input
array.
}
for (int i=0; i < size-1; i++)
{
for(int j = i+1; j < size; j++)
{
if(temp[i]>temp[j])
{
int var = temp[i]; // if certain number is greater
temp[i] = temp[j]; // than following compared
number,
temp[j] = var; // they will switch place
}
}
}
LinkedList * sorting = new LinkedList;
sorting->num = temp[0];
LinkedList * head = sorting;
int counter = 1; // Counter starts from 1 since i starts
from 1
for(int i = 1; i < size; i++)
{
sorting->next = new LinkedList; // new LinkedList is
allocated from the memory whose adress is pointed by "sorting"
sorting = sorting->next; // After each cycle,
sorting shows a new sorting
sorting->num = temp[i]; // Num will takes the
value of sorted array
counter++; // to provide
true condition for assert() till last "sorting"
}
assert(size == counter);
sorting->next= NULL; // Last "sorting"
3. should point NULL since there is no need for another "sorting"
return head; // Returns to
the "head" which equals to "sorting"
}
int main()
{
ifstream file;
string filename = "HW2014-2015-Summer02-Input.txt";
file.open(filename.c_str());
string line = "";
int countOfLines = 0;
while(getline(file,line)) //Read the file in order to obtain the
number of lines
{
countOfLines++; //The size of the myList array
}
int endPosition = countOfLines - 1;
int startPosition = 0;
int* myList = new int[countOfLines]; //Creating a dynamic array
file.clear(); //To read the
file from the beginning
file.seekg(0, ios::beg);
int index = 0;
while(getline(file,line)) // Reads the file
from beggining, pushes elements in MyList array
{
myList[index]=stoi(line); // Read lines must be
converted from string to the integer since numbers of elements will be compared
index++;
}
LinkedList* temp1 = quickSort(myList,startPosition, endPosition);
// Calls the LinkedLink function called "bubbleSort(int* array, int
size)
LinkedList* temp2 = bubbleSort(myList,countOfLines); // Calls
the LinkedLink function called "bubbleSort(int* array, int size)
ofstream myfile;
myfile.open ("HW2014-2015-Summer02-Output.txt");
myfile << "quickSort"<<endl;
while(temp1 != NULL)
{
myfile << temp1->num << endl;
temp1 = temp1->next;
}
myfile << endl << "bubbleSort"<<endl;
while(temp2 != NULL)
{
myfile << temp2->num << endl;
temp2 = temp2->next;
}
myfile.close();