SlideShare a Scribd company logo
c++ program: I need to sort arrays using an insertion sort and a merge sort (I already have). I
need to use these sorting methods to sort input arrays in the form of randomly generated numbers
in the size of 20, 100 and 200. So I need 3 randomily generated arrays and sorted and outputed
by both sorting method. I currently have it as given arrays and need help changing them to
randomily generated arrays
Here is my code so far:
#include
using namespace std;
void InsertionSort(int arr[],int size){
int temp,j;
for(int i=0; i=0){
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
//Merges two sub-arrays. First sub-array is arr[l..m],second is arr[m+1..r]
void merge(int arr[], int l, int m, int r){
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
//Create temp arrays
int L[n1], R[n2];
//Copy data to temp arrays L[] and R[]
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge arrays back into arr[l..r]*/
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2){
if (L[i] <= R[j]){
arr[k] = L[i];
i++;
}
else{
arr[k] = R[j];
j++;
}
k++;
}
//Copy the remaining elements of L[], if there are any
while (i < n1){
arr[k] = L[i];
i++;
k++;
}
//Copy the remaining elements of R[], if there are any
while (j < n2){
arr[k] = R[j];
j++;
k++;
}
}
//l is left index and r is right index of the sub-array of array to be sorted
void MergeSort(int arr[], int l, int r){
if (l < r){
// Same as (l+r)/2, but avoids overflow for large l and h
int m = l+(r-l)/2;
// Sort first and second halves
MergeSort(arr, l, m);
MergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
void printArray(int arr[], int size){//Function to print array
int i;
for (i=0; i < size; i++)
cout << arr[i] <<" ";
cout << endl;
}
int main(){
int size = 20;
int array1[] = {17,33,88,54,96,82,12,36,69,74,79,66,32,11,1,98,76,55,3,4};
int array2[] = {20,1,19,2,18,3,17,4,16,5,15,6,14,7,13,8,12,9,11,10};
int array3[] = {220,209,198,187,176,165,154,143,132,121,110,99,88,77,66,55,44,33,22,11};
int array4[] = {15,35,85,100,40,90,70,65,25,5,10,30,55,75,80,95,20,45,50,60};
int array5[] = {100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81};
//Output for array1
cout << "Original array 1:  ";
printArray(array1, size);
InsertionSort(array1, size);
cout << "Array 1 sorted by INSERTION SORT:  ";
printArray(array1, size);
cout << " ";
//Output for array2
cout << "Original array 2:  ";
printArray(array2, size);
MergeSort(array2,0,size);
cout << "Array 2 sorted by MERGE SORT:  ";
printArray(array2, size);
cout << " ";
//Output for array3
cout << "Original array 3:  ";
printArray(array3, size);
InsertionSort(array3, size);
cout << "Array 3 sorted by INSERTION SORT:  ";
printArray(array3, size);
cout << " ";
//Output for array4
cout << "Original array 4:  ";
printArray(array4, size);
MergeSort(array4,0,size);
cout << "Array 4 sorted by MERGE SORT:  ";
printArray(array4, size);
cout << " ";
//Output for array5
cout << "Original array 5:  ";
printArray(array5, size);
InsertionSort(array5, size);
cout << "Array 5 sorted by INSERTION SORT:  ";
printArray(array5, size);
return 0;
}
Solution
#include
using namespace std;
void InsertionSort(int arr[],int size){
int temp,j;
for(int i=0; i=0){
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
//Merges two sub-arrays. First sub-array is arr[l..m],second is arr[m+1..r]
void merge(int arr[], int l, int m, int r){
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
//Create temp arrays
int L[n1], R[n2];
//Copy data to temp arrays L[] and R[]
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge arrays back into arr[l..r]*/
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2){
if (L[i] <= R[j]){
arr[k] = L[i];
i++;
}
else{
arr[k] = R[j];
j++;
}
k++;
}
//Copy the remaining elements of L[], if there are any
while (i < n1){
arr[k] = L[i];
i++;
k++;
}
//Copy the remaining elements of R[], if there are any
while (j < n2){
arr[k] = R[j];
j++;
k++;
}
}
//l is left index and r is right index of the sub-array of array to be sorted
void MergeSort(int arr[], int l, int r){
if (l < r){
// Same as (l+r)/2, but avoids overflow for large l and h
int m = l+(r-l)/2;
// Sort first and second halves
MergeSort(arr, l, m);
MergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
void printArray(int arr[], int size){//Function to print array
int i;
for (i=0; i < size; i++)
cout << arr[i] <<" ";
cout << endl;
}
int main(){
int size = 20;
int array1[] = {17,33,88,54,96,82,12,36,69,74,79,66,32,11,1,98,76,55,3,4};
int array2[] = {20,1,19,2,18,3,17,4,16,5,15,6,14,7,13,8,12,9,11,10};
int array3[] = {220,209,198,187,176,165,154,143,132,121,110,99,88,77,66,55,44,33,22,11};
int array4[] = {15,35,85,100,40,90,70,65,25,5,10,30,55,75,80,95,20,45,50,60};
int array5[] = {100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81};
int i;
int a[20];
for(i=0;i<20;i++)
a[i]=rand();
printArray(a,20);
//Output for array1
cout << "Original array 1:  ";
printArray(array1, size);
InsertionSort(array1, size);
cout << "Array 1 sorted by INSERTION SORT:  ";
printArray(array1, size);
cout << " ";
//Output for array2
cout << "Original array 2:  ";
printArray(array2, size);
MergeSort(array2,0,size);
cout << "Array 2 sorted by MERGE SORT:  ";
printArray(array2, size);
cout << " ";
//Output for array3
cout << "Original array 3:  ";
printArray(array3, size);
InsertionSort(array3, size);
cout << "Array 3 sorted by INSERTION SORT:  ";
printArray(array3, size);
cout << " ";
//Output for array4
cout << "Original array 4:  ";
printArray(array4, size);
MergeSort(array4,0,size);
cout << "Array 4 sorted by MERGE SORT:  ";
printArray(array4, size);
cout << " ";
//Output for array5
cout << "Original array 5:  ";
printArray(array5, size);
InsertionSort(array5, size);
cout << "Array 5 sorted by INSERTION SORT:  ";
printArray(array5, size);
return 0;
}

More Related Content

Similar to c++ program I need to sort arrays using an insertion sort and a mer.pdf

2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
Nooryaseen9
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Sunil Yadav
 
DAA Lab Work.docx
DAA Lab Work.docxDAA Lab Work.docx
DAA Lab Work.docx
Deepusri2000Srivasta
 
Array
ArrayArray
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
sowmya koneru
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
Education Front
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
arri2009av
 
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdfMerge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
mdameer02
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
乐群 陈
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
YOGESH SINGH
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
HimanshuKansal22
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
Dev Chauhan
 
Unit 3 arrays and_string
Unit 3 arrays and_stringUnit 3 arrays and_string
Unit 3 arrays and_string
kirthika jeyenth
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
Tareq Hasan
 
The language is C. Thanks. Write a function that computes the intege.pdf
The language is C. Thanks. Write a function that computes the intege.pdfThe language is C. Thanks. Write a function that computes the intege.pdf
The language is C. Thanks. Write a function that computes the intege.pdf
forwardcom41
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
Ramachendran Logarajah
 
Chapter2
Chapter2Chapter2
Chapter2
Krishna Kumar
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
Rahul04August
 

Similar to c++ program I need to sort arrays using an insertion sort and a mer.pdf (20)

2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
 
DAA Lab Work.docx
DAA Lab Work.docxDAA Lab Work.docx
DAA Lab Work.docx
 
Array
ArrayArray
Array
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
 
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdfMerge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
 
Unit 3 arrays and_string
Unit 3 arrays and_stringUnit 3 arrays and_string
Unit 3 arrays and_string
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
The language is C. Thanks. Write a function that computes the intege.pdf
The language is C. Thanks. Write a function that computes the intege.pdfThe language is C. Thanks. Write a function that computes the intege.pdf
The language is C. Thanks. Write a function that computes the intege.pdf
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Chapter2
Chapter2Chapter2
Chapter2
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 

More from dhavalbl38

Imagine a spherical balloon filled wild helium- the balloon itself is.pdf
Imagine a spherical balloon filled wild helium- the balloon itself is.pdfImagine a spherical balloon filled wild helium- the balloon itself is.pdf
Imagine a spherical balloon filled wild helium- the balloon itself is.pdf
dhavalbl38
 
If a substance has a pH of 4, is it acidic or basic. What type of io.pdf
If a substance has a pH of 4, is it acidic or basic. What type of io.pdfIf a substance has a pH of 4, is it acidic or basic. What type of io.pdf
If a substance has a pH of 4, is it acidic or basic. What type of io.pdf
dhavalbl38
 
Help with answering these questions for my Human Health Class Biolog.pdf
Help with answering these questions for my Human Health Class Biolog.pdfHelp with answering these questions for my Human Health Class Biolog.pdf
Help with answering these questions for my Human Health Class Biolog.pdf
dhavalbl38
 
For each task, submit your source java code file.(1) Objective Im.pdf
For each task, submit your source java code file.(1) Objective Im.pdfFor each task, submit your source java code file.(1) Objective Im.pdf
For each task, submit your source java code file.(1) Objective Im.pdf
dhavalbl38
 
Emperor penguins live on a diet of fish and crustaceans obtained fro.pdf
Emperor penguins live on a diet of fish and crustaceans obtained fro.pdfEmperor penguins live on a diet of fish and crustaceans obtained fro.pdf
Emperor penguins live on a diet of fish and crustaceans obtained fro.pdf
dhavalbl38
 
Even before the Ownership Society programs of Presidents Clinton.pdf
Even before the Ownership Society programs of Presidents Clinton.pdfEven before the Ownership Society programs of Presidents Clinton.pdf
Even before the Ownership Society programs of Presidents Clinton.pdf
dhavalbl38
 
Do the following picSolutionHere is the solution for the .pdf
Do the following picSolutionHere is the solution for the .pdfDo the following picSolutionHere is the solution for the .pdf
Do the following picSolutionHere is the solution for the .pdf
dhavalbl38
 
Can one mRNA be translated into more than one protein sequence Can .pdf
Can one mRNA be translated into more than one protein sequence Can .pdfCan one mRNA be translated into more than one protein sequence Can .pdf
Can one mRNA be translated into more than one protein sequence Can .pdf
dhavalbl38
 
why is there an uneven distribution of photosystem 1 and 2 on the th.pdf
why is there an uneven distribution of photosystem 1 and 2 on the th.pdfwhy is there an uneven distribution of photosystem 1 and 2 on the th.pdf
why is there an uneven distribution of photosystem 1 and 2 on the th.pdf
dhavalbl38
 
Which of the following are examples of cell signaling Choose all tha.pdf
Which of the following are examples of cell signaling Choose all tha.pdfWhich of the following are examples of cell signaling Choose all tha.pdf
Which of the following are examples of cell signaling Choose all tha.pdf
dhavalbl38
 
What is the purpose of testing a program with different dataSol.pdf
What is the purpose of testing a program with different dataSol.pdfWhat is the purpose of testing a program with different dataSol.pdf
What is the purpose of testing a program with different dataSol.pdf
dhavalbl38
 
what are the factors contributing to the rise in global media Will .pdf
what are the factors contributing to the rise in global media Will .pdfwhat are the factors contributing to the rise in global media Will .pdf
what are the factors contributing to the rise in global media Will .pdf
dhavalbl38
 
WACC of Alibaba company WACC of Alibaba companySolutionTh.pdf
WACC of Alibaba company WACC of Alibaba companySolutionTh.pdfWACC of Alibaba company WACC of Alibaba companySolutionTh.pdf
WACC of Alibaba company WACC of Alibaba companySolutionTh.pdf
dhavalbl38
 
Two particles A and B of equal mass have velocities as shown in the f.pdf
Two particles A and B of equal mass have velocities as shown in the f.pdfTwo particles A and B of equal mass have velocities as shown in the f.pdf
Two particles A and B of equal mass have velocities as shown in the f.pdf
dhavalbl38
 
The forecast equation for mean wind in a turbulent flow is U_it + .pdf
The forecast equation for mean wind in a turbulent flow is  U_it + .pdfThe forecast equation for mean wind in a turbulent flow is  U_it + .pdf
The forecast equation for mean wind in a turbulent flow is U_it + .pdf
dhavalbl38
 
The image below is of a charged moving in a uniform magnetic field. I.pdf
The image below is of a charged moving in a uniform magnetic field. I.pdfThe image below is of a charged moving in a uniform magnetic field. I.pdf
The image below is of a charged moving in a uniform magnetic field. I.pdf
dhavalbl38
 
The concept of a sampling distributionA-Is essential for drawing c.pdf
The concept of a sampling distributionA-Is essential for drawing c.pdfThe concept of a sampling distributionA-Is essential for drawing c.pdf
The concept of a sampling distributionA-Is essential for drawing c.pdf
dhavalbl38
 
The diets common to some cultures emphasize meat. Meat, milk, and br.pdf
The diets common to some cultures emphasize meat. Meat, milk, and br.pdfThe diets common to some cultures emphasize meat. Meat, milk, and br.pdf
The diets common to some cultures emphasize meat. Meat, milk, and br.pdf
dhavalbl38
 
aed by genes at two loci R. rand P. p). A walnut comb is at one Tocus.pdf
aed by genes at two loci R. rand P. p). A walnut comb is at one Tocus.pdfaed by genes at two loci R. rand P. p). A walnut comb is at one Tocus.pdf
aed by genes at two loci R. rand P. p). A walnut comb is at one Tocus.pdf
dhavalbl38
 
t t Deg g olthe year.) 4.9 An engineering construction firm is .pdf
t t Deg g olthe year.) 4.9 An engineering construction firm is .pdft t Deg g olthe year.) 4.9 An engineering construction firm is .pdf
t t Deg g olthe year.) 4.9 An engineering construction firm is .pdf
dhavalbl38
 

