EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Data Structures Tutorial Selection Sort in Data Structure
Secondary Sidebar
Data Structures Tutorial
  • Basics
    • Linked List Advantages
    • What is Data Structure
    • Heap Data Structure
    • Types of Trees in Data Structure
    • AVL Tree in Data Structure
    • B Tree in Data Structure
    • B+ Tree in Data Structure
    • DFS Algorithm
    • BFS Algorithm
    • Arrays in Data Structure
    • Graph in Data Structure
    • Graph Representation
    • Breadth First Search
    • Depth Limited Search
    • Hashing in Data Structure
    • Searching in Data Structure
    • Linear Search in Data Structure
    • Linked List in Data Structure
    • Doubly linked list in Data Structure
    • Circular Linked List in Data Structure
    • Pointers in Data Structure
    • Types of Graph in Data Structure
    • Bubble Sort in Data Structure
    • Quick Sort in Data Structure
    • Bitonic Sort
    • Merge Sort in Data Structure
    • Selection Sort in Data Structure
    • Insertion Sort in Data Structure
    • Radix Sort in Data Structure
    • Stack in Data Structure
    • Queue in Data Structure
    • Priority Queue in Data Structure
    • Asymptotic Analysis
    • Tree Traversal in Data Structure
    • Tree Traversal Techniques
    • Trie Data Structure
    • Splay Tree in Data Structure
    • Spanning Tree Algorithm
    • Sparse Matrix in Data Structure
    • Radix Sort Algorithm
    • Counting Sort Algorithm
    • Skip List Data Structure
    • Linked List Algorithm
    • Linked List Types
    • Inorder Traversal of Binary Tree
    • Kruskals Algorithm
    • Prims Algorithm
    • BFS VS DFS
    • BCNF
    • Skip List
    • Hash Table?in Data Structure
    • Data Structure Interview Questions
    • Data Structures & Algorithms Interview
    • AVL Tree Deletion
    • B+ Tree Deletion
    • Decision Tree Advantages and Disadvantages
    • Data Architect Skills
    • Data Architecture Principles
    • Data Engineer Jobs
    • Data Engineer Roadmap
    • Fundamentals of Data Structure
    • Circular queue in Data Structure
    • Spanning Tree in Data Structure
    • Tree traversal types
    • Deque in Data structure
    • Shell Sort in Data Structure
    • Heap sort in data structure
    • Heap data structure C++
    • Heap data structure in Java
    • Binary Search Tree Types
    • Binary Tree in Data Structure
    • Binary Tree Types
    • Binary search tree in data structure
    • Binary Search Tree Advantages
    • Binary Search Tree Properties
    • Binary Search in Data Structure
    • Binary Tree Deletion
    • Sparse Matrix Multiplication
    • Preorder Traversal of Binary Tree
    • Postorder traversal
    • Decision Tree Hyperparameters
    • PostOrder Traversal without Recursion
    • AVL Tree Rotation
    • Avro File Format
    • Decision Tree Types
    • Binomial heap
    • Confluence Jira Integration
    • Timm Sort
    • Depth First Search
    • Stock Span Problem

Selection Sort in Data Structure

By Priya PedamkarPriya Pedamkar

Selection-Sort-in-Data-Structure

Introduction to Selection Sort in Data Structure

Selection sort is one of the sorting algorithms which arrange the data in ascending order. What selection sort does is that it searches for the lowest value element in the array and then brings it to the first position. Later, it finds the lowest value element from the remaining array, and after searching such an element, it brings it to the second position. Likewise, the algorithm goes on sorting the array finally ending up with the sorted array. Here we will discuss the algorithm and implementation of selection sort in data structure with steps.

Algorithm for Selection Sort

We shall now go through the algorithm for selection sort. Just observe how the operations occur as using this algorithm, we will implement a program for selection sort.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Algorithm:

function selection sort (array [], integer variable n)
declare integer variable position
declare index variables i, j
begin:
loop (i = 0, i < n-1, i = i +1)
begin:
position = i
loop(j = i + 1, j < n, j = j ++)
begin:
if(array[j] < array[position]) then
position = j
end if
end
swap(array[position], a[i])
end
end

Program for Implementing Selection Sort

The program code to implement selection sort is as given below. This program can be implemented in any programming language of our choice. We implemented the program in C programming. Go through the program and each of the coding elements, including variables, statements, and loops to understand how these elements function.

Code:

