Selection sort


Selection sort

The Selection Sort Algorithm will be discussed in this post. The selection sorting technique is also straightforward. This essay will be highly useful and fascinating to students who may be asked about selection sort in their exams. As a result, it is critical to debate the subject.

The smallest value among the array's unsorted items is chosen in each pass and inserted into the proper spot in the selection sort. It is also the most straightforward algorithm, and it's a sorting method that uses in-place comparisons. In this algorithm, the array is separated into two halves: the first is sorted, while the second is unsorted. The sorted component of the array is initially empty, while the unsorted section contains the specified array. The sorted items are on the left, while the unsorted items are on the right.

The first smallest element in an unsorted array is chosen and placed at the top in selection sort. The second smallest element is then chosen and positioned in the second location, and the procedure is repeated until the array is completely sorted.

Selection sort has an average and worst-case complexity of O(n2), where n is the number of items. It is not ideal for large data sets as a result of this.

When a small array needs to be sorted, selection sort is commonly utilized.

It makes no difference how much it costs to swap.

All elements must be double-checked.

Let's look at the selection sort algorithm now.

Algorithm

  1. SORT OF SELECTION (arr, n)
  2. Step 1: For I = 0 to n-1, repeat Steps 2 and 3.
  3. Step 2: CALL SMALLEST(arr, I n, pos) Step 3: SWAP arr[pos] with arr[i].
  4. [THE END OF THE LOOP]
  5. EXIT is the fourth step.
  6. THE SMALLEST (arr, i, n, pos)
  7. [INITIALIZE] is the first step. SMALL = arr[i] SET
  8. Step 2: [GET STARTED] CHANGE pos = I
  9. Step 3: If (SMALL > arr[j]), repeat for j = i+1 to n.
  10. SMALL = arr[j] SET
  11. [END OF IF] SET pos = j
  12. [THE END OF THE LOOP]
  13. STEP 4: RETURN TO THE ORIGINAL POSITION