More from dhavalbl38 (20)

Imagine a spherical balloon filled wild helium- the balloon itself is.pdf
Imagine a spherical balloon filled wild helium- the balloon itself is.pdfImagine a spherical balloon filled wild helium- the balloon itself is.pdf
Imagine a spherical balloon filled wild helium- the balloon itself is.pdf
 
If a substance has a pH of 4, is it acidic or basic. What type of io.pdf
If a substance has a pH of 4, is it acidic or basic. What type of io.pdfIf a substance has a pH of 4, is it acidic or basic. What type of io.pdf
If a substance has a pH of 4, is it acidic or basic. What type of io.pdf
 
Help with answering these questions for my Human Health Class Biolog.pdf
Help with answering these questions for my Human Health Class Biolog.pdfHelp with answering these questions for my Human Health Class Biolog.pdf
Help with answering these questions for my Human Health Class Biolog.pdf
 
For each task, submit your source java code file.(1) Objective Im.pdf
For each task, submit your source java code file.(1) Objective Im.pdfFor each task, submit your source java code file.(1) Objective Im.pdf
For each task, submit your source java code file.(1) Objective Im.pdf
 
Emperor penguins live on a diet of fish and crustaceans obtained fro.pdf
Emperor penguins live on a diet of fish and crustaceans obtained fro.pdfEmperor penguins live on a diet of fish and crustaceans obtained fro.pdf
Emperor penguins live on a diet of fish and crustaceans obtained fro.pdf
 
