Algorithm for the selection sort, Data Structure & Algorithms

Assignment Help:

Q. Give the algorithm for the selection sort. Describe the behaviours of selection sort when the input given is already sorted.                                                                                              

Ans.

Algorithm for the Selection Sort is as follows

SELECTION (A, N)

This algorithm sorts the given array A with N elements

1.  Repeat steps 2  & 3 for K = 1,2........N-1

2.  Call MIN (A,K, N, LOC)

3.  [Interchange A[K] and A[LOC].]

Set TEMP : = A[K], A[K]:=A[LOC] and A[LOC]: = TEMP, [END of Step 1 loop]

4.  EXIT

MIN (A, K, N, LOC)

An array A is in memory. This procedure finds the location LOC of the smallest element among A[K], A[K+1]...........A[N],

1.  Set MIN : =A[K] and LOC:=K [Initialize pointer]

2.  Repeat for J= K+1, K+2, .....N;

If MIN > A[J], then : set MIN: = A[J] and LOC:=J [End of loop]

3.  Return

The complexities of Selection sort are given below

 

Worst Case

Average Case

Best Case

O(n2)

O(n2)

O(n2)

 

So if the list is already sorted the it is the best case, then also the complexity of algorithm is O(n2). Therefore, there is no change in behaviors of selection sort if the list is already sorted.


Related Discussions:- Algorithm for the selection sort

Creation of Heap, Q. Create a heap with the given list of keys: ...

Q. Create a heap with the given list of keys: 8, 20, 9, 4, 15, 10, 7, 22, 3, 12                                                  Ans: Creation

File organisation, File organisation might be described as a method of stor...

File organisation might be described as a method of storing records in file. Also, the subsequent implications approaching these records can be accessed. Given are the factors invo

Algorithm for linear search, Here,  m represents the unordered array of ele...

Here,  m represents the unordered array of elements n  represents number of elements in the array and el  represents the value to be searched in the list Sep 1: [Initialize]

What is algorithms optimality, What is algorithm's Optimality? Optimali...

What is algorithm's Optimality? Optimality  is  about  the  complexity  of  the  problem  that  algorithm  solves.  What  is  the  minimum amount  of  effort  any  algorithm  w

Explain worst fit method, Worst Fit method:- In this method the system alw...

Worst Fit method:- In this method the system always allocate a portion of the largest free block in memory. The philosophy behind this method is that by using small number of a ve

Sort the Sequence Using Merge Sort, Q. Sort the sequence written below of k...

Q. Sort the sequence written below of keys using merge sort. 66, 77, 11, 88, 99, 22, 33, 44, 55                                                                      Ans:

Program segment for quick sort, Illustrates the program segment for Quick s...

Illustrates the program segment for Quick sort. It uses recursion. Program 1: Quick Sort Quicksort(A,m,n) int A[ ],m,n { int i, j, k; if m { i=m; j=n+1; k

Array-based representation of a binary tree, Assume a complete binary tree ...

Assume a complete binary tree T with n nodes where each node has an item (value). Label the nodes of the complete binary tree T from top to bottom & from left to right 0, 1, ..., n

Division-remainder hashing, According to this, key value is divided by any ...

According to this, key value is divided by any fitting number, generally a prime number, and the division of remainder is utilized as the address for the record. The choice of s

State the range of operation of abstract data type, State the range of oper...

State the range of operation of ADT Operations of the Range of T ADT includes following, where a, b ∈ T and r and s are values of Range of T: a...b-returns a range value (an

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd