Write a function that takes a numerical vector and returns the sorted vector. To detect whether a vector is sorted, use diff() and all() (or any()). So long as the vector is not sorted, pick two random positions from the vector and swap them. (in matlab program)
>> v = sortbyrandomswaps( [39 25 91 71] ) v = [ 25 39 71 91 ]
function v = sortbyrandomswaps(vec)
while(1)
k1 = randi(size(vec)) ;
k2 = randi(size(vec)) ;
temp = vec(k1);
vec(k1) = vec(k2);
vec(k2) = temp;
if all(diff(vec)>=0)
break;
end
end
v = vec;
end
v = sortbyrandomswaps( [39 25 91 71] )
v = sortbyrandomswaps( [5 4 3 2 1] )
===============================================
SEE OUTPUT
Thanks, PLEASE COMMENT if there is any concern.
Write a function that takes a numerical vector and returns the sorted vector. To detect whether...
*** write in matlab *** use for loops largestfactor_for Write a function that takes a positive integer greater than one and returns its largest factor (other than itseir). You must use a for loop to solve this problem. Do not use functions factor), primes(), and divisors() . Do not use while loops Do not use vectorized code. alargestfactor 105) 35 largestfactor_for Write a function that takes a positive integer greater than one and returns its largest factor (other than itseir)....
python 1. Write a function that takes in a string and returns the string sorted For example, "cat" becomes "act", "dog" becomes "dgo" Hint: You might need to use the functions join and sorted
Write a function maxList that takes in a list of integers as argument and returns their highest. You may not use the max() or the sorted() Python built-in functions.
What would the function look like in MATLAB? Write a function called mysort that takes a 3-element vector as its sole arguments. It uses if statements, possibly nested, to return a 3-element vector with its elements in non-decreasing order, i.e., the first element in the returned vector equals the smallest element of the input vector and the last element equals the largest element in the input vector. NOTE: Your function should not use any built-in functions, e.g., sort, min, max,...
1 write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple. Hint (can be done in one pass, you are not allowed to use built-on min and max functions.)max, min = find_max_min(my_list):2 write a Python function that takes in a list of integers (elements), and an integer number (num). The functions should count and return number of integers in elements greater than, less than, and equal to...
3. Write a function called sort3 that takes a 3-element vector as its sole arguments. It uses if-statements, possibly nested, to return the three elements of the vector as three scalar output-arguments in nondecreasino-roer , i.e., the first output argument equals the smallest element of the input vector and the last output argument equals the largest element. NOTE: Your function may NOT use any built-in functions, e.g., sort, min, max, median, etc. (5 points) (bonus question). Write a function called...
1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector containing the values of the integral (A) for n= 1,2,3,..., n. The function must use the relation (B) and the value of y(1). Your function must preallocate the array that it returns. Use for loop when writing your code. b) Write MATLAB script that uses your function to calculate the values of the integral (A) using the recurrence relation (B), y(n) for n=1,2,... 19...
Create a new function file and write a function s= SUM_EVEn_ ubitname(V) that takes a vector V of arbitrary length and determines the sum of the even element and assign it to s. your function calculations must do the following: * Use a loop that iterates through every element of V and adds s only even element and skips all other elements. * Contain the appropriate H1 and help text lines. * Note: A number is even, if round(number/2) is...
in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...
Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...