Code in C - CountingSort exact as the algorithm which takes in an array of 1000 random integers. Provide comments in code for understanding purpose
![COUNTING-SORT(A, B, k) 1 let C[0..k] be a new array 2 for i = 0 to k C[i] = 0 4 for j = 1 to A.length 5 C[Ajl] = C[A;l] + 1 6](http://img.homeworklib.com/questions/e0794180-2151-11ec-a829-c53aad10f565.png?x-oss-process=image/resize,w_560)
//C program
#include<stdio.h>
#include<time.h>
#define n 1000
//counting sort function
void countSort(int A[] , int B[] , int k){
int i;
int C[k+1];
for(i=0;i<=k;i++) C[i] = 0;
for(i=0 ;i<n;i++)C[A[i]] = C[A[i]] + 1;
for(i=1;i<=k;i++)C[i] = C[i] + C[i-1];
for(i = n-1;i>=0;i--){
B[C[A[i]]-1] = A[i];
C[A[i]] = C[A[i]] -1;
}
}
//max() function return maximum element in array A[]
int max(int A[]){
int m = A[0];
int i;
for(i=0;i<n;i++){
if(A[i]>m) m =A[i];
}
return m;
}
//Driver main function
int main(){
int A[n],B[n];
int i=0;
srand(time(NULL));
//Array initialization using random numbers
for(i=0;i<n;i++){
A[i] = rand()%1000;
}
printf("Unsorted Array : \n");
//printing unsorted array
for(i=0;i<n;i++)printf("%d ",A[i]);
int k = max(A);
//calling countSort() function to sort array A[] and
put sorted element int array B[]
countSort(A,B,k);
printf("\nsorted Array : \n");
//printing sorted array B[]
for(i=0;i<n;i++)printf("%d ",B[i]);
return 0;
}
Code in C - CountingSort exact as the algorithm which takes in an array of 1000...
When asked to describe an algorithm you are expected to give a
clear pseudo-code description of the algorithm
1. (10 pts) Here is a new sorting algorithm NewSort Suppose the original call made is NewSort(A,0,n-1) where A is an array integers. == void NewSort(int A[], int i, int j){ \\ sorts the subarray Aſi..j] if (j i+1) \\when there are only 2 elements if (A[i] > A[j]) swap(A,i,j) \\swaps A[i] and A[j] else { int k = (j-i+1)/3; NewSort(A,i,j-k); \\...
MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First ask the user how many elements of his/her array. 2) Then, read the integer array elements as input from the User. 3) Then, print out the array before the sorting 4) Apply Bubble sort algorithm on your array 5) Print out the array after the sorting 6) Print some welcome text to th user 7) Add comments to your code to describe how is...
Which sorting algorithm works by iterating through an input array a, and swapping a[i] with a[x], where i starts at 0 and ends at a.length-1, and x is an index of a minimum value found in a[i], a[i+1], ..., a[a.length-1]? Selection Sort Bubble Sort Insertion Sort Bogo Sort Ascending Sort
[Code in C] Help me with this. Not sure what to do.
1 Couting Sort You may have learned some sorting algorithms - such as bubble sort ad quicksort in CS 110 and CS 210. This homework is about counting sort. Let n be the number of elements to be sorted. Bubble sort and quicksort assume tha time, which one is larger and which one is smaller. They make no assumption on the values of the elements t we can...
How to write a recursive method named lessThanKFirst that receives an array of integers a and an integer k and rearranges the integers in a in such a way that all integers that are smaller than k come before any integers that are greater than or equal to k. As an example, suppose that a and k are: int[] a = {35, 12, 57, 28, 49, 100, 61, 73, 92, 27, 39, 83, 52}; int k = 73; Then, the following...
Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...
the question from the course COMP 4040 that Analysis of
Algorithms
if you want to answer it by code please use C or C++
5. Algorithm Design (20 points) Input: array A contains n distinct numbers from 1 to n, in arbitrary order. Output: number of inversions (defined as the number of pair(i, j) of array indices with i < j and A[i] > Aj]) (a) (5 points) What array with elements from the set {1, 2, ..., n) has...
Run-able, Corrected source code The following code will sort an array in descending order. Keep it as Descending. However it has two logic bugs. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /* The sorting algorithm is to be left as descending There are two logical bugs, the number do not sort correctly Fix it Add analysis output as per requirements */ #include <stdio.h> main() { char wait; short m[]={3,5,7,2,5,1,2,2, 6,5,7,2,4,1,3,3, 7,7,3,2,5,7,1,9}; unsigned char temp, i, j; unsigned char numElements =...
Bubble sort is a popular, but inefficient, sorting algorithm. It works by repeatedly swapping adjacent elements that out of order. BUBBLESORT(A) 1. for i = 1 to A.length – 1 2. for j = i + 1 to A.length 3. if A[j] < A[i] 4. exchange A[j] with A[i] a) A loop invariant for the outer for loop in lines 1 – 4 is: At iteration i, the sub-array A[1..i] is sorted and any element in A[i+1..A.size] is greater or...
(15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an earray A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A i=j-1 while i > 0 and A[i] > key do Ali + 1] = Ai i=i-1 7: A[i+1] = key (a) Follow this algorithm for A[1..4) =< 7,9,6,8 >. Specifically, please indicate the contents of the array after each iteration of the outer...