
package org.code2care.java.sorting.algo;
import java.util.Arrays;
/**
* SelectionSort Example in Java
*
* Author: Code2care.org
* Date: 28 April 2023
* Version: v1.0
*
*/
public class SelectionSortJavaExample {
public static void main(String[] args) {
//Unsorted Array
int[] unsortedArray = {4, 99, 2, 11, 34, 67, 54, 12, 45, 245, 234, 12, 200};
SelectionSortJavaExample selectionSort = new SelectionSortJavaExample();
int[] sortedArray = selectionSort.selectionSortAlgorithm(unsortedArray);
System.out.println(Arrays.toString(sortedArray));
}
public int[] selectionSortAlgorithm(int[] arrayOfNumbers) {
int arrayLength = arrayOfNumbers.length;
for (int i = 0; i < arrayLength - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arrayLength; j++) {
if (arrayOfNumbers[j] < arrayOfNumbers[minIndex]) {
minIndex = j;
}
}
int tempVariable = arrayOfNumbers[minIndex];
arrayOfNumbers[minIndex] = arrayOfNumbers[i];
arrayOfNumbers[i] = tempVariable;
}
return arrayOfNumbers;
}
}
Time Complexity | |||||
---|---|---|---|---|---|
Best Case | Average Case | Worst Case | Space Complexity | ||
Selection Sort | O(n^2) | O(n^2) | O(n^2) | O(1) |
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- How to change default macOS Terminal Window size - MacOS
- [fix] URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) IntelliJ - Java
- Steps to Install Jenkins on M1/M2 Mac - MacOS
- The Date Command and its usage [Linux - Unix - macOS] - Linux
- Ubuntu: How to search for a package to install using apt in Terminal - Ubuntu
- How to deep copy a dictionary in Python - Python
- How to find version of Cargo in Rust - Rust
- How to hard reset Mac Terminal Window - MacOS