my professor said:i should get the sizeof array and then use modulus so i can apply merging.
example:
Input number of items to sort(must be divisible by 3, max of 18): 18 //it should be divisible by 3
input 18 numbers:51 60 80 2 9 5 22 100 11 13 15 8 3 33 30 120 150 107
phase1 sort:
file1: 51 60 80| 11 22 100| 3 30 33| it will be in order and it will merge and sort with file 2.
file2: 2 5 9| 8 13 15| 107 120 150|
phase1 merge:
file1:empty
file2:empty
file3:2 5 9 51 60 80|8 11 13 15 22 100|3 30 33 107 120 150|
phase2 sort:
file1: 2 5 9 51 60 80(it will merge/sort with file 2:8 11 13 15 22 100) |3 30 33 107 120 150|
file2:8 11 13 15 22 100|
phase 2 merge:
file3:2 5 8 9 11 13 15 22 51 60 80 100 |3 30 33 107 120 150
phase3 sort:
file1:2 5 8 9 11 13 15 22 51 60 80 100 //it will merge and sort with file2
file2:3 30 33 107 120 150
phase3(final merge!);
file3:2 3 5 8 9 11 13 15 22 30 33 51 60 80 100 107 120 150
the numbers are sorted in ascending order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <string.h>
#include <iostream>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <math.h>
using namespace std;
void TwoWayMerge(int*, int);
void printSort(int*, int);
void treeChopArray();
int main()
{
int n,nmchk;
int i=0;
cout << "Input number of items to sort(must be divisible by 3, max of 18): ";
cin >> n;
nmchk=n%3;
if(nmchk==0||nmchk>18)
{ int *integers=new int [n];
cout << "Please enter the "<<n<<" numbers : \n\n";
while ( i < n)
{
cout<<"input number "<<i+1<<": ";
cin >> integers[i];
i++;
}
TwoWayMerge(integers,n);
printSort(integers, n);}
{
cout<<"error! size should be divisible by 3!\n"; main();
}
}
void TwoWayMerge(int *q, int length)
{
int m,t;
for(m=0;m<length;m++)
{
for(t=0;t<m;t++)
{
if(q[m]<q[t])
{
int temp=q[m];
q[m]=q[t];
q[t]=temp;
}
}
}
}
void treeChopArray()
{
}
void printSort(int *q, int length)
{
int s=0;
cout<<"file: ";
for(s=0;s<length;s++)
{
cout<<q[s]<<" ";
}
cout<<"\n";
main();
}
| |