Skip to main content

Selection Sort Algorithm | Sorting in JAVA

                                                       Sorting in JAVA


  1. Selection Sort

Idea: The inner loop selects the minimum element in the unsorted array and places the elements in increasing order. 

Time complexity: O(N2


Code

import java.util.*;

 

class Sorting {

   public static void printArray(int arr[]) {

       for(int i=0; i<arr.length; i++) {

           System.out.print(arr[i]+" ");

       }

       System.out.println();

   }

 

   public static void main(String args[]) {

       int arr[] = {7, 8, 1, 3, 2};

 

       //selection sort

       for(int i=0; i<arr.length-1; i++) {

           int smallest = i;

           for(int j=i+1; j<arr.length; j++) {

               if(arr[j] < arr[smallest]) {

                   smallest = j;

               }

           }

           //swap

           int temp = arr[smallest];

           arr[smallest] = arr[i];

           arr[i] = temp;

       }

 

       printArray(arr);

   }

}




Sure! Here's an example implementation of the selection sort algorithm in Java:

java
Copy code
public class SelectionSort {

public static void selectionSort(int[] arr) {
int n = arr.length;

for (int i = 0; i < n-1; i++) {
int minIndex = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}

// swap the minimum element with the current element
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
selectionSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}


In this implementation, the selectionSort() method takes an array of integers as input and sorts the array using the selection sort algorithm. The outer loop iterates through the array, and the inner loop finds the index of the minimum element in the unsorted part of the array. Once the minimum element is found, it is swapped with the current element being sorted. Finally, the sorted array is printed to the console using a for loop in the main() method.


Comments

Popular posts from this blog

Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.

Enter 3 numbers from the user & make a function to print their average. To find the average of three numbers, you need to add the three numbers together and then divide the sum by three. Here's how you can write a function in C++, C, Python, and Java to take three numbers from the user and calculate their average: JAVA import java . util .*; public class Solutions {     public static void main ( String args []) {        Scanner sc = new Scanner ( System . in );        int a = sc . nextInt ();        int b = sc . nextInt ();        int c = sc . nextInt ();          int average = ( a + b + c ) / 3 ;        System . out . println ( average );    }    }   C++: // c++ #include <iostream> using namespace std ;...

2D Arrays | Java Complete Placement Course | Lecture 11

Java - Introduction to Programming Lecture 11 2D Arrays In Java It is similar to 2D matrices that we studied in 11th and 12th class. Creating a 2D Array - with new keyword int [][] marks = new int [ 3 ][ 3 ] ; Taking a matrix as an input and printing its elements. import java . util .*;   public class TwoDArrays {     public static void main ( String args []) {         Scanner sc = new Scanner ( System . in );         int rows = sc . nextInt ();         int cols = sc . nextInt ();           int [][] numbers = new int [ rows ][ cols ];           //input         //rows         for ( int i = 0 ; i < rows ; i ++) {             //columns  ...

Merge Sort | How do I learn merge sort?

       Merge Sort | For Beginners | Java Placement Course >> Merge sort is a popular sorting algorithm that utilizes a divide-and-conquer approach to sort elements in an array. The basic idea behind merge sort is to divide an unsorted array into two halves, recursively sort each half, and then merge the two sorted halves into a single sorted array. >>  Merge sort algorithm works by recursively dividing an array into two halves, sorting each half, and then merging them to obtain the sorted array. The basic steps of the merge sort algorithm are as follows: Divide the unsorted array into two halves, mid and left. Sort the left half recursively using merge sort algorithm. Sort the right half recursively using merge sort algorithm. Merge the two sorted halves to obtain the final sorted array. JAVA Implementation: // Merge sort public class MergeSort {     //time complexity=nlogn     public static void conquer ( int arr [], int ...