Programming: Programmatically generate three type of arrays (or vectors) of size 10,000: an array that is sorted in ascending order, a reversed array (an array that is sorted in descending order), and a random array. • Programmatically sort the three arrays using bubble sort, selection sort, insertion sort, shell sort, merge sort, and quick sort (the regular one and the improved one ) algorithms. For each sorting algorithm, calculate the number of exchanges (or shifts) and the number of comparisons for these sorting algorithms. Summarize the result in tables (see below). • Discuss whether your results match the big O of these algorithms. • Note: you can use the sorting algorithms on Blackboard. For quick sort, the improved one is called "middle_quick_sort".
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int MAX_VAL = 65536;
int compares; // total number of comparisons
int moves; // total number of moves
int compare(int comparison) {
compares++;
return comparison;
}
void initStats() {
compares = 0;
moves = 0;
}
void move(int *arr, int dest, int source) {
moves++;
arr[dest] = arr[source];
}
void swap(int *arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
moves += 3;
}
/**
* randomArray - creates an array of randomly generated integers
* with the specified number of elements and the specified
* maximum value
*/
int *randomArray(int n, int maxVal) {
int *arr = (int *)malloc(n*sizeof(int));
for (int i = 0; i < n; i++) {
arr[i] = rand()%100;
}
return arr;
}
/*
* indexSmallest - returns the index of the smallest element
* in the subarray from arr[lower] to arr[upper]. Used by
* selectionSort.
*/
int indexSmallest(int *arr, int lower, int upper) {
int indexMin = lower;
for (int i = lower + 1; i <= upper; i++)
if (compare(arr[i] < arr[indexMin]))
indexMin = i;
return indexMin;
}
/** selectionSort */
void selectionSort(int *arr, int n) {
for (int i = 0; i < n - 1; i++) {
int j = indexSmallest(arr, i, n - 1);
swap(arr, i, j);
}
}
/** insertionSort */
void insertionSort(int *arr, int n) {
for (int i = 1; i < n; i++) {
if (compare(arr[i] < arr[i-1])) {
int toInsert = arr[i];
moves++;
int j = i;
do {
move(arr, j, j - 1);
j = j - 1;
} while (j > 0 && compare(toInsert < arr[j-1]));
arr[j] = toInsert;
moves++;
}
}
}
/** bubbleSort */
void bubbleSort(int *arr, int n) {
for (int i = n - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (compare(arr[j] > arr[j+1]))
swap(arr, j, j+1);
}
}
}
/*
* A helper method for qSort that takes the array that begins with
* element arr[first] and ends with element arr[last] and
* partitions it into two subarrays using the middle element of
* the array for the pivot. It returns the index of the last
* element of the left subarray formed by the partition. All
* elements in the left subarray are <= the pivot, and all
* elements in the right subarray are >= the pivot.
*/
int partition(int *arr, int first, int last) {
int pivot = arr[(first + last)/2];
moves++; // for the above assignment
int i = first - 1; // index going left to right
int j = last + 1; // index going right to left
while (1) {
// Moving from left to right, find an element >= the pivot.
do {
i++;
} while (compare(arr[i] < pivot));
// Moving from right to left, find an element <= the pivot.
do {
j--;
} while (compare(arr[j] > pivot));
// Swap the elements so that they end up in the correct
// subarray, or quit if the indices have overlapped or crossed.
if (i < j)
swap(arr, i, j);
else
return j; // index of last element in the left subarray
}
}
/*
* A recursive helper method that actually implements quicksort.
* The initial recursive call is made by quicksort() -- see below.
*/
void qSort(int *arr, int first, int last) {
// Partition the array. split is the index of the last
// element of the left subarray formed by the partition.
int split = partition(arr, first, last);
//
// Note that we only make recursive calls on subarrays that
// have two or more elements, and thus the base case is when
// neither subarray has two or more elements.
//
if (first < split)
qSort(arr, first, split); // left subarray
if (last > split + 1)
qSort(arr, split + 1, last); // right subarray
}
/** quickSort */
void quickSort(int *arr, int n) {
qSort(arr, 0, n - 1);
}
void printArray(int *arr, int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}
void printStats() {
printf("%d comparisons\n ",compares);
}
int main()
{
srand(time(NULL));
int *a = randomArray(10,100);
printf("Original Array: \n");
printArray(a, 10);
printf("\n\ninsertionSort\t\t");
initStats();
insertionSort(a,10);
printStats();
printArray(a, 10);
printf("\n\nselectionSort\t\t");
initStats();
selectionSort(a,10);
printStats();
printArray(a, 10);
printf("\n\nbubbleSort\t\t");
initStats();
bubbleSort(a,10);
printStats();
printArray(a, 10);
printf("\n\nQuickSort\t\t");
initStats();
quickSort(a,10);
printStats();
printArray(a, 10);
}
============================================================
See output

Thanks, PLEASE COMMENT if there is any concern. You can increase
the size and it will work ( I showed it for 10
elements)
Programming: Programmatically generate three type of arrays (or vectors) of size 10,000: an array that is...
Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...
This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...
Please come up with an array of 9 random integers then sort the array. Show the contents of the array each time a sorting algorithm changes it while sorting the array into ascending order. The 6 sorting algorithm we are using are: 1. Selection Sort 3 points 2. Insertion Sort 3 points 3. Shell Sort 6 points 4. Bubble Sort 3 points 5. Merge Sort 10 points 6. Quick Sort 10 points It is OK to do this assignment on...
Need help with program. I'm stuck
Objectives: In this assignment, we will
practice manipulating lists of data and arranging items in an
ascending/descending order. you will also explore storing
lists/arrays using different sorting algorithms including, the
selection sort, bubble sort, and insertion sort algorithm.
Comparison between the three algorithms are made based on the
number of comparisons and item assignments (basic operations) each
algorithms executes.
Background: Ordering the elements of a list is
a problem that occurs in many computer...
Declare an array of integers of size 8 and initialize it with any non-duplicate integer values you like. Don’t enter the values in any order. Print the unsorted array. Sort the array using selection sort or insertion sort in descending order. Print what sort you are using. Write any additional functions that you need for sorting. Keep monitoring the number of comparisons and number of swaps performed while sorting. Report both after sorting. Print the sorted array. in C++
Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...
Hello, IN C++ please help creating a 100% functional program that follows all the guidelines shown below. (I WILL RATE, NO INCOMPLETE OR OTHER PEOPLE'S SOLUTIONS PLEASE): THANK YOU. a. Randomly generate three integer arrays A1, A2, and A3 of sizes N=103 , 105 , and 107 (or 106 , if your computer cannot handle an integer array of size 107 ), in that order. b. Run all the four sorting algorithms (insertion, merge, heap and quick sort) we’ve learned...
HOME WORK 9. Sorting Benchmarks Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending 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...
C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential search of a list assumes that the list elements are sorted in ascending order. b. A binary search of a list assumes that the list is sorted. 2. Consider the following list: 63 45 32 98 46 57 28 100 Using a sequential search, how many comparisons are required to determine whether the following items are in the list or not? (Recall that comparisons...
C programming Strictly -
Write a program to sort an array of
integers via arrays of pointers to those integers as shown in the
figure.
Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...