Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chapter_3_sorting;
- import java.util.Arrays;
- /**
- * Pseudocode for Selection Sort
- * @author shivamchopra
- *
- *
- * for i = 0 to Array.length - 2
- * minIndex = i;
- * for j = i + 1 to Array.length - 1
- * if(data[j] < data[minIndex])
- * minIndex = j
- * temp = data[minIndex]
- * data[minIndex] = data[i]
- * data[i] = temp;
- *
- */
- public class SelectionSorting {
- public static void main(String[] args) {
- int[] numbers = { 1, 5, 2, 8, 2, 9, 10, 32, 3 };
- System.out.println("Unsorted Array : " + Arrays.toString(numbers));
- SelectionSorting sorting = new SelectionSorting();
- sorting.sort(numbers);
- System.out.println("Sorted Array : " + Arrays.toString(numbers));
- }
- public void sort(int[] numbers) {
- int minIndex = 0;
- for(int i = 0; i < numbers.length - 1; i++) {
- minIndex = i;
- for(int j = i + 1; j < numbers.length; j++) {
- if(numbers[j] < numbers[minIndex]) {
- minIndex = j;
- int temp = numbers[minIndex];
- numbers[minIndex] = numbers[i];
- numbers[i] = temp;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment