Please help me, I need to have this done in c programming ( NOT C++) , using vis
ID: 3856750 • Letter: P
Question
Please help me, I need to have this done in c programming ( NOT C++), using visual studios 2015
Write a function called smallInt that takes in an array of integers and returns the smallest element in the array. Test your result with the array x = {23, 30, 19, 40, 3, 62, 4, 16, 2}.
- 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
- Given the following pseudocode for selection sort. Write a corresponding function in C called selectionSort that fully implements the design efficiently.
PSEUDOCODE FOR SELECTION-SORT ALGORITHM
1. for j 1 to n-1
2. smallest j
3. for i j + 1 to n
4. if A[ i ] < A[ smallest ]
5. smallest i
6. Exchange A[ j ] A[ smallest ]
Now sort the following arrays by calling the function selectionSort in the main function and passing the array.
Array A
12 10 11 4 12 5
Array B
7 2 23 12 12 21
Explanation / Answer
#include <stdio.h>
#include <math.h>
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
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;
}
arr[j+1] = key;
}
}
// A utility function ot print an array of size n
void printArray(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf(" ");
}
/* Driver program to test insertion sort */
int main()
{
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr)/sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
#include <stdio.h>
#include <math.h>
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
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;
}
arr[j+1] = key;
}
}
// A utility function ot print an array of size n
void printArray(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf(" ");
}
/* Driver program to test insertion sort */
int main()
{
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr)/sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.