Question

In: Computer Science

Develop a program that creates just 3 identical arrays, mylist_1, mylist_2, and mylist_3, of just 250...

Develop a program that creates just 3 identical arrays, mylist_1, mylist_2, and mylist_3, of just 250 items. For this assignment, the program then will sort mylist_1 using a bubble sort, mylist_2 using a selection sort, and mylist_3 will be using an insertion sort and outputs the number of comparisons and item assignments made by each sorting algorithm.

template <class elemType>
int seqSearch(const elemType list[], int length, const elemType& item)
{
int loc;
bool found = false;

loc = 0;

while (loc < length && !found)
if (list[loc] == item)
found = true;
else
loc++;

if (found)
return loc;
else
return -1;
} //end seqSearch


template <class elemType>
int binarySearch(const elemType list[], int length,
const elemType& item)
{
int first = 0;
int last = length - 1;
int mid;

bool found = false;

while (first <= last && !found)
{
mid = (first + last) / 2;

if (list[mid] == item)
found = true;
else if (list[mid] > item)
last = mid - 1;
else
first = mid + 1;
}

if (found)
return mid;
else
return -1;
} //end binarySearch

template <class elemType>
void bubbleSort(elemType list[], int length)
{
for (int iteration = 1; iteration < length; iteration++)
{
for (int index = 0; index < length - iteration;
index++)
{
if (list[index] > list[index + 1])
{
elemType temp = list[index];
list[index] = list[index + 1];
list[index + 1] = temp;
}
}
}
} //end bubbleSort

template <class elemType>
void selectionSort(elemType list[], int length)
{
int minIndex;

for (int loc = 0; loc < length; loc++)
{
minIndex = minLocation(list, loc, length - 1);
swap(list, loc, minIndex);
}
} //end selectionSort

template <class elemType>
void swap(elemType list[], int first, int second)
{
elemType temp;

temp = list[first];
list[first] = list[second];
list[second] = temp;
} //end swap

template <class elemType>
int minLocation(elemType list[], int first, int last)
{
int minIndex;

minIndex = first;

for (int loc = first + 1; loc <= last; loc++)
if (list[loc] < list[minIndex])
minIndex = loc;

return minIndex;
} //end minLocation

template <class elemType>
void insertionSort(elemType list[], int length)
{
for (int firstOutOfOrder = 1; firstOutOfOrder < length;
firstOutOfOrder++)
if (list[firstOutOfOrder] < list[firstOutOfOrder - 1])
{
elemType temp = list[firstOutOfOrder];
int location = firstOutOfOrder;

do
{
list[location] = list[location - 1];
location--;
}
while(location > 0 && list[location - 1] > temp);

list[location] = temp;
}
} //end insertionSort

template <class elemType>
void quickSort(elemType list[], int length)
{
recQuickSort(list, 0, length - 1);
} //end quickSort

template <class elemType>
void recQuickSort(elemType list[], int first, int last)
{
int pivotLocation;

if (first < last)
{
pivotLocation = partition(list, first, last);
recQuickSort(list, first, pivotLocation - 1);
recQuickSort(list, pivotLocation + 1, last);
}
} //end recQuickSort

template <class elemType>
int partition(elemType list[], int first, int last)
{
elemType pivot;

int smallIndex;

swap(list, first, (first + last) / 2);

pivot = list[first];
smallIndex = first;

for (int index = first + 1; index <= last; index++)
if (list[index] < pivot)
{
smallIndex++;
swap(list, smallIndex, index);
}

swap(list, first, smallIndex);

return smallIndex;
} //end partition

template <class elemType>
void heapSort(elemType list[], int length)
{
buildHeap(list, length);

for (int lastOutOfOrder = length - 1; lastOutOfOrder >= 0;
lastOutOfOrder--)
{
elemType temp = list[lastOutOfOrder];
list[lastOutOfOrder] = list[0];
list[0] = temp;
heapify(list, 0, lastOutOfOrder - 1);
}//end for
}//end heapSort

template <class elemType>
void heapify(elemType list[], int low, int high)
{
int largeIndex;

elemType temp = list[low]; //copy the root node of
//the subtree

largeIndex = 2 * low + 1; //index of the left child

while (largeIndex <= high)
{
if (largeIndex < high)
if (list[largeIndex] < list[largeIndex + 1])
largeIndex = largeIndex + 1; //index of the
//largest child

if (temp > list[largeIndex]) //subtree
//is already in a heap
break;
else
{
list[low] = list[largeIndex]; //move the larger
//child to the root
low = largeIndex; //go to the subtree to
//restore the heap
largeIndex = 2 * low + 1;
}
}//end while

list[low] = temp; //insert temp into the tree,
//that is, list
}//end heapify

template <class elemType>
void buildHeap(elemType list[], int length)
{
for (int index = length / 2 - 1; index >= 0; index--)
heapify(list, index, length - 1);
}

Solutions

Expert Solution

CODE:

CODE(for copy):

#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

void bubbleSort(int list[], int length)
{
int comparison = 0, item_assignment = 0;
item_assignment++; //Assigning iteration=1;
for (int iteration = 1; iteration < length; iteration++)
{
comparison++; //Comparing iteration < length
item_assignment++; //Assigning index=0;
for (int index = 0; index < length - iteration; index++)
{
comparison++; //Comparing index < length - iteration
if (list[index] > list[index + 1])
{
comparison++; //Comparing list[index] > list[index + 1]
int temp = list[index];
list[index] = list[index + 1];
list[index + 1] = temp;
item_assignment+=3; //Assigning above three statement
}
}
}
cout<<"Bubble Sort\n";
cout<<"Number of comparison: "<<comparison<<"\n";
cout<<"Number of item assignment: "<<item_assignment<<"\n";
} //end bubbleSort


