I had to pick one inefficient and one efficient sorting methods, then implement them in a c++ program:
Generate 100,000 or any proper number of random integers and store them in an array
I have comprised two such function and just want to ensure I'm meeting specifications.
Please see attached code
This is the inefficient code
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
void PrintArray(int* array, int n);
void selectionSort(int *array,int length);
int main(void)
{
int ARRAY_SIZE;
cout<< "How many random numbers would you like generated? " << endl;
cin >> ARRAY_SIZE;
int* array = new int[ARRAY_SIZE];
srand((unsigned)time(0));
for(int i=0; i<ARRAY_SIZE; i++){
array[i] = (rand()%100000)+1;
}
PrintArray(array, ARRAY_SIZE);
selectionSort(array,ARRAY_SIZE);
cout<<endl<<"The list has been sorted, now it is : "<<endl;
PrintArray(array, ARRAY_SIZE);
cin.get();
cin.get();
system("PAUSE");
delete[] array;
return 0;
}
void selectionSort(int *array,int size)
{
int i,j,tmp,temp;
for(i=0;i<(size-1);i++)
{
temp=i;
tmp=array[i];
for(j=i+1;j<(size);j++)
{
if(tmp>array[j])
{
temp=j;
tmp=array[j];
}
}
int tmp1=array[i] ;
array[i]=array[temp];
array[temp]=tmp1;
}
}
void PrintArray(int* array, int n)
{
int i;
for( i = 0; i < n; i++) cout<<array[i]<<'\t';
}