In: Computer Science
Question #1 (Java)
- Which sorting does in-place sorting?
- Which sort does the minimum swap to order ascending/descending manner?
Note: I just need a short answer to the question no code.
Answer:---
I am using Selection Sort for in-place sorting
The selection sort algorithm sorting an array by repeatedly finding the minimum element. That we considering ascending order from unsorted part and placed at the beginning.
The Selection sort algorithm maintains two subarrays in a given array.
· The subarray which is already sorted.
· Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element considering ascending order from the unsorted subarray is picked and moved to the sorted subarray.
Worst complexity: n^2
Average complexity: n^2
Best complexity: n^2
Space complexity: 1
Following Steps :--
arr[] = 12 60 64 25 13 10
// Given array 10 is minimum so we are swapping from 12 (first position) to 10
10 60 64 25 13 12
// Next smallest value 12, this value swap from 60(second position) to 12
10 12 64 25 13 60
// Next smallest value 13, this value swap from 64(third position) to 13
10 12 13 25 64 60
// Next smallest value 25, we are placed same position
10 12 13 25 64 60
// Next smallest value 60, this value swap from 64(last position) to 60
10 12 13 25 60 64