void swap(int list[], int first, int second)
{
int temp;

temp = list[first];
list[first] = list[second];
list[second] = temp;
} //end swap


int minLocation(int list[], int first, int last, int &comp, int &assignment)
{
int minIndex;
assignment++; //Assigning minIndex=first;
minIndex = first;
assignment++; //Assigning loc=first+1;
for (int loc = first + 1; loc <= last; loc++)
{
comp++; //Comparing loc <= last
if (list[loc] < list[minIndex])
{
comp++; //Comparing list[loc] < list[minIndex]
minIndex = loc;
assignment++; //Assigning minIndex = loc;
}
}

return minIndex;
} //end minLocation


void selectionSort(int list[], int length)
{
int minIndex, comparison = 0, item_assignment = 0;

item_assignment++; //Assigning loc=0;
for (int loc = 0; loc < length; loc++)
{
comparison++; //Comparing loc < length
minIndex = minLocation(list, loc, length - 1, comparison, item_assignment);
swap(list, loc, minIndex);
item_assignment+=3; //Assignment in swap condition
}
cout<<"Selection Sort\n";
cout<<"Number of comparison: "<<comparison<<"\n";
cout<<"Number of item assignment: "<<item_assignment<<"\n";
} //end selectionSort

void insertionSort(int list[], int length)
{
int comparison = 0, item_assignment = 0;
item_assignment++; //Assigning firstOutOfOrder = 1
for (int firstOutOfOrder = 1; firstOutOfOrder < length; firstOutOfOrder++)
{
comparison++; //Comparing firstOutOfOrder < length
if (list[firstOutOfOrder] < list[firstOutOfOrder - 1])
{
comparison++; //Comparing firstOutOfOrder < length
int temp = list[firstOutOfOrder];
int location = firstOutOfOrder;
item_assignment+=2; //Assigning of above 2 statement
do
{
list[location] = list[location - 1];
location--;
item_assignment++; //Assigning list[location] = list[location - 1]
comparison+=2; //Comparing while statement
}
while(location > 0 && list[location - 1] > temp);

list[location] = temp;
item_assignment++; //Assigning list[location]=temp
}
}
cout<<"Insertion Sort\n";
cout<<"Number of comparison: "<<comparison<<"\n";
cout<<"Number of item assignment: "<<item_assignment<<"\n";
} //end insertionSort


void Create_Array(int mylist[], int len)
{
srand(time(0));
for(int i=0; i < len; i++)
{
mylist[i] = rand() % 10000;
}
}


int main()
{
int mylist_1[250], mylist_2[250], mylist_3[250];
Create_Array(mylist_1, 250);
for(int i=0; i<250; i++)
{
mylist_2[i]=mylist_1[i];
mylist_3[i]=mylist_1[i];
}
bubbleSort(mylist_1, 250);
selectionSort(mylist_2, 250);
insertionSort(mylist_3, 250);
return 0;
}

OUTPUT:

For any doubt please comment.


Related Solutions

USING MatLab (Arrays) Set ASIZE to 5. Write a program that creates an array of ASIZE...
USING MatLab (Arrays) Set ASIZE to 5. Write a program that creates an array of ASIZE numeric elements. Prompt the User for ASIZE numbers and store them in the array. After storing the values, calculate the sum of all the values in the array and display the sum. Modify the program you wrote for Problem 5 such that it calculates the product of all the values instead of the sum. Modify the program you wrote in Problem 6 such that...
Write a program that uses two identical arrays of at least 25 integers. It should call...
Write a program that uses two identical arrays of at least 25 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in descending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it makes. Display these values on...
In C++, write a program that uses two identical arrays of ten randomly ordered integers. It...
In C++, write a program that uses two identical arrays of ten randomly ordered integers. It should display the contents of the first array, then call a function to sort it using the most efficient descending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using descending order selection sort, modified to print out the...
Java Proect Project Outcomes Develop a Java program that: • creates a user designed class •...
Java Proect Project Outcomes Develop a Java program that: • creates a user designed class • uses proper design techniques including reading UML Class Diagrams • reads input from the keyboard using a Scanner Object and its methods • uses selection (if and if else) statements • uses iteration (while, do while or for) statements • uses String comparison methods. • follows standard acceptable programming practices. • handles integer overflow errors Prep Readings: Zybooks chapter 1 - 6 1. General...
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers....
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using an ascending order selection sort, modified to print out the...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions with arguments. Create programs with small functions where main goes to a series of functions where the real work takes place. Don’t use global variables and don’t use break within a loop (unless working with a switch statement). Functions can’t have more than 30 statements of code, not including comments, blank lines, or variable definitions. Don’t use a return in the middle of the...
for Python 3 Write a python program that Creates a list that has these values in...
for Python 3 Write a python program that Creates a list that has these values in this order, 'Python', 'JavaScript', and 'PHP' Define a displayMyClasses function that sorts the list and then displays each item on the list, one per line, in sorted order. Also, number the displayed list as shown in the "Running a Sample Program" shown below. Define a guessNext function that selects a random element from the list and returns it to the call of the function....
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list...
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list of homework assignments. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services: • Add a new assignment. • Remove an assignment. • Provide a list of the assignments in the order they were assigned. • Find the assignment(s) with the earliest...
This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files...
This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files into a new C++ project.    NOTE: if your IDE does not allow you to create projects - or you are not sure how to do it - then you may combine the two cpp files into a single file. 2) Complete the program by adding code the the class methods. You may want to study the existing code first to get a feel...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT