SlideShare a Scribd company logo
(JAVA NetBeans) Write a Java program able to perform selection sort.
Solution
import java.util.Scanner;
/**
* @author
*
*/
public class SelectionSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = setArray();
System.out.println(" Before Sorting:");
printArray(arr);
arr = selectionSort(arr);
System.out.println(" After Sorting:");
printArray(arr);
}
/**
* reads and stores the data in the one-dimensional array
*/
/**
* @return
*/
public static int[] setArray() {
int arr[] = new int[12];
System.out.println("Enter 12 integers.");
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
return arr;
}
/**
* prints the data in the one-dimensional array
*/
public static void printArray(int[] arr) {
System.out.println();
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
/**
* method will sort the array in ascending order
*/
public static int[] selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
}
OUTPUT:
Enter 12 integers.
4 23 65 34 84 37 12 17 24 36 82 51
Before Sorting:
4 23 65 34 84 37 12 17 24 36 82 51
After Sorting:
4 12 17 23 24 34 36 37 51 65 82 84

More Related Content

PDF
ArrayOperations.java import java.util.Arrays; import java.util.pdf
PDF
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
PDF
import java.util.Scanner;public class ArrayOperation {    inp.pdf
PDF
Prompt a user to enter a series of integers separated by spaces and .pdf
PDF
Write a program (any language) to randomly generate the following se.pdf
PDF
import java.awt.LinearGradientPaint; public class searchComparis.pdf
PDF
Star pattern programs in java Print Star pattern in java and print triangle ...
PPTX
Basic java, java collection Framework and Date Time API
ArrayOperations.java import java.util.Arrays; import java.util.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
import java.util.Scanner;public class ArrayOperation {    inp.pdf
Prompt a user to enter a series of integers separated by spaces and .pdf
Write a program (any language) to randomly generate the following se.pdf
import java.awt.LinearGradientPaint; public class searchComparis.pdf
Star pattern programs in java Print Star pattern in java and print triangle ...
Basic java, java collection Framework and Date Time API

Similar to (JAVA NetBeans) Write a Java program able to perform selection sort-So.docx (14)

PDF
Review Questions for Exam 10182016 1. public class .pdf
PDF
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
PDF
Java Unit 1 Project
PDF
Frequency .java Word frequency counter package frequ.pdf
PPTX
java input & output statements
PDF
Simple 27 Java Program on basic java syntax
DOCX
java program assigment -2
PDF
Simple Java Program for beginner with easy method.pdf
DOCX
PDF
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
PDF
PDF
Please fix the java code (using eclipse)package hw4p1;import jav.pdf
PDF
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
DOCX
object oriented programming lab manual .docx
Review Questions for Exam 10182016 1. public class .pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
Java Unit 1 Project
Frequency .java Word frequency counter package frequ.pdf
java input & output statements
Simple 27 Java Program on basic java syntax
java program assigment -2
Simple Java Program for beginner with easy method.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
Please fix the java code (using eclipse)package hw4p1;import jav.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
object oriented programming lab manual .docx
Ad

More from dorisc7 (20)

DOCX
(TCO 2) Which of the following project management activities is associ.docx
DOCX
(TCO 1) Who are the users of managerial accounting information- How do.docx
DOCX
(Subsequent events) Green- CPA- is auditing the financial statements o.docx
DOCX
(Reliability engineering) Make a list of six population stereotypical.docx
DOCX
-Java programming- Write a class named Junior-java Inherits the Stud.docx
DOCX
--Social Organizations and Core Competencies-- Please respond to the f.docx
DOCX
-- I have two classes One is Carnivore and the other is Herbivore- Eac.docx
DOCX
- Answer the following questions about acids 1- What is the phase of a.docx
DOCX
- At time 110 four processes (P1 P4) are waiting for a timeout signal.docx
DOCX
(70 pts) Nuclear History Identify the event in nuclear history that oc.docx
DOCX
(2) The CPU incorporates several major components (as shown in Fig- de.docx
DOCX
(Audit evidence) The third GAAS of field work requires that the audito.docx
DOCX
What is the Big Bang-SolutionBig Bang is a theory which talks ababo th.docx
DOCX
What is the concept of sustainable income- Please describe it in your.docx
DOCX
What is the backbone for the Internet- Briefly describe how the Intern.docx
DOCX
What is the pH of a 0-1 M aqueous solution of NaH2PO4SolutionNaH2PO4 -.docx
DOCX
what is the ph of a 0-16M NaF(aq) solution-SolutionNaF(aq) -----------.docx
DOCX
What is the pH of a 0-114 M solution of the acid HA if its ionization (1).docx
DOCX
What is the minimum number of nodes in an AVL tree of height 8- Let N(.docx
DOCX
What is the meaning of the keyword this- Explain why it is valid only.docx
(TCO 2) Which of the following project management activities is associ.docx
(TCO 1) Who are the users of managerial accounting information- How do.docx
(Subsequent events) Green- CPA- is auditing the financial statements o.docx
(Reliability engineering) Make a list of six population stereotypical.docx
-Java programming- Write a class named Junior-java Inherits the Stud.docx
--Social Organizations and Core Competencies-- Please respond to the f.docx
-- I have two classes One is Carnivore and the other is Herbivore- Eac.docx
- Answer the following questions about acids 1- What is the phase of a.docx
- At time 110 four processes (P1 P4) are waiting for a timeout signal.docx
(70 pts) Nuclear History Identify the event in nuclear history that oc.docx
(2) The CPU incorporates several major components (as shown in Fig- de.docx
(Audit evidence) The third GAAS of field work requires that the audito.docx
What is the Big Bang-SolutionBig Bang is a theory which talks ababo th.docx
What is the concept of sustainable income- Please describe it in your.docx
What is the backbone for the Internet- Briefly describe how the Intern.docx
What is the pH of a 0-1 M aqueous solution of NaH2PO4SolutionNaH2PO4 -.docx
what is the ph of a 0-16M NaF(aq) solution-SolutionNaF(aq) -----------.docx
What is the pH of a 0-114 M solution of the acid HA if its ionization (1).docx
What is the minimum number of nodes in an AVL tree of height 8- Let N(.docx
What is the meaning of the keyword this- Explain why it is valid only.docx
Ad

Recently uploaded (20)

PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Classroom Observation Tools for Teachers
PPTX
Cell Types and Its function , kingdom of life
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Pharma ospi slides which help in ospi learning
Module 4: Burden of Disease Tutorial Slides S2 2025
O7-L3 Supply Chain Operations - ICLT Program
Classroom Observation Tools for Teachers
Cell Types and Its function , kingdom of life
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
RMMM.pdf make it easy to upload and study
Chinmaya Tiranga quiz Grand Finale.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Microbial diseases, their pathogenesis and prophylaxis
FourierSeries-QuestionsWithAnswers(Part-A).pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3

(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx

  • 1. (JAVA NetBeans) Write a Java program able to perform selection sort. Solution import java.util.Scanner; /** * @author * */ public class SelectionSort { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int arr[] = setArray(); System.out.println(" Before Sorting:"); printArray(arr); arr = selectionSort(arr); System.out.println(" After Sorting:"); printArray(arr); } /** * reads and stores the data in the one-dimensional array */ /** * @return */ public static int[] setArray() { int arr[] = new int[12]; System.out.println("Enter 12 integers.");
  • 2. Scanner scanner = new Scanner(System.in); for (int i = 0; i < arr.length; i++) { arr[i] = scanner.nextInt(); } return arr; } /** * prints the data in the one-dimensional array */ public static void printArray(int[] arr) { System.out.println(); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } /** * method will sort the array in ascending order */ public static int[] selectionSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int index = i; for (int j = i + 1; j < arr.length; j++) if (arr[j] < arr[index]) index = j; int smallerNumber = arr[index]; arr[index] = arr[i]; arr[i] = smallerNumber; } return arr; } } OUTPUT:
  • 3. Enter 12 integers. 4 23 65 34 84 37 12 17 24 36 82 51 Before Sorting: 4 23 65 34 84 37 12 17 24 36 82 51 After Sorting: 4 12 17 23 24 34 36 37 51 65 82 84