08/12/2022

What is Sorting?

Sorting refers to arranging data in a particular format. The sorting algorithm specifies the way to.
arrange data in a particular order.
Sorting is nothing but storage of data in sorted order, Types of Sorting descending
order.
Sorting arranges data in a sequence which makes searching easier.

Complexity of Sorting algorithm

The complexity of the sorting algorithm calculates the running time of a function in which ‘n’ number of items is to be sorting.
The choice for which sorting method is suitable for a problem depends on several dependencies.
Configurations for different problems.
The most noteworthy of these considerations are:
The length of time spent by the programmer in.
So, Programming a specific sorting program.
Then, the Amount of machine time necessary for running the program.
The amount of memory necessary for running the program.

Types of Sorting

  1. Selection Sort
  2. Bubble sort
  3. Insertion sort
  4. Quick Sort
  5. Shell Sort
  6. Merge Sort etc.

Selection Sort Algorithm

In selection sort, the list is divide into two sub-lists sort and unsort.

than, These two lists are divide by imaginary wall.

So, We find the smallest element from the unsorted sub-list and swap it to the beginning. And then the wall moves one element ahead, as the sorted list is increases and unsorted list is decreases.

So, now Assume that we have a list on n elements.

By applying selection sort, the first element is compare with all remaining (n-1) elements. And than The the smallest element is place at the first location.

Again, the second element is compare with the remaining (n-1) elements.

At the time of the comparison, the smaller element is swap with the larger element. Similarly, an entire array is a check for the smallest element. And then swapping is done accordingly.

Algorithm Of Selection Sort Steps:

  1. Firstly, Pass1: Find the Location LOC of the smallest in the List N elements A[0], A[1]….A[N], and then interchange A[LOC] and A[0]. Then A[0] is Sorted.
  2. Secondly, Pass 2: Find the Location LOC of the smallest in the List N-1elements A[1], A[2]….A[N], and then interchange A[LOC] and A[1]. Then A[0],A[1] is Sorted. Since A[0]<=A[1].
  3. Thirdly, Pass N-1: Find the Location LOC of the smallest in the List A[N-1], A[N]and then interchange A[LOC] and A[N-1].Then A[0],A[1]……A[N-1]is Sorted. Since A[N-2]<=A[N-1].
  4. Thus A is Sorted after N-1 passes.
Selection sorting, sorting algorithm
Selection sorting, sorting algorithm
Selection sorting, sorting algorithm
Selection sorting, sorting algorithm

Example of Selection Sort

for (c = 0; c < (n - 1); c++) // 
 {
 position = c;
 for (d = c + 1; d < n; d++)
 {
 if (array[position] > array[d])
 position = d;
 }
 interchange(a[position],a[c]);
}

So, we gonna taken this numbers for the Selection sorting 77 33 44 11 88 22 66 55.

Algorithm

Selection_sort(A,n)
{
For i=0 to n-2
imin=i;
For j=i+1 to n-1
{
if(A[imin]>A[j])
imin=j;
}
Swap(A[i],A[imin])
}

Complexity of Selection sort: O(n2)

Then, Using C programming to perform this sorting operation you can follow any language.

So, try it your self on online compiler.

#include <stdio.h>
int main()
{
 int array[100], n, c, d, position, t;
 printf("Enter number of elements\n");
 scanf("%d", &n);
 printf("Enter %d integers\n", n);
 for (c = 0; c < n; c++)
 scanf("%d", &array[c]);
 for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times
 {
 position = c;
 for (d = c + 1; d < n; d++)
 {
 if (array[position] > array[d])
 position = d;
 }
 if (position != c)
 {
 t = array[c];
 array[c] = array[position];
 array[position] = t;
 }
 }
printf("Sorted list in ascending order:\n");
 for (c = 0; c < n; c++)
 printf("%d\n", array[c]);
 return 0;
}

so, visit our website and click here:-https://learningpoints.in/

thank you for coming to our website please share with your friends And do comment

Thank you.

Leave a Reply

Your email address will not be published.