Even before the Ownership Society programs of Presidents Clinton.pdf
Even before the Ownership Society programs of Presidents Clinton.pdfEven before the Ownership Society programs of Presidents Clinton.pdf
Even before the Ownership Society programs of Presidents Clinton.pdf
 
Do the following picSolutionHere is the solution for the .pdf
Do the following picSolutionHere is the solution for the .pdfDo the following picSolutionHere is the solution for the .pdf
Do the following picSolutionHere is the solution for the .pdf
 
Can one mRNA be translated into more than one protein sequence Can .pdf
Can one mRNA be translated into more than one protein sequence Can .pdfCan one mRNA be translated into more than one protein sequence Can .pdf
Can one mRNA be translated into more than one protein sequence Can .pdf
 
why is there an uneven distribution of photosystem 1 and 2 on the th.pdf
why is there an uneven distribution of photosystem 1 and 2 on the th.pdfwhy is there an uneven distribution of photosystem 1 and 2 on the th.pdf
why is there an uneven distribution of photosystem 1 and 2 on the th.pdf
 
Which of the following are examples of cell signaling Choose all tha.pdf
Which of the following are examples of cell signaling Choose all tha.pdfWhich of the following are examples of cell signaling Choose all tha.pdf
Which of the following are examples of cell signaling Choose all tha.pdf
 
