Company: Veritas, MAQ-Software, Juniper, Grofers, Dell, Cisco, Accenture
Question: Perform Insertion sort
CODE:
/* Function to sort an array using insertion sort
void insertionSort(int arr[], int n)
{
GfG obj = new GfG();
for (int i = 1; i < n; i++)
obj.insert(arr, i);
} */
class GfG
{
// Function to sort an array using insertion sort
void insert(int arr[],int i)
{
int key=arr[i];
int j=i-1;
while (j>=0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
EXECUTION TIME:0.4s
Question: Perform Insertion sort
CODE:
/* Function to sort an array using insertion sort
void insertionSort(int arr[], int n)
{
GfG obj = new GfG();
for (int i = 1; i < n; i++)
obj.insert(arr, i);
} */
class GfG
{
// Function to sort an array using insertion sort
void insert(int arr[],int i)
{
int key=arr[i];
int j=i-1;
while (j>=0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
EXECUTION TIME:0.4s
Comments
Post a Comment