0% found this document useful (0 votes)
90 views

Java Program To Implement Selection Sort

The document describes a Java program that implements selection sort on an integer array. It contains a main method that initializes an integer array, prints the array before and after sorting, and calls the selection sort method. The selection sort method loops through the array, finds the smallest element in each pass, and swaps it into the current slot.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Java Program To Implement Selection Sort

The document describes a Java program that implements selection sort on an integer array. It contains a main method that initializes an integer array, prints the array before and after sorting, and calls the selection sort method. The selection sort method loops through the array, finds the smallest element in each pass, and swaps it into the current slot.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Program to implement Selection Sort

package onecaintacollege;

public class SelectionSortAlgo {

public static void main(String a[])


{
int[] myArray = {860,8,200,9};

System.out.println("------Before Selection Sort-----");

printArray(myArray);

selection(myArray);//sorting array using selection sort

System.out.println("-----After Selection Sort-----");

printArray(myArray);
}

public static void selection(int[] array)


{
for (int i = 0; i < array.length - 1; i++)
{ System.out.println("Sort Pass Number "+(i+1));
int index = i;
for (int j = i + 1; j < array.length; j++)
{
System.out.println("Comparing "+ array[index] + " and " + array[j]);
if (array[j] < array[index]){
System.out.println(array[index] + " is greater than " + array[j] );
index = j;

}
}

int smallerNumber = array[index];


array[index] = array[i];
array[i] = smallerNumber;
System.out.println("Swapping Elements: New Array After Swap");
printArray(array);
}
}
static void printArray(int[] array){

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


{
System.out.print(array[i] + " ");
}
System.out.println();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy