Here is it all:
cpp
/*
* Merge and Sort
* KYA
*/
#include <iostream>
using namespace std;
void read(int list[], int &n);
void display(const int a[], int n);
// print the first n elements of a[]
void merge(int a[], int n1, int b[], int n2, int c[]);
// merge values in ascending order of arrays a and b to array c in ascending order
// a has n1 integers; b has n2 integers
const int MAXSIZE = 10;
int main()
{
// don't modify anything in main()
int list1[MAXSIZE];
int list2[MAXSIZE];
int list3[MAXSIZE * 2];
int size1, size2;
read(list1, size1);
display(list1, size1);
read(list2, size2);
display(list2, size2);
merge(list1, size1, list2, size2, list3);
display(list3, (size1 + size2));
cout << endl;
system("pause");
return 0;
}
void read(int list[], int& n)
{
cout << "Enter no more than " << MAXSIZE << " integers in ascending order (-1 to finish): " << endl;
n = 0;
for (int i = 0; i < MAXSIZE; i++)
{
cin >> list[i];
if (list[i] == -1)
break;
else
n++;
if (n == MAXSIZE)
cin.ignore(100, '\n');
}
}
// print the first n elements of a[]
void display(const int a[], int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " " << flush;
cout << endl;
}
// merge sorted values of arrays a and b to array c
void merge(int a[], int n1, int b[], int n2, int c[])
{
//merge a and b into one array c
for (int i = 0; i < n1; i++){
c[i] = a[i];
}//end for
for (int i = n1, j = 0; i < (n1+n2); i++, j++){
c[i] = b[j];
}//end for
cout << "\n\nc[] pre sort:";
display(c, (n1+n2));
cout << "\n\n";
//sort
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(c[i]<c[j])
{
int temp=c[i]; //swap
c[i]=c[j];
c[j]=temp;
}//end if
}//end inner for
}//end outer for
}//end merge and sort function
This one is a bubble sort. Enjoy
This post has been edited by KYA: 30 May, 2008 - 05:59 AM