What is the purpose of testing a program with different dataSol.pdf
What is the purpose of testing a program with different dataSol.pdfWhat is the purpose of testing a program with different dataSol.pdf
What is the purpose of testing a program with different dataSol.pdf
 
what are the factors contributing to the rise in global media Will .pdf
what are the factors contributing to the rise in global media Will .pdfwhat are the factors contributing to the rise in global media Will .pdf
what are the factors contributing to the rise in global media Will .pdf
 
WACC of Alibaba company WACC of Alibaba companySolutionTh.pdf
WACC of Alibaba company WACC of Alibaba companySolutionTh.pdfWACC of Alibaba company WACC of Alibaba companySolutionTh.pdf
WACC of Alibaba company WACC of Alibaba companySolutionTh.pdf
 
Two particles A and B of equal mass have velocities as shown in the f.pdf
Two particles A and B of equal mass have velocities as shown in the f.pdfTwo particles A and B of equal mass have velocities as shown in the f.pdf
Two particles A and B of equal mass have velocities as shown in the f.pdf
 
The forecast equation for mean wind in a turbulent flow is U_it + .pdf
The forecast equation for mean wind in a turbulent flow is  U_it + .pdfThe forecast equation for mean wind in a turbulent flow is  U_it + .pdf
The forecast equation for mean wind in a turbulent flow is U_it + .pdf
 