#include <stdio.h>
#include <conio.h>
void main()
{
int arr[50], num, i, j, pos, temp;
printf("Enter the number of elements in the array: ");
scanf("%d", &num);
printf("\nEnter the numbers: ");
for(i = 0; i < num; i++)
{
scanf("%d", &arr[i]);
}
for(i = 0;i < (num - 1); i++)
{
pos = i;
for(j = i + 1; j < num; j++)
{
if (arr[pos] > arr[j])
pos = j;
}
if(pos != i)
{
temp = arr[i];
arr[i] = arr[pos];
arr[pos] = temp;
}
}
printf("\nThe array sorted in ascending order is as follows.\n");
for(i = 0; i < num; i++)
printf("%d ", arr[i]);
getch();
}

We tested the above program with multiple inputs, and the program worked correctly. Click on the Run option and pass the required input to validate the program. Let’s go through the various inputs that we passed into and the results that we got.

Input 1

In this case, we passed five different two digits numbers. While passing the elements, ensure that they are separated by space or enter. E.g. if we intend to pass three numbers 23, 45, and 87 then pass them as 23 45 87 and not as 234587 or else the compiler will consider it a single element. The following screenshot shows this.

Selection Sort in Data Structure in1

Input 2

In this case, we tested the program by passing an array containing eight elements. The array has elements present in the descending order. When executed, the program worked correctly giving the array sorted in ascending order of the numeric elements. Let’s go through the following programming output.

Selection Sort in Data Structure in2

Input 3

In this case, we passed nine different numbers, as mentioned earlier, while passing numbers separate them with space. We must pass a specified number of elements only. As shown in the below screenshot, we got the input array sorted in the ascending order. So, this shows that our algorithm has worked correctly.

StringBuffer Class in Java eg3

How the Selection Sort Algorithm Works?

In the above section, we saw how the selection sort algorithm gave us correct results after testing the program implementing the algorithm through various inputs. We will go through the steps that are followed by a selection sort algorithm by following which we get the array sorted in ascending order.

To understand how the algorithm works, we are considering an array that we passed in the second input example described above. The array consists of eight elements present in descending order. These eight numeric elements are 98, 93, 87, 64, 57, 48, 32, and 12. The steps followed by the selection sort algorithm to sort these elements in ascending order are described below.

Step 1: Initially, the input array looks like as shown:

Selection Sort img1

Step 2: The lowest element from the entire array is brought to the first place. So, 12 will come to the first place, and 98 will go to the last place, as shown below.

Selection Sort img2

Step 3: Now, the first element remains intact, and all the operations happen with the rest of the array elements. The process works the same as in the above step. From the remaining array, the lowest element is brought to the second position. So, here 32 will come to the second position and 93 will go to the position of 32. The array after swapping looks like as shown below.

img3

Step 4: The next smallest element in the remaining array is 48. Now, 48 gets swapped with 87, and after the positions of these two elements are changed, 48 comes to the third position, and the array looks like as shown in the below screenshot.

img4

Step 5: If we see the array now, we find that it almost looks as if it has got sorted. In the remaining array now we have only two elements left which are 64 and 57. Had 57 been at 64’s position, the selection sort would have given us the ascendingly sorted array in the above step itself. However, now, the position of these two elements must be changed. So, selection sort brings 57 at the position of 64, the former numeric element being lower than the latter. After the swapping takes place, the array looks like as shown below.

img5

Conclusion – Selection Sort in Data Structure

The selection sort is a straightforward algorithm to implement. Irrespective of the programming tool used for implementing the algorithm, the concept remains the same. Before implementing the program, the algorithm must be studied to understand the functioning of the algorithm.

Recommended Articles

This is a guide to Selection Sort in Data Structure. Here we discuss the introduction, how it works, the program, and the algorithm for selection in the data structure. You can also go through our other suggested articles to learn more –

  1. Arrays in Data Structure
  2. NumPy Data Types
  3. Models in Data Mining
  4. Data Analysis Software
  5. B Tree in Data Structure | How to Works?
  6. How to Work JavaScript Array Sort?
  7. Stack in Data Structure | How to Works?
Popular Course in this category
All in One Data Science Bundle (360+ Courses, 50+ projects)
  360+ Online Courses |  1500+ Hours |  Verifiable Certificates |  Lifetime Access
4.7
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
SQL Training Program (10 Courses, 8+ Projects)4.8
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more