hi
I had this problem where I was asked to :
Write a program that compares and records the time taken to sort an array of social security numbers using four sorting algorithms.
The program must generate random social security numbers. Each group must do an extensive testing on several randomly generated arrays so that a meaningful comparative analysis can be made on different sorting methods. Increase the size of the input to get a growth rate for each algorithm.
You are assigned to do elementary sorting methods: Insertion sort, Quick sort, Merge Sort, Heap Sort . Generate random SSNs as 9 characters: "023568708" using rand().
my question is :
the code is for integer data type and we need to change it to character type, BUT when I print the output it have to appear in this form:
12547865
25044745
01247855
01247524
75520455
each number consist of nine digits and must be stored as character data type. I tried to swiching to character type but then each number or character is printed in this form:
4
d
q
5
g
t
r
I am going to post the code in another thread because its long.
any help will be highly appreciated.
thanks
ayman
-----------------
3 1339
here is the code I came up with. I had to split it, please copy and past the two halves so you would have an idea about the program. - #include <iostream>
-
#include <CONIO.H>
-
#include <STDLIB.H>
-
#include <STDIO.H>
-
#include <TIME.H>
-
#include <FSTREAM>
-
///////////////////////////////////////////////////////
-
using namespace std ;
-
const int arrSize=30000;
-
fstream outfile ("SortingAlgorithms.txt");
-
//////////////////////////////////////////////////////
-
void insert_special( int x, int heap[], int start, int end ){
-
// insert x into the sub-heap from start to end
-
// location start is empt
-
int child = 2*start + 1; // left child of start
-
while (child <= end) {
-
if (child < end && heap[child] < heap[child + 1])
-
child++; // child is the larger child of start
-
if (x > heap[child])
-
break; // x stays in start
-
else {
-
heap[start] = heap[child];
-
start = child;
-
child = 2*start + 1;
-
}
-
}
-
heap[start] = x;
-
}
-
void buildHeap(int a[], int size )
-
// Build a heap from an array of items
-
// Binary tree with one node satisfies heap properties
-
// Therefore ignore leaves and start from the parent
-
// of the last item
-
{
-
int i;
-
for (i = (size - 2)/2; i >= 0; i--) // start from parent of last item
-
insert_special( a[i], a, i, size-1);
-
}
-
-
void heapSort( int a[], int size )
-
// sort an array of the given size
-
{
-
int i;
-
int temp;
-
buildHeap( a, size );
-
for (i = size-1; i >=1; i--) {
-
temp = a[i]; // extract the last element from the array
-
a[i] = a[0]; // move top of heap to the end of the array
-
insert_special( temp, a, 0, i-1 ); // restore heap properties
-
}
-
}
-
//////////////////////////////////////////////////////
-
void mergeConquer(int a[], int left, int mid, int right) {
-
int x=0;//counters
-
int y=0;
-
int z=0;
-
int lno = mid - left + 1;
-
int rno = right - mid;
-
int *L = new int[lno];
-
int *R = new int[rno];
-
//////////////////////////////////////
-
for ( y = 0; y < lno; y++) {
-
L[y] = a[left + y];
-
}
-
////////////////////////////////////
-
for (z = 0; z < rno; z++) {
-
R[z] = a[mid + z + 1];
-
}
-
y = 0;
-
z = 0;
-
/////////////////////////////////
-
for (int i = left; i <= right; i++) {
-
if ( (y < lno) && (z < rno)) {
-
if (L[y] <= R[z]) {
-
a[i] = L[y];
-
y++;
-
}
-
else {
-
a[i] = R[z];
-
z++;
-
}
-
}
-
else if ( (y < lno) && (z >= rno)) {
-
a[i] = L[y];
-
y++;
-
}
-
else if ( (y >= lno) && (z < rno)) {
-
a[i] = R[z];
-
z++;
-
}
-
-
}
-
-
}
-
///////////////////////////////////////////////////////////////////////////////////////
-
void mergeDivide(int a[], int left, int right) {
-
int mid = (left + right) / 2;
-
if (left < right) {
-
//cout << "============";
-
mergeDivide(a, left, mid);
-
mergeDivide(a, mid + 1, right);
-
mergeConquer(a, left, mid, right);
-
}
-
-
}
-
-
/////////////////////////////////////////////////////
-
void swap(int array[], int index1, int index2)
-
{
-
int temp = array[index1];
-
array[index1] = array[index2];
-
array[index2] = temp;
-
}
-
//////////////////////////////////////////////////////////////////////////////////////////
-
void quickSort(int array[], int start, int end)
-
{
-
int i = start;//counters
-
int k = end;
-
-
if (end - start >= 1)
-
{
-
int pivot = array[start];
-
-
while (k > i)
-
{
-
while (array[i] <= pivot && i <= end && k > i)
-
i++;
-
while (array[k] > pivot && k >= start && k >= i)
-
k--;
-
if (k > i)
-
swap(array, i, k);
-
}
-
swap(array, start, k);
-
-
quickSort(array, start, k - 1);
-
quickSort(array, k + 1, end);
-
}
-
else
-
{
-
return;
-
}
-
}
-
//////////////////////////////////////////////////////////////////////////////////////////
-
void sortItInQuick(int array[])
-
{
-
quickSort(array, 0, arrSize-1);
-
}
-
-
////////////////////////////////////////////////////////////////////////////////////////
-
void shuffelArray(int a[] ){
-
int x;
-
srand ( time(NULL) );
-
for(int i=0;i< arrSize;i++){
-
-
x=(((rand()%1000)*2)+165959802);
-
//x=(rand()%100);
-
//cout<<x<<endl;
-
a[i]=x;
-
//getch();
-
}
-
-
getch();
-
cout<<endl<<"------------------------------------------- end intioalzition"<<endl;
-
}
-
////////////////////////////////////////////////////////////
-
void printOnFile(int arr[],char name[],int time){
-
outfile<<endl<<"-------------------------------------------------------------------"<<endl;
-
outfile<<"-------------------------------------------------------------------"<<endl;
-
outfile<<"-------------------------------------------------------------------"<<endl;
-
outfile<<"-------------------------------------------------------------------"<<endl;
-
outfile<<name<<endl<<"and the time is : "<<time<<endl;
-
outfile<<"-------------------------------------------------------------------"<<endl;
-
outfile<<"-------------------------------------------------------------------"<<endl;
-
outfile<<"-------------------------------------------------------------------"<<endl;
-
outfile<<"-------------------------------------------------------------------"<<endl;
-
/*for(int i=0;i<arrSize;i++){
-
outfile <<arr[i]<<" ";
-
}*/
-
-
}
-
////////////////////////////////////////////////////////////
-
void copyArray(int arr1[],int arr2[],int size){
-
arr2=new int[size];
-
for(int i=0;i<size;i++)
-
arr2[i]=arr1[i];
-
-
}
-
////////////////////////////////////////////////////////////
-
-
void sortInsertions(int a[]) {
-
-
for (int j = 2; j < arrSize; j++) {
-
for (int k = 0; k < j; k++) {
-
if (a[j] < a[k]) {
-
int temp = a[k];
-
a[k] = a[j];
-
a[j] = temp;
-
}
-
}
-
}
-
-
}
-
-
//////////////////////////////////////////////////////
- void main() {
-
int arr[arrSize];
-
int temp[arrSize];
-
int choice =-1;
-
/////////////////////////////////////////////
-
cout<<"Array is shffiling ";
-
shuffelArray(arr);
-
cout<<endl<<"-------------------------------------------"<<endl;
-
////////////////////////////////////////////
-
while (choice !=0){
-
cout<<"-------------------------------------------"<<endl;
-
cout<<"-------------------------------------------"<<endl;
-
cout<<"1- Quick Sort "<<endl;
-
cout<<"2- inseration Sort "<<endl;
-
cout<<"3- merage Sort "<<endl;
-
cout<<"4- Heap Sort "<<endl;
-
cout<<"-------------------------------------------"<<endl;
-
cout<<"Enter your choice :";
-
cin>>choice ;
-
switch (choice) {
-
case 1:{
-
//cout<<"THis choice is 1 ";
-
copyArray(arr,temp,arrSize);
-
time_t start=time(NULL);
-
cout<<"---------------------be4 quick the time on start : "<<start<<endl<<endl;
-
sortItInQuick(temp);
-
time_t end=time(NULL);
-
printOnFile (temp,"Quick Sort",end-start);
-
cout<<endl<<"---------------------after quick the time is end-start: "<<end-start<<endl<<endl;
-
cout<<"The Array After sorted ";
-
getch();
-
}
-
break ;
-
case 2 :{
-
copyArray(arr,temp,arrSize);
-
time_t start=time(NULL);
-
cout<<"---------------------be4 inseration : and the time is : "<<start<<endl<<endl;
-
sortInsertions (temp);
-
time_t end=time(NULL);
-
//print (temp);
-
printOnFile (temp,"Inseration Sort",end-start);
-
cout<<endl<<"---------------------after inseration: and the time is : "<<end-start<<endl<<endl;
-
cout<<"The Array After sorted ";
-
-
getch();
-
}
-
break;
-
case 3 :{
-
copyArray(arr,temp,arrSize);
-
time_t start=time(NULL);
-
cout<<"---------------------be4 mearge : and the time is : "<<start<<endl<<endl;
-
mergeDivide (temp,0,arrSize-1);
-
time_t end=time(NULL);
-
//print (temp);
-
printOnFile (temp,"Merage Sort",end-start);
-
cout<<endl<<"---------------------after mearge : and the time is : "<<end-start<<endl<<endl;
-
cout<<"The Array After sorted ";
-
-
getch();
-
}
-
break;
-
case 4 :{
-
copyArray(arr,temp,arrSize);
-
time_t start=time(NULL);
-
cout<<"---------------------be4 heap : and the time is : "<<start<<endl<<endl;
-
heapSort(temp,arrSize);
-
time_t end=time(NULL);
-
printOnFile (temp,"Heap Sort",end-start);
-
cout<<endl<<"---------------------after heap: and the time is : "<<end-start<<endl<<endl;
-
cout<<"The Array After sorted ";
-
-
getch();
-
}
-
break;
-
choice=-1;
-
}
-
}
-
outfile.close();
-
-
}
Ganon11 3,652
Recognized Expert Specialist
ayman:
Please find time to read our FAQ, which includes the guidelines for posting a question. Specifically, (a) please do not use 3 threads for 1 question - adding replies to a single thread would have been sufficient, and (b) please use [code] tags when posting your code.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: baustin75 |
last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie
only when debugging in php designer 2005
--------------------------------------------------------------------------------
Hello,
I have a very simple problem but cannot seem to figure it out. I have a
very simple php script that sends a test email to myself. When I debug
it in PHP designer, it works with no problems, I get the test email. If
|
by: Wayne |
last post by:
I'm using the following code from the Access Web to open a folder. The
folder opens in "List" View. Is there an addition that I can make to
the code to force the folder to open in "Details" view?
'This code was originally written by Ken Getz.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
|
by: Warren Sirota |
last post by:
Hi,
Please let me know if I am interpreting this correctly. I've done a
little testing of the difference between passing parameters byVal and
byRef, and the results were slightly non-intuitive, as I expected. I
haven't seen this behavior explained precisely in the .net world yet,
so I wanted to check and make sure I've got it right.
I apologize that this is a bit long. I've tried to keep it concise.
There are code fragments here, but...
|
by: Siv |
last post by:
Hi,
If I run the following:
strSQL = "Select * FROM Clients;"
da = New OleDb.OleDbDataAdapter(strSQL, Conn) 'Create data adapter
cb = New OleDb.OleDbCommandBuilder(da) 'Create command builder using the datadapter
dt = New Data.DataTable
da.Fill(dt) 'pour in the data using the adapter
|
by: ZillionDollarSadist |
last post by:
Hello,
I'm working at a simple Access '97 + VB4 application, and I ran into a
terrible problem: something I never modified now gives me a totally
unwanted "Invalid use of null" error.
It happens in a Text.LostFocus event, this block:
Do While Not TB6.EOF
If Year(TB6(0)) = Val(Trim(Text1.Text)) Then
!!!!!!!!!!!!!Here!!!!!!!!!
...
| |
by: Rahul |
last post by:
Hi Everybody
I have some problem in my script. please help me. This is script file.
I have one *.inq file. I want run this script in XML files. But this
script errors shows . If u want i am attach this script
files and inq files. I cant understand this error. Please suggest me.
You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online.
Plz....Plz..Plz...
|
by: ayman723 |
last post by:
hi
I had this problem where I was asked to :
Write a program that compares and records the time taken to sort an array of social security numbers using four sorting algorithms.
The program must generate random social security numbers. Each group must do an extensive testing on several randomly generated arrays so that a meaningful comparative analysis can be made on different sorting methods. Increase the size of the input to get a growth...
|
by: SOI_0152 |
last post by:
Hi all!
Happy New Year 2008. Il hope it will bring you love and happyness
I'm new on this forum.
I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c.
Everything works fine.
Now we decided to move from mainframe IMS-DB2 to Windows 2003 server-DB2 UDB
for LUW 9.5.
|
by: amija0311 |
last post by:
Hi,
I am new using DB2 9.1 database by windows base.
I want to query the data that contain string then translate the string into integer using DB2.
The problems is If the data is null, i got the problem to translate.
How to translate string also allow null data to integer. If null data it will read as space.
My Data :-
GEOSEG_ID SEQNO
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |