Skip to content

📊Selection Sort : prioritzing tasks by selection sort🐧

Notifications You must be signed in to change notification settings

Abhishek-Singh296/Selection-Sort

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

Selection-Sort

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the unsorted portion of the list and moving it to the sorted portion of the list. The algorithm repeatedly selects the smallest (or largest) element from the unsorted portion of the list and swaps it with the first element of the unsorted portion. This process is repeated for the remaining unsorted portion of the list until the entire list is sorted. This algorithm chooses the minimum element from the list of elements and swaps it with the first element of the list. Similarly, it chooses the second minimum element from the list and swaps it with the second element of the list. It continue swapping the elements until all the elements of the list are completely sorted in either direction.

This selection sort in C is an in-place algorithm as it swaps the elements in the list itself. It does not require an extra list or array to sort the elements.


Working

1.Set the first element as minimum.

Selection-sort-0-initial-array (1)

2.Compare minimum with the second element. If the second element is smaller than minimum, assign the second element as minimum. Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing.The process goes on until the last element.

Selection-sort-0-comparision

3.After each iteration, minimum is placed in the front of the unsorted list.

Selection-sort-0-swapping

4.For each iteration, indexing starts from the first unsorted element. Step 1 to 3 are repeated until all the elements are placed at their correct positions.


Algorithm

The algorithm of the Selection Sort in C is as follows -

- Make a variable (say min_index) and initialize it to the location 0 of the array.
- Traverse the whole array to find the smallest element in the array.
- While traversing the array, if we find an element that is smaller than min_index then swap both these elements.
- After which, increase the min_index by 1 so that it points to the next element of the array.
- Repeat the above process until the whole array is sorted.
- The whole algorithm has been depicted with the help of a pictorial representation below.

Code

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int SelectionSort(int a[], int array_size)
{
    int i,j, smallest,temp;
    for(i=0;i<array_size-1;i++)
    {
      smallest=i;
      for(j=i+1;j<array_size;j++)
      {
          if(a[smallest]>a[j])
          smallest=j;
      }
      if(i!=smallest)
      {
                     temp=a[i];
                     a[i]=a[smallest];
                     a[smallest]=temp;
      }
    }
    printf("\nSeelctionSorted Data :");
    for (i = 0; i < array_size; i++) {
    printf("\t%d", a[i]);
}
}

int main()
{
  int ret;
  int arr[5]={32,5,7,3,72};
  SelectionSort(arr,5);
}

Output

Screenshot 2023-04-24 023757


About

📊Selection Sort : prioritzing tasks by selection sort🐧

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages