C++ only.
PROBLEM STATEMENT:
A review and extension of cs132:sort a file with 120 records.
However, due to memory restrictions
only 20 records may be placed into memory. You are to implement a
“quasi” external sort
CODE/DIRECTIONS:
For the sake of simplicity, and without much loss of generality, we
will say for this lab are records
are just ints : thus sort a file with 120 ints where only 20 ints
maybe placed into memory at any
one time
general idea:
break data into blocks, sort blocks into runs, merge runs
less general idea:
Call inFile1 our source file ( the one with the initial 120 records
to be sorted.
You will also need an inFile2, and 2 other files outFile1 and
outFile2.
Break the file into blocks of size 20: in this case there will be 6
blocks ( 120/20 )
Sort the blocks by
read a block, sort it, store in outFile1
read a block, sort it, store in outFile2
read a block, sort it, store in outFile1
( in other words, sort a block and alternately place in the files
outFile1,
outFile2 )
By definition a run is a sorted block
Note that each file outFile1 and outFile2 will have half of the
runs
Merge the runs
Merge data from outFile1 and outFile2 to inFile1 and inFile2.
Merge the first run on outFile1 and the first run on
outFile2,
and store the result on inFile1:
Read two records in main memory, compare, store the smaller
on inFile1
Read the next record from either outFile1 or outFile2 the
file
that had its record moved/stored to inFile1
Similarly merge the second run on outFile1 and the second run
on
outFile2, store the result on inFile2.
Merge the third run on outFile1 and the third run on outFile2,
store
the result on inFile1... etc
merging each run and storing the result alternatively on
inFile1
and inFile2.
At the end
inFile1 and inFile2 will contain sorted runs twice the size of
the
previous runs on outFile1 and outFile2
Now merge data from inFile1 and inFile2 to outFile1 and
outFile2.
Merge the first run on inFile1 and the first run on inFile2, and
store
the result on outFile1.
Merge the second run on inFile1 and the second run on
inFile2,
store the result on outFile2
Etc, merge and store alternatively on inFile1 and inFile2.
Repeat the process until only one run is obtained. This would be
the sorted
file
Must use above algorithm
MUST use algorithms from sort_algorithms.t:
the algorithms may not be modified. - but you may assume an
overloaded < operator
exists. Thus you may remove the pointer to the function cmp (
obviously, the code will have to
replace any reference to cmp, no puns intended, with < )
It is permissible to copy/paste any algorithms from
sort_algorithms.t that you use to your own sort_alg.t
Declare a variable MAX_MEM_SIZE, of type size_t. Initialize to
20
If you are really paying attention to the directions, you will
realize that 2 arrays of 20 cannot both be in
memory at the same time – depending on how you implement the above
algorithm and which
sort you use, you may need to break into blocks of size 10
a sample file to be sorted is provided.
data.txt
15437 5579 30403 20723 15051 1275 31856 3192 20446 26400 2346 1804 20246 10454 24880 5574 5060 26748 22640 28586 8296 26589 3486 19567 21101 16655 23428 24710 32276 25244 29849 23127 1711 5856 23764 17614 18191 6834 13762 29462 24127 1863 4311 22948 13427 4946 15362 30840 19515 28528 15491 15007 13308 16836 27649 28413 11169 1246 27607 15945 21134 19938 25264 16985 29141 15846 14419 8864 13997 8859 17344 23029 28960 2608 8059 2453 11011 20083 6184 32108 11051 23354 26968 17808 14558 13313 15523 2319 13192 31956 7927 30186 21167 2672 24623 29281 8745 15781 4327 29553 3725 11774 4751 7324 5607 26532 28095 3304 28848 16512 25290 32042 18173 2208 26774 4996 17910 6620 3499 1
sort_algorithms.t
#ifndef SORT_ALGORITHMS_T__
#define SORT_ALGORITHMS_T__
#include
#include
#include
using std::fstream;
using std::streampos;
#include
using std::ios;
using std::endl;
template
void swap( T &a, T &b )
{
T temp = a;
a = b;
b = temp;
}
template
void selectSort( T* arrayptr, int arraySize, bool ( *cmp )( T
&baseData1, T &baseData2 ) )
{
int smallindex; // index of smallest element in the sublist
int pass, j;
// pass has the range 0 to n-2
for ( pass = 0; pass < arraySize - 1; pass++ )
{
// scan the sublist starting at index pass
smallindex = pass;
// j traverses the sublist arrayptr[pass+1] to
aarrayptr[n-1]
for ( j = pass + 1; j < arraySize; j++ )
// update if smaller element found
if ( cmp ( arrayptr[j], arrayptr[smallindex] ) )
smallindex = j;
// when finished, exchange smallest item with
arrayptr[pass]
swap( arrayptr[pass], arrayptr[smallindex] );
}
}
template
void doubleSeletcSort( T* arrayptr, int arraySize, bool ( *cmp )( T
&baseData1, T &baseData2 ) )
{
// index of smallest and largest elements in a sublist
int smallIndex, largeIndex;
int i, j, k;
T temp;
// start indices
i = 0;
j = arraySize - 1;
// as long as i < j
while (i < j)
{
// scan the sublist {arrayptr[i], ..., arrayptr[j]}
smallIndex = i;
largeIndex = i;
// k traverses the sublist {arrayptr[i+1], ...,
arrayptr[j]}
for (k = i+1; k <= j; k++)
// update if smaller element found
if ( cmp( arrayptr[k], arrayptr[smallIndex] ) )
smallIndex = k;
// update if larger element found
else if ( cmp( arrayptr[largeIndex], arrayptr[k] ) )
largeIndex = k;
// if smallIndex and i are not the same location,
// swap smallest item in the sublist with arrayptr[i]
if (smallIndex != i)
{
swap( arrayptr[i], arrayptr[smallIndex] );
// at index i, arrayptr[i] maybe largest element
// if so, swap moves the largest value to index smallIndex
if (largeIndex == i)
largeIndex = smallIndex;
}
// if largeIndex and j are not the same location,
// swap largest item in the sublist with arrayptr[j]
if (largeIndex != j)
{
swap( arrayptr[j], arrayptr[largeIndex] );
}
// move i forward and j backward
i++;
j--;
}
}
template
void insertionSort( T* arrayptr, int arraySize, bool ( *cmp )( T
&baseData1, T &baseData2 ) )
{
int i, j;
T target;
// place arrayptr[i] into the sublist
// arrayptr[0] ... arrayptr[i-1], 1 <= i < n,
// so it is in the correct position
for (i = 1; i < arraySize; i++)
{
// index j scans down list from arrayptr[i] looking for
// correct position to locate target. assigns it to
// arrayptr[j]
j = i;
target = arrayptr[i];
// locate insertion point by scanning downward as long
// as target < arrayptr[j-1] and we have not encountered
the
// beginning of the list
while (j > 0 && target < arrayptr[j-1])
{
// shift elements up list to make room for insertion
arrayptr[j] = arrayptr[j-1];
j--;
}
// the location is found; insert target
arrayptr[j] = target;
}
}
template
void bubbleSort( T* arrayptr, int arraySize, bool ( *cmp )( T
&baseData1, T &baseData2 ) )
{
register int i,j;
// index of last exchange
bool did_swap = true;
// i is the index of last element in the current sublist
i = arraySize - 1;
// continue the process until we make no exchanges or
// we have made n-1 passes
while ( i > 0 && did_swap )
{
// assume no exchange occurs
did_swap = false;
// scan the sublist arr[0] to arr[i]
for ( j = 0; j < i; j++ )
// exchange a pair and assign true to exchangeOccurs
if ( cmp( arrayptr[j + 1], arrayptr[j] ) )
{
swap( arrayptr[ j ], arrayptr[ j+1 ] );
did_swap = true;
}
// move i downward one element
i--;
}
}
template
void basicBubbleSort( T* arrayptr, int arraySize, bool ( *cmp )( T
&baseData1, T &baseData2 ) )
{
register int i,j;
for ( i = 1; i < arraySize; i++ )
{
for ( j = arraySize - 1; j >= i; j-- )
{
if ( cmp( arrayptr[ j ], arrayptr[ j - 1 ] ) )
swap( arrayptr[ j -1 ] , arrayptr[ j ] );
}
}
}
template
void mergesort1(T* arrayptr, const int& arraySize )
{
sortmerge1( arrayptr, arraySize, 0, arraySize - 1 );
}
template
void sortmerge1( T* arrayptr, const int& arraySize, int l, int
r )
{
int mid, i, j, k;
if ( l < r )
{
mid = (r + l)/2;
sortmerge1( arrayptr, arraySize, l, mid );
sortmerge1( arrayptr, arraySize, mid + 1, r );
T* temp = new T[ arraySize ];
for ( i = mid + 1; i > l; i-- )
temp[ i - 1 ]= arrayptr[ i - 1 ];
for ( j = mid; j < r; j++ )
temp[ r + mid - j ] = arrayptr[ j + 1 ];
for ( k = l; k <= r; k++)
arrayptr[k] = ( temp[i] < temp[j] ) ? temp[i++] : temp[j--];
}
temp = 0;
delete [] temp;
}
template
void msSort( T* arrayptr, const int& arraySize )
{
T* copy = new T[ arraySize ];
int i;
for ( i = 0; i < arraySize; i++ )
copy[i] = arrayptr[i];
mergesort2( copy, arrayptr, 0, arraySize - 1 );
}
template
void mergesort2( T* source, T* dest, int l, int r )
{
if ( l != r )
{
int mid = ( l + r )/2;
mergesort2( dest, source, l, mid );
mergesort2( dest, source, mid + 1, r );
merge2( source, dest, l, mid, r );
}
}
template
void merge2( T* source, T* arrayptr , int l, int mid, int r )
{
int i = l;
int j = mid + 1;
int k = l;
while ( ( i <= mid ) && ( j <= r ) )
// Compare current item from each list
if ( source[ i ] < source[ j ] )
// Then i item comes first
arrayptr[ k++ ] = source[ i++ ];
else // j item comes first
arrayptr[ k++ ] = source[ j++ ];
// Move
what is left of remaining list
if ( i > mid )
while ( j <= r )
arrayptr[ k++ ] = source[ j++ ];
else
while (i <= mid )
arrayptr[ k++ ] = source[ i++ ];
}
#endif
struct MinHeapNode
{
// The element to be stored
int element;
// index of the array from which the element is taken
int i;
};
// Prototype of a utility function to swap two min heap nodes
void swap(MinHeapNode* x, MinHeapNode* y);
// A class for Min Heap
class MinHeap
{
MinHeapNode* harr; // pointer to array of elements in heap
int heap_size; // size of min heap
public:
// Constructor: creates a min heap of given size
MinHeap(MinHeapNode a[], int size);
// to heapify a subtree with root at given index
void MinHeapify(int);
// to get index of left child of node at index i
int left(int i) { return (2 * i + 1); }
// to get index of right child of node at index i
int right(int i) { return (2 * i + 2); }
// to get the root
MinHeapNode getMin() { return harr[0]; }
// to replace root with new node x and heapify()
// new root
void replaceMin(MinHeapNode x)
{
harr[0] = x;
MinHeapify(0);
}
};
// Constructor: Builds a heap from a given array a[]
// of given size
MinHeap::MinHeap(MinHeapNode a[], int size)
{
heap_size = size;
harr = a; // store address of array
int i = (heap_size - 1) / 2;
while (i >= 0)
{
MinHeapify(i);
i--;
}
}
// A recursive method to heapify a subtree with root
// at given index. This method assumes that the
// subtrees are already heapified
void MinHeap::MinHeapify(int i)
{
int l = left(i);
int r = right(i);
int smallest = i;
if (l < heap_size && harr[l].element < harr[i].element)
smallest = l;
if (r < heap_size && harr[r].element < harr[smallest].element)
smallest = r;
if (smallest != i)
{
swap(&harr[i], &harr[smallest]);
MinHeapify(smallest);
}
}
// A utility function to swap two elements
void swap(MinHeapNode* x, MinHeapNode* y)
{
MinHeapNode temp = *x;
*x = *y;
*y = temp;
}
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for(i = 0; i < n1; i++)
L[i] = arr[l + i];
for(j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
arr[k++] = L[i++];
/* Copy the remaining elements of R[], if there
are any */
while(j < n2)
arr[k++] = R[j++];
}
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l + (r - l) / 2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
FILE* openFile(char* fileName, char* mode)
{
FILE* fp = fopen(fileName, mode);
if (fp == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
return fp;
}
// Merges k sorted files. Names of files are assumed
// to be 1, 2, 3, ... k
void mergeFiles(char *output_file, int n, int k)
{
FILE* in[k];
for (int i = 0; i < k; i++)
{
char fileName[2];
// convert i to string
snprintf(fileName, sizeof(fileName), "%d", i);
// Open output files in read mode.
in[i] = openFile(fileName, "r");
}
// FINAL OUTPUT FILE
FILE *out = openFile(output_file, "w");
// Create a min heap with k heap nodes. Every heap node
// has first element of scratch output file
MinHeapNode* harr = new MinHeapNode[k];
int i;
for (i = 0; i < k; i++)
{
// break if no output file is empty and
// index i will be no. of input files
if (fscanf(in[i], "%d ", &harr[i].element) != 1)
break;
harr[i].i = i; // Index of scratch output file
}
MinHeap hp(harr, i); // Create the heap
int count = 0;
// Now one by one get the minimum element from min
// heap and replace it with next element.
// run till all filled input files reach EOF
while (count != i)
{
// Get the minimum element and store it in output file
MinHeapNode root = hp.getMin();
fprintf(out, "%d ", root.element);
// Find the next element that will replace current
// root of heap. The next element belongs to same
// input file as the current min element.
if (fscanf(in[root.i], "%d ", &root.element) != 1 )
{
root.element = INT_MAX;
count++;
}
// Replace root with next element of input file
hp.replaceMin(root);
}
// close input and output files
for (int i = 0; i < k; i++)
fclose(in[i]);
fclose(out);
}
// Using a merge-sort algorithm, create the initial runs
// and divide them evenly among the output files
void createInitialRuns(char *input_file, int run_size,
int num_ways)
{
// For big input file
FILE *in = openFile(input_file, "r");
// output scratch files
FILE* out[num_ways];
char fileName[2];
for (int i = 0; i < num_ways; i++)
{
// convert i to string
snprintf(fileName, sizeof(fileName), "%d", i);
// Open output files in write mode.
out[i] = openFile(fileName, "w");
}
// allocate a dynamic array large enough
// to accommodate runs of size run_size
int* arr = (int*)malloc(run_size * sizeof(int));
bool more_input = true;
int next_output_file = 0;
int i;
while (more_input)
{
// write run_size elements into arr from input file
for (i = 0; i < run_size; i++)
{
if (fscanf(in, "%d ", &arr[i]) != 1)
{
more_input = false;
break;
}
}
// sort array using merge sort
mergeSort(arr, 0, i - 1);
// write the records to the appropriate scratch output file
// can't assume that the loop runs to run_size
// since the last run's length may be less than run_size
for (int j = 0; j < i; j++)
fprintf(out[next_output_file], "%d ", arr[j]);
next_output_file++;
}
// close input and output files
for (int i = 0; i < num_ways; i++)
fclose(out[i]);
fclose(in);
}
// For sorting data stored on disk
void externalSort(char* input_file, char *output_file,
int num_ways, int run_size)
{
// read the input file, create the initial runs,
// and assign the runs to the scratch output files
createInitialRuns(input_file, run_size, num_ways);
// Merge the runs using the K-way merging
mergeFiles(output_file, run_size, num_ways);
}
C++ only. PROBLEM STATEMENT: A review and extension of cs132:sort a file with 120 records. However,...
How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private: float array[1000] ; int n = 1000; int i=0; public: void fillArray() { for(i=1;i<=n;i++) { array[i-1]= ( rand() % ( 1000) )+1; } } void arrayout () { ...
In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays. You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); //left recursion mergesort(a,mid+1,j); //right...
Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8, Part 3 * * Author: your name * z-ID: your z-ID * Date: due date of assignment * * This program builds, sorts and prints lists using the quicksort and * merge sort algorithms. */ #include <iostream> #include <iomanip> #include <vector> #include <string> #include "sorts.h" #include "quicksort.h" #include "mergesort.h" using std::cout; using std::fixed; using std::left; using std::setprecision; using std::string; using std::vector; // Data files...
Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel. The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge...
Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...
Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...
Hello, I want to check if my C++ code is correct and follows the
requeriments described thanks.
Requeriments:
Assignment Sorting
Benchmark each of the sorting methods listed below.
Insertion Sort
Bubble Sort
Selection Sort
Heap Sort.
Quick Sort.
Merge Sort.
Benchmark each of the above sorting methods for data sizes of
10000, 20000, 30000, 40000 and 50000. Display the results in a
table as shown below. The table should have rows and columns.
However, the rows and columns need not...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) { int i, j, k, c[100000]; i = low; k = low; j = mid + 1; while (i <= mid && j <= high) { if (a[i] < a[j]) { c[k] = a[i]; k++; i++; } else { ...