Write a program that creates three identical arrays, list1, list2, and list 3, o
ID: 3627780 • Letter: W
Question
Write a program that creates three identical arrays, list1, list2, and list 3, of 5000 elements. The program then sorts list1 using bubble sort, list2 using selection sort, and list3 using merge sort and outputs the number of comparisons and item assignments made by quick sort and insertion sort and the number of comparisons made by merge sort.
here is part of the code i have.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "searchSortAlgorithms.h"
using namespace std;
void fillArray(int list[], int length);
void copyArray(int list1[], int list2[], int length);
int main()
{
int list1[5000];
int list2[5000];
int list3[5000];
int compBubbleSort = 0, compSelectionSort = 0, compInsertionSort = 0;
int assignBubbleSort = 0, assignSelectionSort = 0, assignInsertionSort = 0;
fillArray(list1, 5000);
copyArray(list1, list2, 5000);
copyArray(list1, list3, 5000);
bubbleSort2(list1, 5000, compBubbleSort, assignBubbleSort);
selectionSort2(list2, 5000, compSelectionSort, assignSelectionSort);
insertionSort2(list3, 5000, compInsertionSort, assignInsertionSort);
cout << "Number of comparisons---" << endl;
cout << " Bubble sort: " << compBubbleSort << endl;
cout << " Selection sort: " << compSelectionSort << endl;
cout << " Insertion sort: " << compInsertionSort << endl << endl;
cout << "Number of item assignments---" << endl;
cout << " Bubble sort: " << assignBubbleSort << endl;
cout << " Selection sort: " << assignSelectionSort << endl;
cout << " Insertion sort: " << assignInsertionSort << endl << endl;
return 0;
}
void fillArray(int list[], int length)
{
srand(time(0));
for (int i = 0; i < length; i++)
list[i] = rand() % 20000;
}
void copyArray(int list1[], int list2[], int length)
{
for (int i = 0; i < length; i++)
list2[i] = list1[i];
}
Explanation / Answer
please rate - thanks
#include <iostream>
#include <cstdlib>
#include <ctime>
//#include "searchSortAlgorithms.h"
using namespace std;
void selectionSort2(int[],int,int&,int&);
void bubbleSort2(int [],int,int&,int&);
void insertionSort2(int [],int,int&,int&);
void fillArray(int list[], int length);
void copyArray(int list1[], int list2[], int length);
int main()
{
int list1[5000];
int list2[5000];
int list3[5000];
int compBubbleSort = 0, compSelectionSort = 0, compInsertionSort = 0;
int assignBubbleSort = 0, assignSelectionSort = 0, assignInsertionSort = 0;
fillArray(list1, 5000);
copyArray(list1, list2, 5000);
copyArray(list1, list3, 5000);
bubbleSort2(list1, 5000, compBubbleSort, assignBubbleSort);
selectionSort2(list2, 5000, compSelectionSort, assignSelectionSort);
insertionSort2(list3, 5000, compInsertionSort, assignInsertionSort);
cout << "Number of comparisons---" << endl;
cout << " Bubble sort: " << compBubbleSort << endl;
cout << " Selection sort: " << compSelectionSort << endl;
cout << " Insertion sort: " << compInsertionSort << endl << endl;
cout << "Number of item assignments---" << endl;
cout << " Bubble sort: " << assignBubbleSort << endl;
cout << " Selection sort: " << assignSelectionSort << endl;
cout << " Insertion sort: " << assignInsertionSort << endl << endl;
system("pause");
return 0;
}
void selectionSort2(int a[],int n,int& compare,int& exchange )
{
int i,j,min,index,temp,k;
for(i=0;i<n-1; i++)
{index=i;
min=a[i];
for(j=i+1;j<n;j++)
{compare++;
if(a[j]<min)
{index=j;
min=a[j];
}
}
if(min<a[i])
{temp=a[i];
a[i]=min;
a[index]=temp;
exchange++;
}
}
}
void bubbleSort2(int num[],int n,int& compare,int& exchange )
{int i,j,temp,flag=1;
for(i=1;i<=n&&flag==1;i++)
{flag = 0;
for(j=0;j<n-1;j++)
{compare++;
if(num[j+1]<num[j])
{temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
flag = 1;
exchange++;
}
}
}
}
void insertionSort2(int num[],int n,int& compare,int& exchange )
{int key,i,j;
for(j=1;j<n;j++)
{
key=num[j];
i=j-1;
while(num[i]>key && i>=0)
{compare++;
num[i+1]=num[i];
exchange++;
i--;
}
{num[i+1]=key;
exchange++;
}
}
}
void fillArray(int list[], int length)
{
srand(time(0));
for (int i = 0; i < length; i++)
list[i] = rand() % 20000;
}
void copyArray(int list1[], int list2[], int length)
{
for (int i = 0; i < length; i++)
list2[i] = list1[i];
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.