1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
icang [17]
3 years ago
5

Create an integer variable named ‘listSize’ and initialize the value to 1000.

Computers and Technology
1 answer:
Natalija [7]3 years ago
3 0

Answer:

The code is given in C++. The variable name is arraySize. It should be replaced with listSize

Explanation:

// header file

#include<iostream>

#include<fstream>

#include<cstdlib>

#include<chrono>

using namespace std;

using namespace std::chrono;

// bubble sort algorithm

void bubbleSort(int *a, int size) {

for (int i = 0; i<size - 1; i++) {

for (int j = 0; j<size - i - 1; j++) {

if (a[j]>a[j + 1]) {

//swap

int temp = a[j];

a[j] = a[j + 1];

a[j + 1] = temp;

}

}

}

}

// selection sort

void selectionSort(int *a, int size) {

for (int i = 0; i<size - 1; i++) {

int m_i = i;

for (int j = i + 1; j<size; j++) {

if (a[j]<a[m_i]) {

m_i = j;

}

}

//swap

int temp = a[i];

a[i] = a[m_i];

a[m_i] = a[i];

}

}

// insertion sort

void insertionSort(int *a, int size) {

for (int i = 1; i<size; ++i) {

int key = a[i];

int j=i-1;

for (; (j >= 0 && key < a[j]); j--) {

a[j + 1] = a[j];

}

a[j + 1] = key;

}

}

// make partition on array

int getPartition(int *a, int l, int h) {

int pvt = a[h];

int i = (l - 1);

for (int j = l; j <= h - 1; j++) {

if (a[j]<pvt) {

i++;

//swap

int tmp = a[i];

a[i] = a[j];

a[j] = tmp;

}

}

//swap

int tmp = a[i + 1];

a[i + 1] = a[h];

a[h] = tmp;

return(i + 1);

}

// quick sort

void quickSort(int *a, int l, int h) {

if (l<h) {

int p = getPartition(a, l, h);

quickSort(a, l, p - 1);

quickSort(a, p + 1, h);

}

}

// merge array

void merge(int *a, int l, int m, int r) {

int n1 = m - l + 1;

int n2 = r - m;

int *L = new int[n1];

int *R = new int[n2];

for (int i = 0; i<n1; i++) {

L[i] = a[i + l];

}

for (int j = 0; j<n2; j++) {

R[j] = a[m + j + 1];

}

int i = 0, j = 0, k = 1;

while (i<n1 && j<n2) {

if (L[i] <= R[j]) {

a[k] = L[i];

i++;

}

else {

a[k] = R[j];

j++;

}

k++;

}

while (i<n1) {

a[k] = L[i];

i++;

k++;

}

while (j<n2) {

a[k] = R[j];

j++;

k++;

}

}

// merge sort

void mergeSort(int *a, int l, int r) {

if (l<r) {

int m = l + (r - l) / 2;

mergeSort(a, l, m);

mergeSort(a, m + 1, r);

merge(a, l, m, r);

}

}

int* randomData(int size) {

int *ar = new int[size];

for (int i = 0; i<size; i++) {

ar[i] = rand();

}

return(ar);

}

void copyAry(int *a, int *b, int size) {

for (int i = 0; i < size; i++) {

a[i] = b[i];

}

}

int main() {

int arraySize = 1000;

int *arrLst = new int[arraySize];

int *arLst = new int[arraySize];

arrLst = randomData(arraySize);

//copy array

// because after sorting arry will be sorted

copyAry(arLst, arrLst, arraySize);

auto start1 = high_resolution_clock::now();

bubbleSort(arLst, arraySize);

auto end1 = high_resolution_clock::now();

auto duration1 = duration_cast<microseconds>(end1 - start1);

cout << duration1.count()<<endl;

// copy array for new sort

copyAry(arLst, arrLst, arraySize);

auto start2 = high_resolution_clock::now();

selectionSort(arLst, arraySize);

auto end2 = high_resolution_clock::now();

auto duration2 = duration_cast<microseconds>(end2 - start2);

cout << duration2.count() << endl;

// copy array for new sort

copyAry(arLst, arrLst, arraySize);

auto start3 = high_resolution_clock::now();

insertionSort(arLst, arraySize);

auto end3 = high_resolution_clock::now();

auto duration3 = duration_cast<microseconds>(end3 - start3);

cout << duration3.count() << endl;

// copy array for new sort

copyAry(arLst, arrLst, arraySize);

auto start4 = high_resolution_clock::now();

mergeSort(arLst, 0,arraySize-1);

auto end4 = high_resolution_clock::now();

auto duration4 = duration_cast<microseconds>(end4 - start4);

cout << duration4.count() << endl;

// copy array for new sort

copyAry(arLst, arrLst, arraySize);

auto start5 = high_resolution_clock::now();

quickSort(arLst, 0,arraySize-1);

auto end5 = high_resolution_clock::now();

auto duration5 = duration_cast<microseconds>(end5 - start5);

cout << duration5.count() << endl;

ofstream out("GroupAssignment2Results.csv");

out << "Array Size,Bubble Sort Time,Selection Sort Time,Insertion Sort Time,Quick Sort Time,Merge Sort Time\n";

out << arraySize << "," << duration1.count() << "," << duration2.count() << "," << duration3.count() << "," << duration5.count() << "," << duration4.count();

cout << "\nOutput in File.";

out.close();

cin.get();

return(0);

}

You might be interested in
What benefit does internet have​
Licemer1 [7]
Information can be transferred and passed way easier.
5 0
3 years ago
Whose task it is to ensure that the product flows logically from one step to another?
vlabodo [156]

Answer:

The broad responsibility of a UX designer is to ensure that the product logically flows from one step to the next. One way that a UX designer might do this is by conducting in-person user tests to observe one’s behavior.

Explanation: I don't know if this is what your looking for though.

3 0
3 years ago
A(n) _________________________ is a character that word displays on the screen but is not visible on a printed document.
oksano4ka [1.4K]
Nonprint ...but im like 99 percent sure
6 0
3 years ago
What is HDLC flow control?​
Likurg_2 [28]
HDLC is a synchronous Data Link layer bit-oriented protocol developed by the International Organization for Standardization (ISO).
6 0
2 years ago
What type of device can be used to block unwanted traffic initiated from the internet and can also restrict internet access from
statuscvo [17]
It is the firewall exactly.
4 0
2 years ago
Other questions:
  • What was the purpose of the Declaration of Independence and what led to it​
    10·1 answer
  • The material chosen for your study group
    11·1 answer
  • Quiz
    6·2 answers
  • /*Implement a class Address . An address has a house number, a street, an optional
    5·1 answer
  • What is the Gain (dB) of a transmission if the Maximum Data Rate is 1 Gbps and the Bandwidth =7000 MHz? Group of answer choices
    6·1 answer
  • Write a program that takes the account's present value, monthly interest rate, and the number of months that the money will be l
    5·1 answer
  • Write a Python program segment, using a loop, to calculate and print the sum of the odd integers from 20 to 120. (Hint: 21 23 25
    13·1 answer
  • Convert the following denary numbers into binary using 8-bit register:
    6·1 answer
  • CreatePolicies<br> I need help with this in java.
    8·1 answer
  • Differenciate between foreign key and primary key in database.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!