Please help me, I need to have this done in c programming ( NOT C++) , using vis
ID: 3856764 • Letter: P
Question
Please help me, I need to have this done in c programming ( NOT C++), using visual studios 2015
- Given the following pseudo code for insertion sort. Write a corresponding function in C called InsertionSort that fully implements the design efficiently.
PSEUDOCODE FOR INSERTION-SORT ALGORITHM
1. for j = 2 to n
2. key A [j]
3. // Insert A[j] into the sorted sequence A[1..j-1]. i.e. find the closest place to j to insert the key in the right order.
4. i j – 1
5. while i > 0 and A[i] > key
6. A[i+1] A[i]
7. i i – 1
8. A[i+1] key
Now sort the following arrays by calling the function insertionSort in the main function and passing the array.
Array A
16 18 13 4 12 6
Array B
7 9 7 10 8 6
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
void insertion_sort(int a[], int n){
int i, j, key;
for (i=1;i<n;i++)
{
key = a[i];
j = i-1;
while (j>=0 && a[j]>key)
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = key;
}
printf("Sorted elements are:- ");
for(i=0;i<n;i++)
printf("%d,", a[i]);
printf(" ");
}
int main(){
int n = 6;
int A[6] = {16, 18, 13, 4, 12, 6} ;
int B[6] = {7, 9, 7, 10, 8, 6};
insertion_sort(A, n);
insertion_sort(B, n);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.