Skip to main content

Selection Sort in C

Selection Sort in C

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list. The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning.
In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.


Following example explains the above steps:
arr[] = 64 25 12 22 11

// Find the minimum element in arr[0...4]
// and place it at beginning
11 25 12 22 64

// Find the minimum element in arr[1...4]
// and place it at beginning of arr[1...4]
11 12 25 22 64

// Find the minimum element in arr[2...4]
// and place it at beginning of arr[2...4]
11 12 22 25 64

// Find the minimum element in arr[3...4]
// and place it at beginning of arr[3...4]
11 12 22 25 64 

Input:

8

6  -5  17  2  90  12  34  1

Output:

-5 1 2 6 12 17 34 90


Solution: 

#include <stdio.h>

void swap (int a, int b)

{

  int t;

  t = a;

  a = b;

  b = t;

}

void SelectionSortArray (int *arr, int len)

{

  for (int i = 0; i < len; i++)

    {

      int min = i;

      int t = 0;

      for (int j = i; j < len; j++)

{

  if (arr[j] < arr[min])

    {

      min = j;

    }

}

      t = arr[i];

      arr[i] = arr[min];

      arr[min] = t;

    }

}

int main ()

{

  int a[100], n;

  scanf ("%d", &n);

  for (int i = 0; i < n; i++)

    {

      scanf ("%d", &a[i]);

    }

  SelectionSortArray (a, n);

  for (int i = 0; i < n; i++)

    {

      printf ("%d\t", a[i]);

    }

  return 0;

}


_________________________________________________________

Related Articles:

Sorting array in descending order using bubble sort


Please Like, Comment and Share!😉


Comments

Popular posts from this blog

Fibonacci Pattern/Series C program

C Program to print fibonacci pattern till N Input: 5 (N value) Output: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Solution:   #include <stdio.h> int Fibonacci(int num){     if(num<=1)         return num;     else     return Fibonacci(num-1)+Fibonacci(num-2) ;//2,1 } int main() { int n,f=1;     scanf("%d",&n);     for(int row=1;row<=n;row++,printf("\n")){         for(int col=1;col<=row;col++){             printf("%d\t",Fibonacci(f++));         }     }     return 0; } ___________________________________________________________ Please Like, Comment and Share! 😉

Leetcode Solutions (Array101) in C++

Max Consecutive Ones Given a binary array  nums , return  the maximum number of consecutive  1 's in the array . Example1: Input: nums=[1,1,0,1,1,1] Output: 3 Explanation:   The first two digits or the last three digits are consecutive 1s.  The maximum number of consecutive 1s is 3. Example2: Input: nums=[1,0,1,1,0,1] Output: 2 Constraints: 1 <= nums.length <= 10 5 nums[i]  is either  0  or  1 . CPP Code:     int findMaxConsecutiveOnes(vector<int>& nums) {         int max=0,count=0;         for (int x: nums)         {             if(x==1){                 count++;                 if(count>max)                     max=count;             }         ...

Removing Odd numbers from a linked list

Deleting odd numbers from a linked list in python: In this program, we will see  how to remove odd numbers from a linked list in Python. This is a very interesting program where we will learn how to remove odd numbers from a linked list. This contains a very easy to understand method of making a linked list. So when you know how to use the linked list we will learn how to remove odd numbers from it. (Note that the program done here is done without using recursion for further simplicity). For removing odd number we first find out the position of the “ head “.  The head is the starting node in a linked list.   Source Code:   class Node:     def __init__(self,data=None,next=None):         self.data=data         self.next=next class SingleLinkedList:          def __init__(self):         self.head=None           def print_list(self):   ...