Start
Declare : public class ShiftLinear {
Declare: public static void main(String[] args)
Assign: int[] a = {-1,7,3,-5,4,-3,1,2};
Input: sorting(a);
Output: System.out.println(Arrays.toString(a));
}
Declare: public static void sorting(int[] a){
Assign: int pos = 0;
int neg = 0;
int i,j;
int max = Integer.MIN_VALUE;
for->_i=0 to a.len
{
if(a[i]<0) neg++;
else pos++;
if(a[i]>max) max = a[i];
}
max++;
if(neg==0 || pos == 0) return;
i=0;
j=1;
while(true){
while(i<=neg && a[i]<0) i++;
while(j<a.len && a[j]>=0) j++;
if(i>neg || j>=a.len) break;
a[i]+= max*(i+1);
swapping(a,i,j);
}
i = a.len-1;
while(i>=neg){
int div = a[i]/max;
if(div == 0) i--;
else{
a[i]%=max;
swap(a,i,neg+div-2);
}
}
}
Declare : private static void swapping(int[] a, int i , int j){
Assign: int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
END
Write an algorithm with θ (n**0.59) average complexity that locates the location ( index) of negative...
Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers) in an array. Note that the second largest element should be distinctly smaller than the largest element. You should also adhere to the following restrictions. (1) You should traverse through the array ONLY ONCE. (2) The array could have both positive and negative elements. So, you should NOT initialize any temporary variables to very small negative values as part of your algorithm. (3) You...
Show that the time complexity for the
number of assignments of records for the Mergesort algorithm
(Algorithms 2.2 and 2.4) is approximated by T (n)
= 2nlgn.
by Mingfu LI, CGUEE Algorithms Algorithms 2.2 : Mergesort O Problem Sort n keys in nondecreasing sequence. Inputs positive integer n, array of keys S index from 1 to n Output the array 5 containing the keys in nondecreasing sequence. void mergesort (int n, keytype S[]) (1 <u)и } keytype UI..h), ИI.m); copy...
What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...
What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...
Show that the time complexity for the
number of assignments of records for the Mergesort algorithm
(Algorithms 2.2 and 2.4) is approximated by T (n)
= 2nlgn.
by Mingfu LI, CGUEE Algorithms Algorithms 2.2 : Mergesort O Problem Sort n keys in nondecreasing sequence. Inputs positive integer n, array of keys S index from 1 to n Output the array 5 containing the keys in nondecreasing sequence. void mergesort (int n, keytype S[]) (1 <u)и } keytype UI..h), ИI.m); copy...
Problem 5: Recurrence relations and detailed analysis of recursive algorithm efficiency g(n: non-negative integer) 1. if n ≤ 1 then return n 2. else return (5 * g(n─1) ─ 6 * g(n─2)) MergeSort divides the array to be sorted into two equal halves, calls itself recursively on each half to sort that subarray, and then calls the Merge algorithm to merge the two sorted halves in linear time. This leads to its two recurrence relations T(n)=2T(n/2)+cn, n>1;...
Java question
Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...
Individually or in pairs, please write pseudo-code to solve the following problems and state which Big-θ category the algorithm belongs to (you don’t have to prove it). An array, A[0…n-2], contains n-1 integers from 0 to n-1 in increasing order – one integer is missing in the range. Design the most efficient algorithm you can to find the missing integer. You have an unsorted sequence of college applications in an array, A[0…n-1]. Each application A contains a name A.name, an...
JAVA What is the complexity of this algorithm? Assign each student in the class a number from 1 to n, where n is the number of students. Then ask each of the odd-numbered students whether he or she is left-handed. a. O(1) b. O(n) c. O(n ^ 2) d. O(log n) What is the complexity of this algorithm? In a very difficult CS class, half the n students who originally signed up drop the course after the first quiz. After...
Please help me with this algorithm assignment using Java Write 2 functions to calculate the MSS of a given array one with running time of O(n) and one with O(nlogn) .Request the user to enter a positive integer, and call it n. 1.Generate n random integers between -100 to 100 and save them in array a. 2.Print the generated array and the outputsof your two functions