[C++] Attached is a flowchart to do a Shell Sort. In order to do the Shell Sort,
ID: 3682534 • Letter: #
Question
[C++] Attached is a flowchart to do a Shell Sort. In order to do the Shell Sort, you must use all three different types of loops. Your assignment is to write a program that will that a list of 20 random numbers from 1 to 50 inclusively and sort the numbers. This will be a menu driven program where the user will be able to pick which of the 4 sorting methods they would like to use - Bubble Sort, Selection Sort, Dan Sort and the Shell Sort. Make sure that you display the original list as well as the sorted lists of the number. A Program must be user-friendly and well documented both internally and externally.
https://www.dropbox.com/s/f1242hsznzd5a39/Shell%20Sort.pdf?dl=0
Explanation / Answer
#include<iostream>
#include <iomanip>
#include<stdlib.h>
void bobbleSort(int ar[],int size);
void selectionSort(int ar[],int size);
void shellSort(int ar[],int size);
void danSort(int ar[],int size);
int main()
{
int a[20];
int ch;
//initialising the array with random numbers
for(int i=0;i<20;i++)
{
a[i]=(1 + (rand() % 50));
}
/// selecting the sort method
std::cout<<"Enter choice 1:bobble sort 2:selection sort 3:shell sort 4:dan sort ";
std::cin>>ch;
switch(ch)
{
case 1:bobbleSort(a,20);break;//call for bubblesort
case 2:selectionSort(a,20);break;//call for selectionsort
case 3:shellSort(a,20);break;//call for shellsort
case 4:danSort(a,20);break;//call for dansort
default:std::cout<<"Invalid choice";
}
system("pause");
return 0;
}
//shell sort program
void shellSort (int ar[], int size)
{
int j;
std::cout<<"List before sort ";
for(int i=0;i<size;i++)
{
std::cout<<ar[i]<<" ";
}
std::cout<<" ";
for (int gap = size / 2; gap > 0; gap /= 2)
{
for (int i = gap; i < size; ++i)
{
int temp = ar[i];
for (j = i; j >= gap && temp < ar[j - gap]; j -= gap)
{
ar[j] = ar[j - gap];
}
ar[j] = temp;
}
}
std::cout<<"List after sort ";
for(int i=0;i<size;i++)
{
std::cout<<ar[i]<<" ";
}
std::cout<<" ";
}
//bubble sort prog
void bobbleSort(int ar[], int size)
{
int temp;
std::cout<<"List before sort ";
for(int i=0;i<size;i++)
{
std::cout<<ar[i]<<" ";
}
std::cout<<" ";
for(int p=1;p<size;p++) // Loop for Pass
{
for(int j=1;j<size;j++)
{
if(ar[j]>ar[j+1])
{
temp=ar[j]; // Interchange Values
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
std::cout<<"List after sort ";
for(int i=0;i<size;i++)
{
std::cout<<ar[i]<<" ";
}
std::cout<<" ";
}
//selection sort prog
void selectionSort(int ar[], int size)
{
int temp;
std::cout<<"List before sort ";
for(int i=0;i<size;i++)
{
std::cout<<ar[i]<<" ";
}
std::cout<<" ";
int pos_min;
for (int i=0; i < size; i++)
{
pos_min = i;//set pos_min to the current index of array
for (int j=i+1; j < size; j++)
{
if (ar[j] < ar[pos_min])
pos_min=j;
//pos_min will keep track of the index that min is in, this is needed when a swap happens
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
temp = ar[i];
ar[i] = ar[pos_min];
ar[pos_min] = temp;
}
}
std::cout<<"List after sort ";
for(int i=0;i<size;i++)
{
std::cout<<ar[i]<<" ";
}
std::cout<<" ";
}
void danSort(int ar[], int size)
{
int j, temp;
std::cout<<"List before sort ";
for(int i=0;i<size;i++)
{
std::cout<<ar[i]<<" ";
}
std::cout<<" ";
for (int i = 0; i < size; i++){
j = i;
while (j > 0 && ar[j] < ar[j-1]){
temp = ar[j];
ar[j] = ar[j-1];
ar[j-1] = temp;
j--;
}
}
std::cout<<"List after sort ";
for(int i=0;i<size;i++)
{
std::cout<<ar[i]<<" ";
}
std::cout<<" ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.