Jan 30, 2013 at 4:05pm UTC
I want to implement a function into the code below that sorts the user defined amount of random numbers and then sorts them in ascending order.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;
class IntegerArray
{
private:
int size;
int* numArray;
public :
int input;
int x, i, j;
int min, max;
IntegerArray()
:numArray(NULL),
size(0)
{
}
~IntegerArray()
{
if (numArray != NULL)
{
delete [] numArray;
}
}
void selectionSort()
{
int first, temp;
int* num;
int numLength;
cout << "Please enter the amount of random numbers to be generated: ";
cin >> input;
srand(time(0));
numArray = new int[input];
cout << "Unsorted" << endl;
for(x = 1; x <= input; x++)
{
numArray[x] = 1+(rand()%10000);
cout << numArray[x] << " ";
}
}
};
void main()
{
IntegerArray ArrayObject;
ArrayObject.selectionSort();
clock_t startTime = clock();
clock_t endTime = clock();
cout << endl;
cout << "Execution time: " << endTime - startTime << " ticks" << endl;
cin.ignore(2);
}
Jan 30, 2013 at 5:50pm UTC
Check this code out. BTW sorry it is written on the old Turbo C++, May not work on the new compilers. Please change the code accordingly. This sorting is called Insertion Sorting-
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
#include<iostream.h>
#include<conio.h>
int i=0;
void Insertionsort(int AR[],int length)
{
int tmp=0,j=0;
for (i=1;i<length;i++)
{
tmp=AR[i];
j=i-1;
while (AR[j]>tmp&&j>=0)
{
AR[j+1]=AR[j];
j--;
}
AR[j+1]=tmp;
}
}
int main()
{
int array[30]={'\0' },size=0;
cout<<"Enter the size of array:" ;
cin>>size;
cout<<"Enter the values of the array:" <<endl;
for (i=0;i<size;i++)
cin>>array[i];
Insertionsort(array,size);
cout<<'\a' <<"The sorted array is:" ;
for (i=0;i<size;i++)
cout<<array[i]<<" " ;
return 0;
}
Last edited on Jan 30, 2013 at 5:51pm UTC
Jan 30, 2013 at 6:25pm UTC
Has to be sorted based upon the selection sort algorithm
Jan 30, 2013 at 6:27pm UTC
Changed it this however the sorted numbers do not display on the console:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;
class IntegerArray
{
private:
int size;
int* numArray;
public :
int getSize() { return size; }
int input;
int x;
int min, max;
IntegerArray()
:numArray(NULL),
size(0)
{
}
~IntegerArray()
{
if (numArray != NULL)
{
delete [] numArray;
}
}
void generateArray()
{
cout << "Please enter the amount of random numbers to be generated: ";
cin >> input;
srand(time(0));
numArray = new int[input];
cout << "Unsorted" << endl;
for(x = 1; x <= input; x++)
{
numArray[x] = 1+(rand()%10000);
cout << numArray[x] << " ";
}
}
void selectionSort()
{
int i, j, temp, lowest, at;
int numLength = size;
for (i = 0; i < numLength - 1; i++)
{
temp = numArray[i];
lowest = temp;
at = i;
for(j=i; j < numLength; j++)
{
if (numArray[j] < temp)
{
temp = numArray[j];
at = j;
}
}
numArray[i] = temp;
numArray[at] = lowest;
cout << numArray[i];
}
return;
}
};
void main()
{
IntegerArray ArrayObject;
ArrayObject.generateArray();
cout << endl;
cout << "Selection Sort: " << endl;
ArrayObject.selectionSort();
cout << endl;
cout << "Binary Search: " << endl;
clock_t startTime = clock();
clock_t endTime = clock();
cout << endl;
cout << "Execution time: " << endTime - startTime << " ticks" << endl;
cin.ignore(2);
}