The image below is of a charged moving in a uniform magnetic field. I.pdf
The image below is of a charged moving in a uniform magnetic field. I.pdfThe image below is of a charged moving in a uniform magnetic field. I.pdf
The image below is of a charged moving in a uniform magnetic field. I.pdf
 
The concept of a sampling distributionA-Is essential for drawing c.pdf
The concept of a sampling distributionA-Is essential for drawing c.pdfThe concept of a sampling distributionA-Is essential for drawing c.pdf
The concept of a sampling distributionA-Is essential for drawing c.pdf
 
The diets common to some cultures emphasize meat. Meat, milk, and br.pdf
The diets common to some cultures emphasize meat. Meat, milk, and br.pdfThe diets common to some cultures emphasize meat. Meat, milk, and br.pdf
The diets common to some cultures emphasize meat. Meat, milk, and br.pdf
 
aed by genes at two loci R. rand P. p). A walnut comb is at one Tocus.pdf
aed by genes at two loci R. rand P. p). A walnut comb is at one Tocus.pdfaed by genes at two loci R. rand P. p). A walnut comb is at one Tocus.pdf
aed by genes at two loci R. rand P. p). A walnut comb is at one Tocus.pdf
 
t t Deg g olthe year.) 4.9 An engineering construction firm is .pdf
t t Deg g olthe year.) 4.9 An engineering construction firm is .pdft t Deg g olthe year.) 4.9 An engineering construction firm is .pdf
t t Deg g olthe year.) 4.9 An engineering construction firm is .pdf
 

Recently uploaded

Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 

Recently uploaded (20)

Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 

