Selection Sort in Java – The smallest element in an unsorted array is brought to the front of the selection sort.
You’ll go through each item until you discover the smallest one (from left to right). The first item in the array is now sorted, while the rest is still unsorted.
This article has project files included, just scroll down and click the download button and you can edit the project source code for free.
Example Program:
package selectionsort_in_java;
import java.util.Scanner;
public class SelectionSort_in_Java {
public static void Sort(int a[]) {
int n = a.length, i, j, p, temp;
for (i = 0; i < n - 1; i++) {
p = i;
for (j = i + 1; j < n; j++) {
if (a[p] > a[j]) {
p = j;
}
}
temp = a[p];
a[p] = a[i];
a[i] = temp;
}
}
public static void printarray(int a[]) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
public static void main(String[] args) {
int n, res, i;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in the array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter " + n + " elements ");
for (i = 0; i < n; i++) {
a[i] = s.nextInt();
}
System.out.println("elements in array ");
printarray(a);
Sort(a);
System.out.println("\nelements after sorting");
printarray(a);
}
}
Output:
//Selection Sort in Java: Sample Code Output
Enter number of elements in the array:4
Enter 4 elements
7
3
4
9
elements in array
7 3 4 9
elements after sorting
3 4 7 9
What is the importance of the Selection sort in the Java program?
The most significant aspect of the Selection sort algorithm is to realize that it maintains two sub-arrays: one is the sorted array, and the other is the unsorted array. The unsorted array is another sub-array.
What is the use of the Program for selection sort in java?
We can use selection sort to order array elements in a Java program. We look for the lowest element in the selection sort algorithm and place it in the appropriate spot. The current element is replaced with the next lowest number.
About the Project
Selection sort is a sorting algorithm that selects the smallest element from an unsorted list and sets it at the top of the unsorted list in each iteration.
The Selection sort in Java using recursion below covers all of the necessary and vital elements that can be used for college projects by first-year, second-year, and final-year IT students.
The project is entirely written in Java and built in the Netbeans code editor.
Project Details and Technology
Project Name: | Selection Sort in Java with Source Code |
Abstract | Selection sort works by keeping two sub-arrays apart. The unsorted subarray’s minimal element is placed in its rightful location in a sorted subarray. The second-lowest element is then positioned correctly. This method sorts the entire array by selecting the smallest entry at each iteration. |
Language/s Used: | Java |
Java version (Recommended): | 8 |
Database: | N/A |
Type: | Desktop Application |
Developer: | sourcecodehero |
Updates: | 0 |
To start executing a Selection Sort in Java with Source Code, make sure that you have NetBeans IDE or any platform code editor installed on your computer.
code for selection sort in java: Follow the steps below
Time needed: 3 minutes
- Step 1: Download Source Code
To get started, find the downloaded source code file below and click to start downloading it.
- Step 2: Extract File
Navigate to the file’s location when you’ve finished downloading it and right-click it to extract it.
- Step 3: Run the project
Next, open NetBeans IDE and click open project, and choose your download.
- Step 4: Run the project.
Next, right-click the project folder and click run.
Download the Source Code below
Summary
For those Web developers interested in learning more about web apps, this system is a fully functional project.
Related Articles
- Quicksort in Java with Source Code
- Bubble Sort Java Program with Source Code
- Merge Sort Java Program with Source Code
- Insertion Sort in Java with Source Code
Inquiries
If you have any questions or comments on the project, please leave a comment below.