anshu99.9's blog

By anshu99.9, history, 10 months ago, In English

Concepts needed for Selection Sorting: -

The easy way that new programmer can sort an array is by using Selection Sorting. In this process you going to use only basic array concept and iteration(for loop).

Definition: -

Select smallest(or largest) element from the unsorted part and move it to the sorted part of the array/list.

Logic of the Selection Sorting: -

-> We are Sorting it in increasing order.

  • We have an array named arr, it's size n and integer variable named min.

  • We will select the first element of the unsorted part of the array and compare with rest of the element of the unsorted part of array if we find element less than the selected element then we will store the index number into the integer variable named min.

  • We will use this min variable to swap the values in array.

  • So the selection of the element will be from index 0 to (n — 2). (n-2) is because we need at least two number for sorting, We can't sort only one element(last element of the array).

  • There will be nested loops: -

  1. In first loop we going to select the element of an array till (n — 2) and initialize min = i, So our loop**(i)** will go from 0 to (n — 2).

  2. In second loop we will iterate in all unsorted part of an array, So our second loop**(j)** will be from i to (n — 1).

  • In second loop we will compare if (arr[j] < arr[min]) then we will update min = j.

  • When second loop over then we will swap the values of arr[i] to arr[min].

  • This whole process will sort your array.

  • This is the logic of Selection Sorting Now you have a task to think how you going to convert this logic into code. If you find any difficulty then you can see the code which is available on google.

Thank you :)

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it