Question

How do you take an array for example A = [4 6 7 1 2 5]...

How do you take an array for example A = [4 6 7 1 2 5] and sort it from ascending order. So it would output A = [1 2 4 5 6 7]. In MATlab without using the sort function as well.

Homework Answers

Answer #1

HI, Please find my implementation of selection sort.


%--------------------------------------------------------------------------
% Syntax: sx = selectionSort(x);
%   
% Inputs: x is a vector of length n
%   
% Outputs: sx is the sorted (ascending) version of x
%   
% Description: This function sorts the input array x in ascending order
% using the selection sort algorithm
%   
% Complexity: O(n^2) best-case performance
% O(n^2) average-case performance
% O(n^2) worst-case performance
% O(1) auxiliary space
%--------------------------------------------------------------------------

% Seletion sort

function x = selectionSort(x)
   n = length(x);
   for j = 1:(n - 1)
   % Find jth smallest element
   imin = j;
   for i = (j + 1):n
   if (x(i) < x(imin))
   imin = i;
   end
   end
  
   % Put jth smallest element in place
   if (imin ~= j)
   x = swap(x,imin,j);
   end
   end

end

% Swap x(i) and x(j)
% Note: In practice, x xhould be passed by reference

function x = swap(x,i,j)
   val = x(i);
   x(i) = x(j);
   x(j) = val;

end

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]...
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] W X D T P N R Q K M E If the array was being sorted using the SHELL sort and the halving method, and sorting into ASCENDING order as demonstrated in the course content, list the letters in the resulting array, in order AFTER the FIRST pass. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
Giving an array of integers A[1:8] ={5, 1, 3, 2, 7, 6, 8, 4}. Build a...
Giving an array of integers A[1:8] ={5, 1, 3, 2, 7, 6, 8, 4}. Build a MAX-HEAP on A. Show the steps using binary tree representation or array view. Use heap sort: show the array after the first, the second, and the third of heap sort
using MATLAB Write a function which will take two inputs: an array ? and an integer...
using MATLAB Write a function which will take two inputs: an array ? and an integer ?. Here, 1 <= ? < ?????h(?). The objective is to find a contiguous subarray of length ? with the largest sum, within the array ?. The output of the function will be that largest sum. For example, x = [1 2 -1 3], and k =2. Then there are 3 possible contiguous subarrays. 1st subarray : [ 1 2], sum = 3 2nd...
This program is in C++, And please consider " sort pass #" for the output: Write...
This program is in C++, And please consider " sort pass #" for the output: Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT(...
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT( TRUE/ FALSE) QUIZ 8 Array Challenge Have the function ArrayChallenge(arr) take the array of numbers stored in arr and return the string true if any two numbers can be multiplied so that the answer is greater than double the sum of all the elements in the array. If not, return the string false. For example: if arr is [2, 5, 6, -6, 16, 2,...
php please shiftNTimes // Modify array so it is "left shifted" n times -- so shiftNTimes(array(6,...
php please shiftNTimes // Modify array so it is "left shifted" n times -- so shiftNTimes(array(6, 2, 5, 3), 1) // changes the array argument to (2, 5, 3, 6) and shiftNTimes(array(6, 2, 5, 3), 2) // changes the array argument to (5, 3, 6, 2). You must modify the array argument by // changing the parameter array inside method shiftNTimes. A change to the // parameter inside the method shiftNTimes changes the argument if the // argument is passed...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] dataset. As you can see Y is a discrete variable. Please write down a probability mass function for Y. Remember the example of pmf for die rolling experiment;
CHAPTER 2 MATLAB EXERCISES QUESTION 1: Enter the matrices A=[0 -4 5] and B= [-5 6...
CHAPTER 2 MATLAB EXERCISES QUESTION 1: Enter the matrices A=[0 -4 5] and B= [-5 6 7] 3 1 -2 0  -1 2 2 1 4 4 0 -3 USE MATLAB to find a) A + B b) B - 3A c) AB d) BA QUESTION 6: ENTER THE THREE MATRICES A= [1 2 3 4] B= [4 -6 3 5] 2 3 4 5 0 -3 -6 8 3 4 5 6   3 5 0 7 4 5 6 7...
C D 3 2 6 7 8 5 9 4 1 0 3 4 Using the...
C D 3 2 6 7 8 5 9 4 1 0 3 4 Using the predict function in R, find each of the following predicted values of D, for the given value of C regardless of whether or not it is appropriate to do so. In real life you should not calculate for predictions when it is not appropriate, but this is just for practice. Find when C = 6 Find when C = 4 Find when C =...