Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HOW WOULD I MAKE THIS PROGRAM SORT 5000 ELEMENTS INSTEAD OF THE 10 SPECIFIED?? s

ID: 3703171 • Letter: H

Question

HOW WOULD I MAKE THIS PROGRAM SORT 5000 ELEMENTS INSTEAD OF THE 10 SPECIFIED??

searchSortAlgorithms.h

void swap(int *xp, int *yp);

void bubbleSort(int arr[], int n);

void selectionSort(int arr[], int n);

void insertionSort(int arr[], int n);

void printArray(int arr[], int size);

Ch18_Ex15.cpp

#include <iostream>

#include <cstdlib>

#include "searchSortAlgorithms.h"

using namespace std;

void swap(int *xp, int *yp)

{

int temp = *xp;

*xp = *yp;

*yp = temp;

}

// A function to implement bubble sort

void bubbleSort(int arr[], int n)

{

int i, j, comparison = 0;

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

// Last i elements are already in place

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

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

comparison += 1;

swap(&arr[j], &arr[j+1]);

}

}

}

cout<<" Number of Comparison Made using Bubble Sort Algorithm is: "<<comparison<<endl;

}

void selectionSort(int arr[], int n)

{

int i, j, min_idx, comparison = 0;

// One by one move boundary of unsorted subarray

for (i = 0; i < n-1; i++)

{

// Find the minimum element in unsorted array

min_idx = i;

for (j = i+1; j < n; j++){

if (arr[j] < arr[min_idx]){

min_idx = j;

}

comparison += 1;

// Swap the found minimum element with the first element

swap(&arr[min_idx], &arr[i]);

}

}

cout<<" Number of Comparison Made using Selection Sort Algorithm is: "<<comparison<<endl;

}

/* Function to sort an array using insertion sort*/

void insertionSort(int arr[], int n)

{

int i, key, j, comparison = 0;

for (i = 1; i < n; i++)

{

key = arr[i];

j = i-1;

/* Move elements of arr[0..i-1], that are

greater than key, to one position ahead

of their current position */

while (j >= 0 && arr[j] > key)

{

arr[j+1] = arr[j];

j = j-1;

}

comparison += 1;

arr[j+1] = key;

}

cout<<" Number of Comparison Made using Insertion Sort Algorithm is: "<<comparison<<endl;

}

/* Function to print an array */

void printArray(int arr[], int size)

{

int i;

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

cout<<arr[i]<<" ";

cout<<endl;

}

// Driver program to test above functions

int main()

{

int list1[] = {64, 34, 25, 12, 22, 11, 90, 76, 90, 100};

int list2[] = {64, 34, 25, 12, 22, 11, 90, 76, 90, 100};

int list3[] = {64, 34, 25, 12, 22, 11, 90, 76, 90, 100};

int n = sizeof(list1)/sizeof(list1[0]);

bubbleSort(list1, n);

cout<< "Sorted array using Bubble Sort is: "<<endl;

printArray(list1, n);

selectionSort(list2, n);

cout<< "Sorted array using Selection Sort is: "<<endl;

printArray(list2, n);

insertionSort(list3, n);

cout<< "Sorted array using Insertion Sort is: "<<endl;

printArray(list3, n);   

return 0;

}

Explanation / Answer

i have written a driver code..in which 5000 random number are being cretaed using rand function and then these number are stored in an array after that array passed to the functions..Please try this driver code.

Changes are highlighted in bold.

==============================================================

int main()

{

srand(3);

int list1[5000], list2[5000], list3[5000];

int n = 5000;

for(int i = 0; i<n; i++)

{

//generate random number between 1 to 100 and store number in the lists

list1[i] = 1 + rand()%100;

list2[i] = 1 + rand()%100;

list3[i] = 1 + rand()%100;

}

bubbleSort(list1, n);

cout<< "Sorted array using Bubble Sort is: "<<endl;

printArray(list1, n);

selectionSort(list2, n);

cout<< "Sorted array using Selection Sort is: "<<endl;

printArray(list2, n);

insertionSort(list3, n);

cout<< "Sorted array using Insertion Sort is: "<<endl;

printArray(list3, n);   

return 0;

}

==============================================================

Kindly Check and Verify Thanks..!!!