Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Selection sort java

// Java program for implementation of Selection Sort
class SelectionSort
{
    void sort(int arr[])
    {
        int n = arr.length;

        // One by one move boundary of unsorted subarray
        for (int i = 0; i < n-1; i++)
        {
            // Find the minimum element in unsorted array
            int min_idx = i;
            for (int j = i+1; j < n; j++)
                if (arr[j] < arr[min_idx])
                    min_idx = j;

            // Swap the found minimum element with the first
            // element
            int temp = arr[min_idx];
            arr[min_idx] = arr[i];
            arr[i] = temp;
        }
    }

    // Prints the array
    void printArray(int arr[])
    {
        int n = arr.length;
        for (int i=0; i<n; ++i)
            System.out.print(arr[i]+" ");
        System.out.println();
    }

    // Driver code to test above
    public static void main(String args[])
    {
        SelectionSort ob = new SelectionSort();
        int arr[] = {64,25,12,22,11};
        ob.sort(arr);
        System.out.println("Sorted array");
        ob.printArray(arr);
    }
}
/* This code is contributed by Rajat Mishra*/
Comment

Selection Sort Java

// Java Selection Sort
// -------------------

/* 
   Time Complexity
     Best Time Complexity:O(n^2)
	 Average Time Complexity:O(n^2)
	 Worst Time Complexity:O(n^2)
     
   Space Complexity
     No auxiliary space is required in Linear Search implementation.
	 Hence space complexity is:O(1)
*/

import java.util.*;

public class SelectionSort {
    public static int[] sort(int arr[]) {
        for (int i = 0; i < arr.length; i++) {
            int k = i; // referring to current index
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[k]) {
                    k = j;
                }
            }
            int temp;

            //Swapping algorithm
            temp = arr[i];
            arr[i] = arr[k];
            arr[k] = temp;
        }

        return arr; // returns the sorted array
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int arr[] = new int[10];

        System.out.println("Enter Array Values: ");
        for (int i = 0; i < arr.length; i++) {
            arr[i] = sc.nextInt();
        }

        int a[] = new int[arr.length];

        a = Arrays.copyOf(sort(arr), arr.length); // copies the sorted array to another array

		//sorted in ascending order
        System.out.println("Sorted Array:");
        for (int i = 0; i < a.length; i++) {
            System.out.print(+a[i] + " ");
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Java Program to Change the Border of a JFrame: 
Java :: border swing 
Java :: java sub function 
Java :: put arraylist in hashtable java 
Java :: date java use fix timezone 
Java :: java system.out.println shortcut 
Java :: Java Hashmap Replace Elements 
Java :: show all spring boot beans 
Java :: switch en java 
Java :: pythagoras method in java 
Java :: Java Create a ByteArrayInputStream 
Java :: Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8 
Java :: share location in android programmatically 
Java :: arraylist replace 
Java :: permutation and combination program in java 
Java :: scaletype android dynamic 
Java :: java split on { 
Java :: java list of a class has a string that is equal to 
Java :: Java empty() Method 
Java :: how to print each element of an arraylist on a new line in java 
Java :: Java Remove Elements from HashSet 
Java :: java parameterized constructor 
Java :: edittext editable false android java 
Java :: how to add random numbers to an array 
Java :: binary tree traversal 
Java :: encapsulation in java 
Java :: loop and save letters in a string java 
Java :: variable might not have been initialized error 
Java :: properties object java 
Java :: java mahout get clusters centers 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =