Here is the skeleton of a code for insertion sorting in an imperative language. You have to add right lines of codes for the language you choose (C, C++, C#, JAVA etc,). The following sample is for C++.

New Perspectives on HTML5, CSS3, and JavaScript
6th Edition
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Patrick M. Carey
Chapter14: Exploring Object-based Programming: Designing An Online Poker
Section14.1: Visual Overview: Custom Objects, Properties, And Methods
Problem 7QC
icon
Related questions
Question

Please Help In JAVA

 

Here is the skeleton of a code for insertion sorting in an imperative language. You have to add right lines of codes for the language you choose (C, C++, C#, JAVA etc,). The following sample is for C++.

 

#include <iostream>
#include <array>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
using namespace std;

void insertionSort(int A[], int n)
{
for (int i = n-2; i >= 0; i--)
{
 int j;
 int v = A[i];
 for (j = i + 1; j <= n-1; j++)
 {
  if (A[j] > v)
   break;
  else
  A[j-1] = A[j]; 
 }
 A[j-1] = v;
}
}

int *randomArray(int n)
{
srand((unsigned) time(0));
int * A = new int [n];
for (int i = 0; i < n; i++)
{
 A[i] = rand();
}

return A;
}

Sample function calls (call these functions in main driver function):
int n = 100000;
int * A = randomArray(n);
insertionSort(A, n);
delete [] A;

 

Note: delete memory of first array before sorting second array.

After you modify the code: (1) Generate 100, 000 random numbers and sort them (2) Generate 300000 random numbers and sort them.   Determine the time takes for each case.

Expert Solution
steps

Step by step

Solved in 3 steps with 5 images

Blurred answer
Knowledge Booster
Use of XOR function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning