Question

Selection-Sort( A[1..n] ) 1 2 3 4 5 6 7 8 9 10 // INPUT: A[1..n],...

Selection-Sort( A[1..n] )

1 2 3 4 5 6 7 8 9
10
// INPUT: A[1..n], an array of any n numbers in unknown order integer i, j, m
fori=1ton−1
do
swap A[i] ↔ A[m]

// OUTPUT: A[1..n], its numbers now sorted in non-decreasing order
m=i
for j = i to n
do if A[j] < A[m] then m = j


Give a proof that this algorithm sorts its input as claimed.

Homework Answers

Answer #1

selection sort algorithm
selectionSort(A,n)
min = 0

for (i = 0; i < n-1; i++)
{
min = i;
for (j = i+1 to n-1)
if (A[j] < A[min])
min = j;

swap(A[min], A[i]);
}

Proof using Loop Invariant
Loop Invariant : After i iteration ,all elements from index 0 to i-1 array A[] is sorted in non decreasing order.And all entries in A[i..n-1] are larger than or equal to the entries in A[0..i-1]

Base case :initially (when i=0),there is no any element from index 0 to -1.

Maintenance: In each outer loop iteration,inner loop finds minimum element in A[i to n-1] and put it at ith place.which make array A[0 to i] sorted in non decreasing order(Because all entries in A[i..n-1] are larger than or equal to the entries in A[0..i-1]).

Termination: when i=n all element in array A[0..n-1] are sorted in non decreasing order.

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
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
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]
Test each algorithm (Counting Sort, Radix Sort, and Bucket Sort algorithms) on three different arrays of...
Test each algorithm (Counting Sort, Radix Sort, and Bucket Sort algorithms) on three different arrays of 1000 elements each with the following properties: By the way: To perform bucket sort the right way, convert the array elements to a value between 0 and 1 Array1: integers only in the range 0 through 999, already sorted in increasing order Array2: integers only in the range 0 through 999, already sorted in decreasing order Array3: integers only in the range 0 through...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 1.Design an algorithm to find all the common elements in two sorted lists of numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2,...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...
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.
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
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
A = [1 2 3 4 5 6 7 8 9 10 11 12 13 14...
A = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] In MATLAB. Use a fully vectorized code (ie. no loops) to determine when the numbers have increased to at least 15 in the above array. Report answer to command window.
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...