Question: // Shell Sort // I want to use three different types of loops here #include #include #include using namespace std; // Functions prototype int myRand();
// Shell Sort
// I want to use three different types of loops here
#include
#include
#include
using namespace std;
// Functions prototype
int myRand();
void sortArray(int shellSort[], int number);
void displayArray(int shellSort[]);
int main()
{
int myArray[30];
srand((unsigned)time(NULL));
for(int i = 0; i < 20; i++)
{
myArray[i] = myRand();
}
sortArray(myArray, 20); // Calling function sortArray
displayArray(myArray); // Calling function displayArray
return 0;
}
int myRand()
{
return(1 + rand() % 50); // return random number from 1 to 50
}
void sortArray(int shellSort[], int number)
{
int temp;
for(int x = 0; x < number; x++)
{
for(int j = 0; j < number; j++)
{
if(shellSort[j] > shellSort[j + 1])
{
temp = shellSort[j];
shellSort[j] = shellSort[j + 1];
shellSort[j + 1] = temp;
}
}
}
}
void displayArray(int shellSort[])
{
for(int y = 0; y < 20; y++)
{
cout << shellSort[y];
cout << endl;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
