Sorting array

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);
}
Does this help:

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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <string>
 
using namespace std;
 
class IntegerArray
{
private:
	int* numArray;
	int array_size;

	static void sort(int*& arr, const int size)
	{		
		for (int n = 0; n < size; ++n)
		{
			for (int m = 0; m < size-1; ++m)
			{
				if (arr[m] > arr[m+1])
				{
					int x = arr[m];
					arr[m] = arr[m+1];
					arr[m+1] = x;
				}
			}
		}
	}
 
public: 
	IntegerArray()
		: numArray(NULL)
		, array_size(0)
	{
	}
 
	~IntegerArray()
	{
		if (numArray)
		{
			delete[] numArray;
			numArray = NULL;
		}
	}

	void selectionSort()
	{
		cout << "Please enter the amount of random numbers to be generated: ";
		cin >> array_size;

		srand(time(0));
		numArray = new int[array_size];

		cout << "Unsorted" << endl;
	
		for(int n = 0; n < array_size; ++n)
		{
			numArray[n] = 1 + (rand() % 10000);
			cout << numArray[n] << " ";
		}

		sort(numArray, array_size);
	}
 };
 
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);
 } 
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
Has to be sorted based upon the selection sort algorithm

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);
}
You should have told that before dude. Selection sort is easier but insertion sort algorithm is considered better. Chekth function out-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Sel Sort(int Ar[], int size)
{
 int small=0,pos=0,tmp=0;
 for(int i=0;i<size;i++)
 {
 	small=Ar[i];
 	for(int j=i+1;j<size;j++)
 	{
 		if(Ar[j]<small)
 		{
 			small=Ar[j];
 			pos=j;
		}
 	}
 tmp=Ar[i];
 Ar[i]=Ar[pos];
 Ar[pos]=tmp;
 }
 for(i=0;i<size;i++)
 	cout<<Ar[i]<<" ";
}

While calling the function call by-SelSort(array,n), Here array is the array you want to sort and n is the number of numbers in the array.
Topic archived. No new replies allowed.