c++ program I need to sort arrays using an insertion sort and a mer.pdf

  • 1. c++ program: I need to sort arrays using an insertion sort and a merge sort (I already have). I need to use these sorting methods to sort input arrays in the form of randomly generated numbers in the size of 20, 100 and 200. So I need 3 randomily generated arrays and sorted and outputed by both sorting method. I currently have it as given arrays and need help changing them to randomily generated arrays Here is my code so far: #include using namespace std; void InsertionSort(int arr[],int size){ int temp,j; for(int i=0; i=0){ arr[j+1] = arr[j]; j--; } arr[j+1] = temp; } } //Merges two sub-arrays. First sub-array is arr[l..m],second is arr[m+1..r] void merge(int arr[], int l, int m, int r){ int i, j, k; int n1 = m - l + 1; int n2 = r - m; //Create temp arrays int L[n1], R[n2]; //Copy data to temp arrays L[] and R[] for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j]; /* Merge arrays back into arr[l..r]*/ i = 0; j = 0; k = l;
  • 2. while (i < n1 && j < n2){ if (L[i] <= R[j]){ arr[k] = L[i]; i++; } else{ arr[k] = R[j]; j++; } k++; } //Copy the remaining elements of L[], if there are any while (i < n1){ arr[k] = L[i]; i++; k++; } //Copy the remaining elements of R[], if there are any while (j < n2){ arr[k] = R[j]; j++; k++; } } //l is left index and r is right index of the sub-array of array to be sorted void MergeSort(int arr[], int l, int r){ if (l < r){ // Same as (l+r)/2, but avoids overflow for large l and h int m = l+(r-l)/2; // Sort first and second halves MergeSort(arr, l, m); MergeSort(arr, m+1, r); merge(arr, l, m, r); } }
  • 3. void printArray(int arr[], int size){//Function to print array int i; for (i=0; i < size; i++) cout << arr[i] <<" "; cout << endl; } int main(){ int size = 20; int array1[] = {17,33,88,54,96,82,12,36,69,74,79,66,32,11,1,98,76,55,3,4}; int array2[] = {20,1,19,2,18,3,17,4,16,5,15,6,14,7,13,8,12,9,11,10}; int array3[] = {220,209,198,187,176,165,154,143,132,121,110,99,88,77,66,55,44,33,22,11}; int array4[] = {15,35,85,100,40,90,70,65,25,5,10,30,55,75,80,95,20,45,50,60}; int array5[] = {100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81}; //Output for array1 cout << "Original array 1: "; printArray(array1, size); InsertionSort(array1, size); cout << "Array 1 sorted by INSERTION SORT: "; printArray(array1, size); cout << " "; //Output for array2 cout << "Original array 2: "; printArray(array2, size); MergeSort(array2,0,size); cout << "Array 2 sorted by MERGE SORT: "; printArray(array2, size); cout << " "; //Output for array3 cout << "Original array 3: "; printArray(array3, size); InsertionSort(array3, size); cout << "Array 3 sorted by INSERTION SORT: "; printArray(array3, size);
  • 4. cout << " "; //Output for array4 cout << "Original array 4: "; printArray(array4, size); MergeSort(array4,0,size); cout << "Array 4 sorted by MERGE SORT: "; printArray(array4, size); cout << " "; //Output for array5 cout << "Original array 5: "; printArray(array5, size); InsertionSort(array5, size); cout << "Array 5 sorted by INSERTION SORT: "; printArray(array5, size); return 0; } Solution #include using namespace std; void InsertionSort(int arr[],int size){ int temp,j; for(int i=0; i=0){ arr[j+1] = arr[j]; j--; } arr[j+1] = temp; } } //Merges two sub-arrays. First sub-array is arr[l..m],second is arr[m+1..r] void merge(int arr[], int l, int m, int r){ int i, j, k;
  • 5. int n1 = m - l + 1; int n2 = r - m; //Create temp arrays int L[n1], R[n2]; //Copy data to temp arrays L[] and R[] for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j]; /* Merge arrays back into arr[l..r]*/ i = 0; j = 0; k = l; while (i < n1 && j < n2){ if (L[i] <= R[j]){ arr[k] = L[i]; i++; } else{ arr[k] = R[j]; j++; } k++; } //Copy the remaining elements of L[], if there are any while (i < n1){ arr[k] = L[i]; i++; k++; } //Copy the remaining elements of R[], if there are any while (j < n2){ arr[k] = R[j]; j++;
  • 6. k++; } } //l is left index and r is right index of the sub-array of array to be sorted void MergeSort(int arr[], int l, int r){ if (l < r){ // Same as (l+r)/2, but avoids overflow for large l and h int m = l+(r-l)/2; // Sort first and second halves MergeSort(arr, l, m); MergeSort(arr, m+1, r); merge(arr, l, m, r); } } void printArray(int arr[], int size){//Function to print array int i; for (i=0; i < size; i++) cout << arr[i] <<" "; cout << endl; } int main(){ int size = 20; int array1[] = {17,33,88,54,96,82,12,36,69,74,79,66,32,11,1,98,76,55,3,4}; int array2[] = {20,1,19,2,18,3,17,4,16,5,15,6,14,7,13,8,12,9,11,10}; int array3[] = {220,209,198,187,176,165,154,143,132,121,110,99,88,77,66,55,44,33,22,11}; int array4[] = {15,35,85,100,40,90,70,65,25,5,10,30,55,75,80,95,20,45,50,60}; int array5[] = {100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81}; int i; int a[20]; for(i=0;i<20;i++) a[i]=rand(); printArray(a,20);
  • 7. //Output for array1 cout << "Original array 1: "; printArray(array1, size); InsertionSort(array1, size); cout << "Array 1 sorted by INSERTION SORT: "; printArray(array1, size); cout << " "; //Output for array2 cout << "Original array 2: "; printArray(array2, size); MergeSort(array2,0,size); cout << "Array 2 sorted by MERGE SORT: "; printArray(array2, size); cout << " "; //Output for array3 cout << "Original array 3: "; printArray(array3, size); InsertionSort(array3, size); cout << "Array 3 sorted by INSERTION SORT: "; printArray(array3, size); cout << " "; //Output for array4 cout << "Original array 4: "; printArray(array4, size); MergeSort(array4,0,size); cout << "Array 4 sorted by MERGE SORT: "; printArray(array4, size); cout << " "; //Output for array5 cout << "Original array 5: "; printArray(array5, size); InsertionSort(array5, size);
  • 8. cout << "Array 5 sorted by INSERTION SORT: "; printArray(array5, size); return 0; }