Given below is the code for which I would like someone to help me figure out the time and space complexity, keeping in mind that both of them would depend only on the "Sorting Algorithm Code" of the given program.
Do keep in mind that the no. of columns that you can take as input should be an odd integer excluding 1, and an even numbered column will yield false results due to the way the code is being sorted.
When asked for the "constant multiplier" you need to type in these tested numbers, that help sort the 2D array:
1. For 100 elements, columns=33 and constant multiplier=4,
2. For 200 elements, columns=67 and constant multiplier=10,
3. For 300 elements, columns=101 and constant multiplier=15,
4. For 400 elements, columns=133 and constant multiplier=15,
5. For 500 elements, columns=167 and constant multiplier=25,
6. For 30000 elements, columns=9999 and constant multiplier=2500.
etc.. etc..
I would also like to accept input from a .txt file saved elsewhere. How can I do it?
Cheers. Thanks for baring with me :)
The Code:
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<fstream>
using namespace std;
void main()
{
system("cls");
int col;
int d;
int cons=1;
A:
cout << endl;
int a[3][40000] = { {0} };//Initializing the array to 0
cout << "enter no. of columns:";//No. of Columns for the 3xN matrix
cin >> col;
int noe = col * 3;//Total No. of Elements
//Code to accept the constant multiplier
cout << "Enter the constant multiplier:";
cin >> cons;
//Code to generate a list of random integers and store it in the array
for (int i = 0; i < col;i++)
for (int j = 0; j < 3; j++)
{
d = rand() % 9000;
a[j][i] = d;
}
int k = 0;
int temp;//Temporary Storage
int flag = 0;
//Sorting algorithm
for (int n = 0; n <= noe*cons; n++)
{
if ((k % 2 == 0) && (flag == 0))
{
//Sorting Upper Triangle
//3 row check
if (a[0][k]>a[0][k + 1])
{
temp = a[0][k + 1];
a[0][k + 1] = a[0][k];
a[0][k] = temp;
}
if (a[1][k] > a[1][k + 1])
{
temp = a[1][k + 1];
a[1][k + 1] = a[1][k];
a[1][k] = temp;
}
if (a[2][k] > a[0][k + 2])
{
temp = a[0][k + 2];
a[0][k + 2] = a[2][k];
a[2][k] = temp;
}
//First Column Check
{
if (a[0][k] > a[1][k])
{
temp = a[1][k];
a[1][k] = a[0][k];
a[0][k] = temp;
}
if (a[1][k] > a[2][k])
{
temp = a[2][k];
a[2][k] = a[1][k];
a[1][k] = temp;
}
if (a[0][k] > a[1][k])
{
temp = a[1][k];
a[1][k] = a[0][k];
a[0][k] = temp;
}
}
//Second Column Check
{
if (a[0][k + 1] > a[1][k + 1])
{
temp = a[1][k + 1];
a[1][k + 1] = a[0][k + 1];
a[0][k + 1] = temp;
}
if (a[1][k + 1] > a[0][k + 2])
{
temp = a[0][k + 2];
a[0][k + 2] = a[1][k + 1];
a[1][k + 1] = temp;
}
if (a[0][k + 1] > a[1][k + 1])
{
temp = a[1][k + 1];
a[1][k + 1] = a[0][k + 1];
a[0][k + 1] = temp;
}
}
//3 Diagonal Checks
if (a[0][k + 1] < a[1][k])
{
temp = a[1][k];
a[1][k] = a[0][k + 1];
a[0][k + 1] = temp;
}
if (a[2][k] > a[1][k + 1])
{
temp = a[1][k + 1];
a[1][k + 1] = a[2][k];
a[2][k] = temp;
}
if (a[2][k] > a[0][k + 1])
{
temp = a[0][k + 1];
a[0][k + 1] = a[2][k];
a[2][k] = temp;
}
//Upper Triangle Sorted
k = k + 2;
if (k == (col - 1))flag = 1;
}
else if ((k % 2 == 0) && (flag == 1))
{
//Sorting Lower Triangle
//3 row check
if (a[2][k - 2]>a[0][k])
{
temp = a[0][k];
a[0][k] = a[2][k - 1];
a[2][k - 2] = temp;
}
if (a[1][k - 1] > a[1][k])
{
temp = a[1][k];
a[1][k] = a[1][k - 1];
a[1][k - 1] = temp;
}
if (a[2][k - 1] > a[2][k])
{
temp = a[2][k];
a[2][k] = a[2][k - 1];
a[2][k - 1] = temp;
}
//First Column Check
{
if (a[2][k - 2] > a[1][k - 1])
{
temp = a[1][k - 1];
a[1][k - 1] = a[2][k - 2];
a[2][k - 2] = temp;
}
if (a[1][k - 1] > a[2][k - 1])
{
temp = a[2][k - 1];
a[2][k - 1] = a[1][k - 1];
a[1][k - 1] = temp;
}
if (a[2][k - 2] > a[1][k - 1])
{
temp = a[1][k - 1];
a[1][k - 1] = a[2][k - 2];
a[2][k - 2] = temp;
}
}
//Second Column Check
{
if (a[0][k] > a[1][k])
{
temp = a[1][k];
a[1][k] = a[0][k];
a[0][k] = temp;
}
if (a[1][k] > a[2][k])
{
temp = a[2][k];
a[2][k] = a[1][k];
a[1][k] = temp;
}
if (a[0][k] > a[1][k])
{
temp = a[1][k];
a[1][k] = a[0][k];
a[0][k] = temp;
}
}
//3 Diagonal Checks
if (a[0][k] < a[1][k - 1])
{
temp = a[1][k - 1];
a[1][k - 1] = a[0][k];
a[0][k] = temp;
}
if (a[2][k - 1] > a[1][k])
{
temp = a[1][k];
a[1][k] = a[2][k - 1];
a[2][k - 1] = temp;
}
if (a[2][k - 1] > a[0][k])
{
temp = a[0][k];
a[0][k] = a[2][k - 1];
a[2][k - 1] = temp;
}
//Lower Triangle Sorted
k = k - 2;
if (k == 0)flag = 0;
}
}
//Code to print the sorted elements
cout << "Sorted Elements:" << endl;
for (int i = 0; i < col; i++)
for (int j = 0; j < 3; j++)
{
cout << a[j][i] << " ";
}
//Code to check if the elements are sorted or not
int l = 0;
int s = a[0][0];
for (int i = 0; i < col;i++)
for (int j = 0; j < 3; j++)
{
if (s > a[j][i])l++;
s = a[j][i];
}
if (l == 0)cout << "\nSorted Array!\n";
else cout << "\nUnsorted!\n";
system("pause");
goto A;
}