# Selection Sort

In selection sort, the smallest value among the unsorted elements of the array is selected in every pass and inserted to its appropriate position into the array.

First, find the smallest element of the array and place it on the first position. Then, find the second smallest element of the array and place it on the second position. The process continues until we get the sorted array.

The array with n elements is sorted by using n-1 pass of selection sort algorithm.

• In 1st pass, smallest element of the array is to be found along with its index pos. then, swap A and A[pos]. Thus A is sorted, we now have n -1 elements which are to be sorted.
• In 2nd pas, position pos of the smallest element present in the sub-array A[n-1] is found. Then, swap, A and A[pos]. Thus A and A are sorted, we now left with n-2 unsorted elements.
• In n-1th pass, position pos of the smaller element between A[n-1] and A[n-2] is to be found. Then, swap, A[pos] and A[n-1].

Therefore, by following the above explained process, the elements A, A, A,...., A[n-1] are sorted.

## Example

Consider the following array with 6 elements. Sort the elements of the array by using selection sort.

A = {10, 2, 3, 90, 43, 56}.

Pass Pos A A A A A A
1 1 2 10 3 90 43 56
2 2 2 3 10 90 43 56
3 2 2 3 10 90 43 56
4 4 2 3 10 43 90 56
5 5 2 3 10 43 56 90

Sorted A = {2, 3, 10, 43, 56, 90}

## Complexity

Complexity Best Case Average Case Worst Case
Time Ω(n) θ(n2) o(n2)
Space o(1)

## Algorithm

SELECTION SORT(ARR, N)

• Step 1: Repeat Steps 2 and 3 for K = 1 to N-1
• Step 2: CALL SMALLEST(ARR, K, N, POS)
• Step 3: SWAP A[K] with ARR[POS]
[END OF LOOP]
• Step 4: EXIT

SMALLEST (ARR, K, N, POS)

• Step 1: [INITIALIZE] SET SMALL = ARR[K]
• Step 2: [INITIALIZE] SET POS = K
• Step 3: Repeat for J = K+1 to N -1
IF SMALL > ARR[J]
SET SMALL = ARR[J]
SET POS = J
[END OF IF]
[END OF LOOP]
• Step 4: RETURN POS

## C Program

Output:

```printing sorted elements...
7
9
10
12
23
23
34
44
78
101
```

## C++ Program

Output:

```printing sorted elements...
7
9
10
12
23
23
34
44
78
101
```

## Java Program

Output:

```printing sorted elements...
7
9
10
12
23
23
34
44
78
101
```

## C# Program

Output:

```printing sorted elements...
7
9
10
12
23
23
34
44
78
101
```

## Python Program

Output:

```printing sorted elements...
7
9
10
12
23
23
34
44
78
101
```

## Rust Program

Output:

```printing sorted elements...

7
9
10
12
23
23
34
44
78
101
```

## JavaScript Program

Output:

```printing sorted elements ...
7
9
10
12
23
23
34
44
78
101
```

## PHP Program

Output:

```printing sorted elements ...
7
9
10
12
23
23
34
44
78
101
```

Next TopicShell Sort
{"ft_lang_label":"__label__en","ft_lang_prob":0.60791355,"math_prob":0.87438667,"size":1483,"snap":"2020-45-2020-50","text_gpt3_token_len":466,"char_repetition_ratio":0.18458419,"word_repetition_ratio":0.37226278,"special_character_ratio":0.36884692,"punctuation_ratio":0.1954023,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9737973,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-28T23:54:58Z\",\"WARC-Record-ID\":\"<urn:uuid:9e21abf3-149b-4bfd-b838-db89824b7d1c>\",\"Content-Length\":\"57695\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:47a95cd3-478b-4fc4-b41b-37304dd05b46>\",\"WARC-Concurrent-To\":\"<urn:uuid:3c9ffbe4-b59e-426a-b2fd-12fbed07dfa2>\",\"WARC-IP-Address\":\"194.169.80.121\",\"WARC-Target-URI\":\"https://www.javatpoint.com/selection-sort\",\"WARC-Payload-Digest\":\"sha1:JTAGZRCLWD7C2566KKVMBAHLU2PALXJA\",\"WARC-Block-Digest\":\"sha1:I2SMO5YO3QV5XHZNE4MMFV46MVEXCJXH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107902038.86_warc_CC-MAIN-20201028221148-20201029011148-00342.warc.gz\"}"}